All pastes #754564 Raw Edit

Anonymous

public text v1 · immutable
#754564 ·published 2007-10-30 02:29 UTC
rendered paste body
#include <iostream.h>
#include <math.h>

struct point{ float x, y; };

struct linedef{
	point a, b, m, mid, perpen, tri;
	float dist, y_int;
};

void get_coord(point &p){
	char trash;
	cout << "Enter in a point x,y: ";
	cin >> p.x >> trash >> p.y;
}


// function reduces fractions, passed by reference
//   input:  fraction numerator (num) and denominator (den)
//   output: reduced numerator and denominator  
void reduce(int &num, int &den){
	int i = 1, least, gcf;
	
	if(num >= den)
		least = num;
	else
		least = den;
	
	for(i; i <= least; i++){
		if((num % i == 0) && (den % i == 0))
			gcf = i;
	}

	num = num / gcf;
	den = den / gcf;
}

float distance(point a, point b){
	float dist;
	dist = sqrt(pow((b.x - a.x), 2) + pow((b.y - a.y), 2));
	return dist;
}

point midpoint(point a, point b){
	point m;
	m.x = (a.x + b.x) / 2.0;
	m.y = (a.y + b.y) / 2.0;
	return m;
}

point slope(point a, point b){
	point m;
	int x, y;
	m.y = b.y - a.y;
	m.x = b.x - a.x;
	x = int(m.x);
	y = int(m.y);
	reduce(y, x);
	m.x = float(x);
	m.y = float(y);
	return m;
}

float y_intercept(point a, point m){
	float intercept;
	intercept = (((m.y / m.x) * a.x) - a.y) * -1;
	return intercept;
}

void output(linedef l){
	cout << "point a: (" << l.a.x << ", " << l.a.y << ")" << endl;
	cout << "point b: (" << l.b.x << ", " << l.b.y << ")" << endl;
	cout << "slope: " << l.m.x << "/" << l.m.y << endl;
	cout << "slope of perpendicular line: " << l.perpen.y << "/" << l.perpen.x << endl;
	cout << "midpoint: (" << l.mid.x << ", " << l.mid.y << ")" << endl;
	cout << "distance: " << l.dist << endl;
	cout << "y intercept: " << l.y_int << endl;
	cout << "point that makes line a right triangle: " << "(" << l.tri.x << ", " << l.tri.y << ")" << endl;
	cout << "line equasion: y = (" << l.m.y << "/" << l.m.x << ")x + " << l.y_int << endl;
}

void p_slope(point m, point &perpen){
	perpen.y = -1 * m.x;
	perpen.x = m.y;
}

point triangle(point a, point b){
	point tri;
	tri.x = b.x;
	tri.y = a.y;
	return tri;
}


int main(){
	linedef line;
	
	get_coord(line.a);
	get_coord(line.b);

	line.m = slope(line.a, line.b);
	line.mid = midpoint(line.a, line.b);
	line.dist = distance(line.a, line.b);
	line.y_int = y_intercept(line.a, line.m);
	line.tri = triangle(line.a, line.b);
	p_slope(line.m, line.perpen);


	output(line);

	return 0;

}