1 // geometry.cpp: Algebraic geometry helper functions
3 // Part of the Architektonas Project
4 // (C) 2011 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 08/31/2011 Created this file
13 // NOTE: All methods in this class are static.
19 #include "dimension.h"
23 Point Geometry::IntersectionOfLineAndLine(Point p1, Point p2, Point p3, Point p4)
25 // Find the intersection of the lines by formula:
26 // px = (x1y2 - y1x2)(x3 - x4) - (x1 - x2)(x3y4 - y3x4)
27 // py = (x1y2 - y1x2)(y3 - y4) - (y1 - y2)(x3y4 - y3x4)
28 // d = (x1 - x2)(y3 - y4) - (y1 - y2)(x3 - x4) = 0 if lines are parallel
29 // Intersection is (px / d, py / d)
31 double d = ((p1.x - p2.x) * (p3.y - p4.y)) - ((p1.y - p2.y) * (p3.x - p4.x));
33 // Check for parallel lines, and return sentinel if so
35 return Point(0, 0, -1);
37 double px = (((p1.x * p2.y) - (p1.y * p2.x)) * (p3.x - p4.x))
38 - ((p1.x - p2.x) * ((p3.x * p4.y) - (p3.y * p4.x)));
39 double py = (((p1.x * p2.y) - (p1.y * p2.x)) * (p3.y - p4.y))
40 - ((p1.y - p2.y) * ((p3.x * p4.y) - (p3.y * p4.x)));
42 return Point(px / d, py / d, 0);
46 // Returns the parameter of a point in space to this vector. If the parameter
47 // is between 0 and 1, the normal of the vector to the point is on the vector.
48 // Note: lp1 is the tail, lp2 is the head of the line (vector).
49 double Geometry::ParameterOfLineAndPoint(Point tail, Point head, Point point)
51 // Geometric interpretation:
52 // The parameterized point on the vector lineSegment is where the normal of
53 // the lineSegment to the point intersects lineSegment. If the pp < 0, then
54 // the perpendicular lies beyond the 1st endpoint. If pp > 1, then the
55 // perpendicular lies beyond the 2nd endpoint.
57 Vector lineSegment = head - tail;
58 double magnitude = lineSegment.Magnitude();
59 Vector pointSegment = point - tail;
60 double t = lineSegment.Dot(pointSegment) / (magnitude * magnitude);
65 Point Geometry::MirrorPointAroundLine(Point point, Point p1, Point p2)
67 // Get the vector of the intersection of the line and the normal on the
68 // line to the point in question.
69 double t = ParameterOfLineAndPoint(p1, p2, point);
70 Vector v = Vector(p1, p2) * t;
72 // Get the point normal to point to the line passed in (p2 is the tail)
73 Point normalOnLine = p2 + v;
75 // Make our mirrored vector (head - tail)
76 Vector mirror = -(point - normalOnLine);
78 // Find the mirrored point
79 Point mirroredPoint = normalOnLine + mirror;
85 Point Geometry::RotatePointAroundPoint(Point point, Point rotationPoint, double angle)
87 Vector v = Vector(point, rotationPoint);
88 double px = (v.x * cos(angle)) - (v.y * sin(angle));
89 double py = (v.x * sin(angle)) + (v.y * cos(angle));
91 return Vector(rotationPoint.x + px, rotationPoint.y + py, 0);
95 double Geometry::Determinant(Point p1, Point p2)
97 return (p1.x * p2.y) - (p2.x * p1.y);
102 Intersecting line segments:
104 Segment L1 has edges A=(a1,a2), A'=(a1',a2').
105 Segment L2 has edges B=(b1,b2), B'=(b1',b2').
106 Segment L1 is the set of points tA'+(1-t)A, where 0<=t<=1.
107 Segment L2 is the set of points sB'+(1-s)B, where 0<=s<=1.
108 Segment L1 meet segment L2 if and only if for some t and s we have
109 tA'+(1-t)A=sB'+(1-s)B
110 The solution of this with respect to t and s is
112 t=((-b?'a?+b?'b?+b?a?+a?b?'-a?b?-b?b?')/(b?'a?'-b?'a?-b?a?'+b?a?-a?'b?'+a?'b?+a?b?'-a?b?))
114 s=((-a?b?+a?'b?-a?a?'+b?a?+a?'a?-b?a?')/(b?'a?'-b?'a?-b?a?'+b?a?-a?'b??+a?'b?+a?b?'-a?b?))
116 So check if the above two numbers are both >=0 and <=1.
121 // Finds the intesection between two objects (if any)
122 bool Geometry::Intersects(Object * obj1, Object * obj2, double * t, double * s)
127 // Finds the intersection between two lines (if any)
128 int Geometry::Intersects(Line * l1, Line * l2, double * tp/*= 0*/, double * up/*= 0*/)
130 Vector r(l1->position, l1->endpoint);
131 Vector s(l2->position, l2->endpoint);
132 Vector v1 = l2->position - l1->position; // q - p
133 // Vector v2 = l1->position - l2->position; // p - q
134 //printf("l1: (%lf, %lf) (%lf, %lf), l2: (%lf, %lf) (%lf, %lf)\n", l1->position.x, l1->position.y, l1->endpoint.x, l1->endpoint.y, l2->position.x, l2->position.y, l2->endpoint.x, l2->endpoint.y);
135 double rxs = (r.x * s.y) - (s.x * r.y);
140 double qpxr = (v1.x * r.y) - (r.x * v1.y);
142 //printf(" --> R x S = 0! (q - p) x r = %lf\n", qpxr);
143 //printf(" -->(q - p) . r = %lf, r . r = %lf\n", v1.Dot(r), r.Dot(r));
144 //printf(" -->(p - q) . s = %lf, s . s = %lf\n", v2.Dot(s), s.Dot(s));
145 //printf(" -->(q - p) . s = %lf, (p - q) . r = %lf\n", v1.Dot(s), v2.Dot(r));
147 // Lines are parallel, so no intersection...
152 //this works IFF the vectors are pointing in the same direction. everything else
154 // If (q - p) . r == r . r, t = 1, u = 0
155 if (v1.Dot(r) == r.Dot(r))
157 // If (p - q) . s == s . s, t = 0, u = 1
158 else if (v2.Dot(s) == s.Dot(s))
163 // Check to see which endpoints are connected... Four possibilities:
164 if (l1->position == l2->position)
166 else if (l1->position == l2->endpoint)
168 else if (l1->endpoint == l2->position)
170 else if (l1->endpoint == l2->endpoint)
178 t = ((v1.x * s.y) - (s.x * v1.y)) / rxs;
179 u = ((v1.x * r.y) - (r.x * v1.y)) / rxs;
182 Now there are five cases (NOTE: only valid if vectors face the same way!):
184 1. If r × s = 0 and (q − p) × r = 0, then the two lines are collinear. If in addition, either 0 ≤ (q − p) · r ≤ r · r or 0 ≤ (p − q) · s ≤ s · s, then the two lines are overlapping.
186 2. If r × s = 0 and (q − p) × r = 0, but neither 0 ≤ (q − p) · r ≤ r · r nor 0 ≤ (p − q) · s ≤ s · s, then the two lines are collinear but disjoint.
188 3. If r × s = 0 and (q − p) × r ≠ 0, then the two lines are parallel and non-intersecting.
190 4. If r × s ≠ 0 and 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1, the two line segments meet at the point p + t r = q + u s.
192 5. Otherwise, the two line segments are not parallel but do not intersect.
194 // Return parameter values, if user passed in valid pointers
201 // If the parameters are in range, we have overlap!
202 if ((t >= 0) && (t <= 1.0) && (u >= 0) && (u <= 1.0))
209 // Finds the intersection between two lines (if any)
210 int Geometry::Intersects(Line * l1, Dimension * d1, double * tp/*= 0*/, double * up/*= 0*/)
212 Line l2(d1->position, d1->endpoint);
213 return Intersects(l1, &l2, tp, up);
217 // Finds the intesection(s) between a line and a circle (if any)
218 int Geometry::Intersects(Line * l, Circle * c, double * tp/*= 0*/, double * up/*= 0*/, double * vp/*= 0*/, double * wp/*= 0*/)
221 Vector center = c->position;
222 Vector v1 = l->position - center;
223 Vector v2 = l->endpoint - center;
225 double dr = d.Magnitude();
226 double determinant = (v1.x * v2.y) - (v1.y * v2.x);
228 double discriminant = ((c->radius * c->radius) * (dr * dr)) - (determinant * determinant);
230 if (discriminant < 0)
238 I'm thinking a better approach to this might be as follows:
240 -- Get the distance of the circle's center from the line segment. If it's
241 > the radius, it doesn't intersect.
242 -- If the parameter is off the line segment, check distance to endpoints. (Not sure
243 how to proceed from here, it's different than the following.)
244 [Actually, you can use the following for all of it. You only know if you have
245 an intersection at the last step, which is OK.]
246 -- If the radius == distance, we have a tangent line.
247 -- If radius > distance, use Pythagorus to find the length on either side of the
248 normal to the spots where the hypotenuse (== radius' length) contact the line.
249 -- Use those points to find the parameter on the line segment; if they're not on
250 the line segment, no intersection.
252 double t = ParameterOfLineAndPoint(l->position, l->endpoint, c->position);
253 //small problem here: it clamps the result to the line segment. NOT what we want
254 //here! !!! FIX !!! [DONE]
255 Vector p = l->GetPointAtParameter(t);
256 double distance = Vector::Magnitude(c->position, p);
258 // If the center of the circle is farther from the line than the radius, fail.
259 if (distance > c->radius)
262 // Now we have to check for intersection points.
263 // Tangent case: (needs to return something)
264 if ((distance == c->radius) && (t >= 0.0) && (t <= 1.0))
267 // The line intersects the circle in two points (possibly). Use Pythagorus
268 // to find them for testing.
269 double offset = sqrt((c->radius * c->radius) - (distance * distance));
270 //need to convert distance to paramter value... :-/
271 //t = position on line / length of line segment, so if we divide the offset by length,
272 //that should give us what we want.
273 double length = Vector::Magnitude(l->position, l->endpoint);
274 double t1 = t + (offset / length);
275 double t2 = t - (offset / length);
277 //need to find angles for the circle...
278 Vector cp1 = l->position + (Vector(l->position, l->endpoint) * (length * t1));
279 Vector cp2 = l->position + (Vector(l->position, l->endpoint) * (length * t2));
280 double a1 = Vector(c->position, cp1).Angle();
281 double a2 = Vector(c->position, cp2).Angle();
283 //instead of this, return a # which is the # of intersections. [DONE]
284 int intersections = 0;
286 // Now check for if the parameters are in range
287 if ((t1 >= 0) && (t1 <= 1.0))
292 if ((t2 >= 0) && (t2 <= 1.0))
297 return intersections;