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 tail, Point head)
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(tail, head, point);
70 Vector v = Vector(tail, head) * t;
72 // Get the point normal to point to the line passed in
73 Point normalOnLine = tail + 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 // Vector v = Vector(rotationPoint, point);
89 double px = (v.x * cos(angle)) - (v.y * sin(angle));
90 double py = (v.x * sin(angle)) + (v.y * cos(angle));
92 return Vector(rotationPoint.x + px, rotationPoint.y + py, 0);
96 double Geometry::Determinant(Point p1, Point p2)
98 return (p1.x * p2.y) - (p2.x * p1.y);
103 Intersecting line segments:
105 Segment L1 has edges A=(a1,a2), A'=(a1',a2').
106 Segment L2 has edges B=(b1,b2), B'=(b1',b2').
107 Segment L1 is the set of points tA'+(1-t)A, where 0<=t<=1.
108 Segment L2 is the set of points sB'+(1-s)B, where 0<=s<=1.
109 Segment L1 meet segment L2 if and only if for some t and s we have
110 tA'+(1-t)A=sB'+(1-s)B
111 The solution of this with respect to t and s is
113 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?))
115 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?))
117 So check if the above two numbers are both >=0 and <=1.
122 // Finds the intesection between two objects (if any)
123 bool Geometry::Intersects(Object * obj1, Object * obj2, double * t, double * s)
128 // Finds the intersection between two lines (if any)
129 int Geometry::Intersects(Line * l1, Line * l2, double * tp/*= 0*/, double * up/*= 0*/)
131 Vector r(l1->position, l1->endpoint);
132 Vector s(l2->position, l2->endpoint);
133 Vector v1 = l2->position - l1->position; // q - p
134 // Vector v2 = l1->position - l2->position; // p - q
135 //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);
136 double rxs = (r.x * s.y) - (s.x * r.y);
141 double qpxr = (v1.x * r.y) - (r.x * v1.y);
143 //printf(" --> R x S = 0! (q - p) x r = %lf\n", qpxr);
144 //printf(" -->(q - p) . r = %lf, r . r = %lf\n", v1.Dot(r), r.Dot(r));
145 //printf(" -->(p - q) . s = %lf, s . s = %lf\n", v2.Dot(s), s.Dot(s));
146 //printf(" -->(q - p) . s = %lf, (p - q) . r = %lf\n", v1.Dot(s), v2.Dot(r));
148 // Lines are parallel, so no intersection...
153 //this works IFF the vectors are pointing in the same direction. everything else
155 // If (q - p) . r == r . r, t = 1, u = 0
156 if (v1.Dot(r) == r.Dot(r))
158 // If (p - q) . s == s . s, t = 0, u = 1
159 else if (v2.Dot(s) == s.Dot(s))
164 // Check to see which endpoints are connected... Four possibilities:
165 if (l1->position == l2->position)
167 else if (l1->position == l2->endpoint)
169 else if (l1->endpoint == l2->position)
171 else if (l1->endpoint == l2->endpoint)
179 t = ((v1.x * s.y) - (s.x * v1.y)) / rxs;
180 u = ((v1.x * r.y) - (r.x * v1.y)) / rxs;
183 Now there are five cases (NOTE: only valid if vectors face the same way!):
185 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.
187 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.
189 3. If r × s = 0 and (q − p) × r ≠ 0, then the two lines are parallel and non-intersecting.
191 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.
193 5. Otherwise, the two line segments are not parallel but do not intersect.
195 // Return parameter values, if user passed in valid pointers
202 // If the parameters are in range, we have overlap!
203 if ((t >= 0) && (t <= 1.0) && (u >= 0) && (u <= 1.0))
210 // Finds the intersection between two lines (if any)
211 int Geometry::Intersects(Line * l1, Dimension * d1, double * tp/*= 0*/, double * up/*= 0*/)
213 Line l2(d1->position, d1->endpoint);
214 return Intersects(l1, &l2, tp, up);
218 // Finds the intesection(s) between a line and a circle (if any)
219 int Geometry::Intersects(Line * l, Circle * c, double * tp/*= 0*/, double * up/*= 0*/, double * vp/*= 0*/, double * wp/*= 0*/)
222 Vector center = c->position;
223 Vector v1 = l->position - center;
224 Vector v2 = l->endpoint - center;
226 double dr = d.Magnitude();
227 double determinant = (v1.x * v2.y) - (v1.y * v2.x);
229 double discriminant = ((c->radius * c->radius) * (dr * dr)) - (determinant * determinant);
231 if (discriminant < 0)
239 I'm thinking a better approach to this might be as follows:
241 -- Get the distance of the circle's center from the line segment. If it's
242 > the radius, it doesn't intersect.
243 -- If the parameter is off the line segment, check distance to endpoints. (Not sure
244 how to proceed from here, it's different than the following.)
245 [Actually, you can use the following for all of it. You only know if you have
246 an intersection at the last step, which is OK.]
247 -- If the radius == distance, we have a tangent line.
248 -- If radius > distance, use Pythagorus to find the length on either side of the
249 normal to the spots where the hypotenuse (== radius' length) contact the line.
250 -- Use those points to find the parameter on the line segment; if they're not on
251 the line segment, no intersection.
253 double t = ParameterOfLineAndPoint(l->position, l->endpoint, c->position);
254 //small problem here: it clamps the result to the line segment. NOT what we want
255 //here! !!! FIX !!! [DONE]
256 Vector p = l->GetPointAtParameter(t);
257 double distance = Vector::Magnitude(c->position, p);
259 // If the center of the circle is farther from the line than the radius, fail.
260 if (distance > c->radius)
263 // Now we have to check for intersection points.
264 // Tangent case: (needs to return something)
265 if ((distance == c->radius) && (t >= 0.0) && (t <= 1.0))
268 // The line intersects the circle in two points (possibly). Use Pythagorus
269 // to find them for testing.
270 double offset = sqrt((c->radius * c->radius) - (distance * distance));
271 //need to convert distance to paramter value... :-/
272 //t = position on line / length of line segment, so if we divide the offset by length,
273 //that should give us what we want.
274 double length = Vector::Magnitude(l->position, l->endpoint);
275 double t1 = t + (offset / length);
276 double t2 = t - (offset / length);
278 //need to find angles for the circle...
279 Vector cp1 = l->position + (Vector(l->position, l->endpoint) * (length * t1));
280 Vector cp2 = l->position + (Vector(l->position, l->endpoint) * (length * t2));
281 double a1 = Vector(c->position, cp1).Angle();
282 double a2 = Vector(c->position, cp2).Angle();
284 //instead of this, return a # which is the # of intersections. [DONE]
285 int intersections = 0;
287 // Now check for if the parameters are in range
288 if ((t1 >= 0) && (t1 <= 1.0))
293 if ((t2 >= 0) && (t2 <= 1.0))
298 return intersections;