Design aclassLine that implements a line, which is represented by the for-mula y
ID: 3648583 • Letter: D
Question
Design aclassLine that implements a line, which is represented by the for-mula y=ax+b. Your class should store a and b as double member vari-ables. Write a member function intersect(?) that returns the x coordinate at which this line intersects line ?. If the two lines are parallel, then yourfunction should throw an exception Parallel. Write a C++ program thatcreates a number of Line objects and tests each pair for intersection. Yourprogram should print an appropriate error message for parallel lines.Explanation / Answer
#ifndef LINE_H #define LINE_H #include "Point.h" using namespace std; class Line { public: //constructors Line(); //default constructor //non-default constructor //copy constructor //getters and setters //getStart //getEnd //setStart //setEnd //other useful methods //getSlope //getMidpoint //print //distance private: //private, helper methods //getABC Point start; Point end; }; #endif /* LINE_H */ #include #include "Line.h" using namespace std; //constructors Line::Line() //default constructor { } //non-default constructor //copy constructor //getters and setters //getStart //getEnd //setStart //setEnd //other useful methods //getSlope //getMidpoint //print //distance //private, helper methods //getABC #ifdef HAVE_CONFIG_H # include #endif #include "geom.h" #include #include /* Intersect two lines */ /** * Finds the intersection of the two (infinite) lines * defined by the points p such that dot(n0, p) == d0 and dot(n1, p) == d1. * * If the two lines intersect, then result becomes their point of * intersection; otherwise, result remains unchanged. */ sp_intersector_kind sp_intersector_line_intersection(NR::Point const &n0, double const d0, NR::Point const &n1, double const d1, NR::Point &result) { /* This function finds the intersection of the two lines (infinite) * defined by n0.X = d0 and x1.X = d1. The algorithm is as follows: * To compute the intersection point use kramer's rule: * * convert lines to form * ax + by = c * dx + ey = f * * ( * e.g. a = (x2 - x1), b = (y2 - y1), c = (x2 - x1)*x1 + (y2 - y1)*y1 * ) * * In our case we use: * a = n0.x d = n1.x * b = n0.y e = n1.y * c = d0 f = d1 * * so: * * adx + bdy = cd * adx + aey = af * * bdy - aey = cd - af * (bd - ae)y = cd - af * * y = (cd - af)/(bd - ae) * * repeat for x and you get: * * x = (fb - ce)/(bd - ae) * * if the denominator (bd-ae) is 0 then the lines are parallel, if the * numerators are then 0 then the lines coincide. */ double denominator = dot(rot90(n0), n1); double X = (n1[NR::Y] * d0 - n0[NR::Y] * d1); /* X = (-d1, d0) dot (n0[Y], n1[Y]) */ if(denominator == 0) { if ( X == 0 ) { return coincident; } else { return parallel; } } double Y = (n0[NR::X] * d1 - n1[NR::X] * d0); result = NR::Point(X, Y)/denominator; return intersects; } /* New code which we are not yet using */ #ifdef HAVE_NEW_INTERSECTOR_CODE /* ccw exists as a building block */ static int sp_intersector_ccw(const NR::Point p0, const NR::Point p1, const NR::Point p2) /* Determine which way a set of three points winds. */ { NR::Point d1 = p1 - p0; NR::Point d2 = p2 - p0; /* compare slopes but avoid division operation */ double c = dot(rot90(d1), d2); if(c > 0) return +1; // ccw - do these match def'n in header? if(c < 0) return -1; // cw /* Colinear [or NaN]. Decide the order. */ if ( ( d1[0] * d2[0] < 0 ) || ( d1[1] * d2[1] < 0 ) ) { return -1; // p2Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.