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)

instead of:

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

and I cannot figure out why. Please help??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <iostream>
#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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <cmath>
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.