All pastes #756075 Raw Edit

Unnamed

public cpp v1 · immutable
#756075 ·published 2007-10-31 03:35 UTC
rendered paste body
/* The following program will find multiple things about two given points.first, it will find the slope of the line.  then it will find the midpointfrom the two points, and the distance between the two lines. then the slopefor a perpendicular line is found.  all slopes will be reduced and simplifiedfinally, a point to make the two points into a right triangle will be found.the output function takes care of most, if not all, different types of output,including if the slope is none or undefined, if the y intercept doesnt exist (due to vertical line), and how it will display the equasion.  the point structis two floats, x and y.  the linedef struct holds the points (nested struct)a, b, m (slope), mid (midpoint), perpen (perpendicular line slope), and tri(point to make triangle), and the floats dist (distance), and y_int (y-intercept).  program continues until user enters anything else except 'y'on the done function.*/#include <iostream.h>#include <math.h>struct point{ float x, y; };struct linedef{	point a, b, m, mid, perpen, tri;	float dist, y_int;};// function is input for a single coordinate// output: point structpoint get_coord(){	point p;	char trash;	cout << "Enter in a point x,y: ";	cin >> p.x >> trash >> p.y;	return p;}// 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;}// function finds distance between two points// input: two points as a point struct// output: the distance as floatfloat distance(point a, point b){	float dist;	dist = sqrt(pow((b.x - a.x), 2) + pow((b.y - a.y), 2));	return dist;}// function finds the midpoint between two points// input: two points as point struct// output: midpoint as point structpoint 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;}// function finds slope from two points// input: two points as point struct// output: slope as point structpoint 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;}// function finds the y intercept from a point, and the slope// input: point and slope as point struct// output y-int as floatfloat y_intercept(point a, point m){	float intercept;	intercept = (((m.y / m.x) * a.x) - a.y) * -1;	return intercept;}// function finds a perpendicular line's slope// input: slope as point struct// output perpendicular line's slope as point structpoint p_slope(point m){	point perpen;	perpen.y = -1 * m.x;	perpen.x = m.y;	return perpen;}// function finds a point that makes current two points into a right triangle// input: two points as point struct// output: third point as point structpoint triangle(point a, point b){	point tri;	if(a.x == b.x){		tri.x = a.y;		tri.y = b.y;	}	else if(a.y == b.y){		tri.x = a.x;		tri.y = b.x;	}	else{		tri.x = b.x;		tri.y = a.y;	}	return tri;}// function asks user if finished// output: boolean valuebool done(){	char fin;	cout << "are you finished (y/n): ";	cin >> fin;		if(fin == 'y' || fin == 'y')		return true;	else		return false;}// function outputs information found about lines// input: linedef structvoid 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: ";	if(l.m.y == 0)		cout << "0";	else if(l.m.x == 0)		cout << "undefined";	else if(abs(l.m.y) == abs(l.m.x)){		if(l.m.y == l.m.x)			cout << "1";		else			cout << "-1";	}	else cout << l.m.y << "/" << l.m.x;	cout << endl;		cout << "slope: ";	if(l.perpen.y == 0)		cout << "0";	else if(l.perpen.x == 0)		cout << "undefined";	else if(abs(l.perpen.y) == abs(l.perpen.x)){		if(l.perpen.y == l.perpen.x)			cout << "1";		else			cout << "-1";	}	else		cout << l.perpen.y << "/" << l.perpen.x;	cout << endl;	cout << "midpoint: (" << l.mid.x << ", " << l.mid.y << ")" << endl;	cout << "distance: " << l.dist << endl;	cout << "y intercept: ";	if(l.m.x != 0)		cout << l.y_int << endl;	else		cout << "no y intercept" << endl;		cout << "point that makes line a right triangle: " << "(" << l.tri.x << ", " << l.tri.y << ")" << endl;			cout << "line equation: ";	if(l.m.x != 0){		cout << "y = ";		if(abs(l.m.y == 0))			cout << l.y_int;		else if(abs(l.m.y) == abs(l.m.x)){			if(l.m.y != l.m.x)				cout << "-x ";			else				cout << "x ";		}		else			cout << "(" << l.m.y << "/" << l.m.x << ") * x ";		if(abs(l.m.y) != 0){			if(l.y_int > 0)				cout << "+ " << l.y_int;			else if(l.y_int < 0)				cout << l.y_int;		}	}	else		cout << "x = " << l.a.x;	cout << endl;}// function continues program until user decides to quit// output: boolean valuebool run(){	linedef line;		line.a = get_coord();	line.b = get_coord();	cout << endl;	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);	line.perpen = p_slope(line.m);	output(line);		return done();}int main(){	while(run() != true);	return 0;}