### Implementation file issues

This is my implementation file for Class Line. The class essentially finds the equation of a line given 2 sets of coordinates. It has various member functions that calculate things like the intercept, whether 2 lines are perpendicular, parallel, etc..

My question if for my intercept member function. For some reason, it returns:

Line 1: y = 1.03297x + 0.758242
Line 2: y = x + 5
Intersect at (2.34054,7.09878)

Line 1: y = 1.03297x -0.758242
Line 2: y = x + 5
Intersect at (174.667,179.667)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247`` ``````#include #include "Line.h" using namespace std; //Constructors for Line Line::Line() { p1 = Point(); p2 = Point(1.0,1.0); } Line::Line(Point a, Point b) { p1 = a; p2 = b; } //Set value functions void Line::setFirstPoint(Point a) { p1 = a; } void Line::setSecondPoint(Point a) { p2 = a; } //Point returning functions Point Line::getFirstPoint() const { return p1; } Point Line::getSecondPoint() const { return p2; } //Calculation functions bool Line::slope(double& m) const { bool isSlope = false; m = 0.0; //Avoid division by zero, check first. if(p2.x - p1.x > 0.00001 && p2.y - p1.y > 0.00001) { m = (p2.y - p1.y) / (p2.x - p1.x); isSlope = true; } return isSlope; } bool Line::yIntercept(double& b) const { bool isIntercept = false; double m = (p2.y - p1.y) / (p2.x - p1.x); b = (m * p1.x) + p1.y ; if(b > 0.0) isIntercept = true; return isIntercept; } bool Line::isParallel(Line a) const { bool parallel = false; double slope1; double slope2; slope1 = (a.p2.y - a.p1.y) / (a.p2.x - a.p1.x); slope2 = (p2.y - p1.y) / (p2.x - p1.x); if(slope1 == slope2) parallel = true; return parallel; } bool Line::isCollinear(Line a) const { bool collinear = false; if((a.p1.y == p1.y) && (a.p1.x == p1.x) && (a.p2.y == p2.y) && (a.p2.x == p2.x)) collinear = true; return collinear; } bool Line::isPerpendicular(Line a) const { bool perpendicular = false; double slope1; double slope2; slope1 = (a.p2.y - a.p1.y) / (a.p2.x - a.p1.x); slope2 = (p2.y - p1.y) / (p2.x - p1.x); //Slopes are perpendicular if they are negative reciprocals if(slope1 + (1.0 / slope2) < 0.0000000001) perpendicular = true; return perpendicular; } Point Line::intersect(Line a) const { Point intersection (0.0, 0.0); double slope1, constant; double slope2, constant2; double one_delta_y = (a.p2.y - a.p1.y); double one_delta_x = (a.p2.x - a.p1.x); double two_delta_y = (p2.y - p1.y); double two_delta_x = (p2.x - p1.x); if(!isCollinear(a) && !isParallel(a)) { slope1 = one_delta_y / one_delta_x; slope2 = two_delta_y / two_delta_x; //Catching undefined slopes if(one_delta_x == 0.0) { constant = a.p2.x; slope1 = 0.0; } if(two_delta_x == 0.0) { constant2 = p2.x; slope2 = 0.0; } //Determining intersection point if(one_delta_x != 0.0 && two_delta_x != 0.0) { //if y - y1 = mx + b if(a.p1.y > 0.0000000001) { constant = (slope1 * a.p1.x) + a.p1.y; constant2 = (slope2 * p1.x) + p1.y; } //if y + y1 = mx + b else if (a.p1.y < 0.0000000001) { constant = (slope1 * a.p1.x) - a.p1.y; constant2 = (slope2 * p1.x) - p1.y; } if(constant + constant2 < 0.0000000001) { intersection.x = 0.0; intersection.y = 0.0; } if(slope1 + slope2 < 0.0000000001) { intersection.x = (constant + constant2) / 2.0; intersection.y = (slope1 * intersection.x) + (constant + constant2); } else { intersection.x = (constant + constant2) / (slope1 + slope2); intersection.y = (slope1 * intersection.x) + (constant + constant2); } } //If one equation is x = and the other is y = else if (one_delta_x == 0.0 && two_delta_x != 0.0) { intersection.x = (a.p2.x - ((slope2 * p1.x) + p1.y)) / slope2; intersection.y = (slope2 * intersection.x); } else if ((one_delta_x =! 0.0) && (two_delta_x == 0.0)) { intersection.x = (p2.x - ((slope1 * a.p1.x) + a.p1.y)) / slope1; intersection.y = (slope1 * intersection.x); } //If both equations are x = and are not collinear, //they must be parallel (no intersection) else { intersection.x = 0.0; intersection.y = 0.0; } } return intersection; } //Display Function void Line::display(ostream& out) const { double m = (p2.y - p1.y) / (p2.x - p1.x); double constant; if(p1.y > 0.0000000001) constant = (m * p1.x) + p1.y; else if (p1.y < 0.0000000001) constant = (m * p1.x) - p1.y; //incorrect sign on last test case upload site if((p2.x - p1.x) != 0.0) { out << "y = "; //Print coefficient if it is not 1 or -1 if((m > 1) || (m < -1)) { out << m << "x"; } //Do not print coefficient if it's 1 or -1 if(m == -1) out << "-x"; if (m == 1) out << "x"; //Handling constants within equation if(constant > 0.0) out << " + " << constant; else if(constant < 0.0) out << " " << constant; } else if((p2.x - p1.x) == 0) { out << "x = " << p2.x; } } ``````
If m1.x+b1= m2.x+b2 then
x=(b2-b1)/(m1-m2)
and the y coordinate follows by putting this back in either line. Whatever your complicated intercept program is doing I'm pretty sure it's not that. If you have infinite slopes (x=constant) then this would be a special case.

You don't provide compileable code, but I can see other problems.

If y=mx+b then b=y-mx, not what you have written on line 62. This probably explains your sign error.

Why shouldn't b be negative? Line 65.

If you are considering whether x is near zero then consider abs(x) < small, not x< small (lines 48 and 106)

Dangerous to check for exact equality of doubles (line 79).

Your collinear function is mathematically meaningless.

Slightly pedantically, since you are defining lines by points rather than slope and y intercept then it is perfectly possible for them to have zero or infinite slope ... but your perpendicular function won't like it. You could test on the dot product of the vectors joining the two pairs of points.

Here's two methods of finding line intersections: either from slopes and y-intercepts (if available) or from two points on the lines in general.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990`` ``````#include #include using namespace std; struct Point{ double x, y; }; struct Line{ Point a, b; }; const double EPS = 1.0e-10; //====================================================================== bool intersectionFromSlopeIntercept( double m1, double c1, double m2, double c2, Point &r ) { r = Point{ 0, 0 }; if ( abs( m1 - m2 ) < EPS ) return false; // Slopes are equal r.x = ( c2 - c1 ) / ( m1 - m2 ); r.y = m1 * r.x + c1; return true; } //====================================================================== bool intersectionFromLines( Line L, Line M, Point &r ) { r = Point{ 0, 0 }; double crossProduct = ( M.b.x - M.a.x ) * ( L.b.y - L.a.y ) - ( M.b.y - M.a.y ) * ( L.b.x - L.a.x ); if ( abs( crossProduct ) < EPS ) return false; // Slopes are equal (cross product of direction vectors is 0) // Fractional distance from a to b along line M by ratio of projections double fraction = ( ( L.b.x - M.a.x ) * ( L.b.y - L.a.y ) - ( L.b.y - M.a.y ) * ( L.b.x - L.a.x ) ) / crossProduct; r.x = M.a.x + fraction * ( M.b.x - M.a.x ); r.y = M.a.y + fraction * ( M.b.y - M.a.y ); return true; } //====================================================================== int main() { // Line 1 double m1 = 1.03297, c1 = -0.758242; Point La{ 0.0, -0.758242 }, Lb{ 1.0, 0.274728 }; Line L{ La, Lb }; // Line 2 double m2 = 1.0, c2 = 5.0; Point Ma{ 0.0, 5.0 }, Mb{ 1.0, 6.0 }; Line M{ Ma, Mb }; Point r; cout << "Line 1 is y = " << m1 << "x " << ( c1 > 0 ? "+" : "" ) << c1 << '\n'; cout << "Line 2 is y = " << m2 << "x " << ( c2 > 0 ? "+" : "" ) << c2 << '\n'; cout << "Slope/y-intercept method:\n"; if ( intersectionFromSlopeIntercept( m1, c1, m2, c2, r ) ) { cout << "x = " << r.x << '\n'; cout << "y = " << r.y << '\n'; } else { cout << "No intersection" << '\n'; } cout << "Line points method:\n"; if ( intersectionFromLines( L, M, r ) ) { cout << "x = " << r.x << '\n'; cout << "y = " << r.y << '\n'; } else { cout << "No intersection" << '\n'; } }``````

 ```Line 1 is y = 1.03297x -0.758242 Line 2 is y = 1x +5 Slope/y-intercept method: x = 174.651 y = 179.651 Line points method: x = 174.651 y = 179.651```

Last edited on
Topic archived. No new replies allowed.