/* 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 midpoint from the two points, and the distance between the two lines. then the slope for a perpendicular line is found. all slopes will be reduced and simplified finally, 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 struct is 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 #include 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 struct point 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 float float 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 struct 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; } // function finds slope from two points // input: two points as point struct // output: slope as point struct 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; } // function finds the y intercept from a point, and the slope // input: point and slope as point struct // output y-int as float float 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 struct point 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 struct point 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 value bool 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 struct 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: "; 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 value bool 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; }