Design a class Line that implements a line, which is represented by the formula
ID: 3663867 • Letter: D
Question
Design a class Line that implements a line, which is represented by the formula y = ax+b. Your class should store a and b as double member variables. Write a member function intersect(L) that returns the x coordinate at which this line intersects line L. If the two lines are parallel, then your function should return a message " Parallel" and exit. Write a C++ program that creates a number of Line objects and tests each pair for intersection. Your program should print an appropriate error message for parallel lines.
Explanation / Answer
bool LineCollision( const Point& A1, const Point& A2, const Point& B1, const Point& B2, double* out ) { Point a(A2-A1); Point b(B2-B1); double f = PerpDot(a,b); if(!f) // lines are parallel return false; Point c(B2-A2); double aa = PerpDot(a,c); double bb = PerpDot(b,c); if(f < 0) { if(aa > 0) return false; if(bb > 0) return false; if(aa < f) return false; if(bb < f) return false; } else { if(aa < 0) return false; if(bb < 0) return false; if(aa > f) return false; if(bb > f) return false; } if(out) *out = 1.0 - (aa / f); return true; } bool LineCollision( const Point& A1, const Point& A2, const Point& B1, const Point& B2, Point* out ) { //... if(out) *out = (b * (1.0 - (aa / f))) + B1; // this could probably be simplified -- but I'm lazy return true; } Related 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.