]> Shamusworld >> Repos - architektonas/blob - src/geometry.cpp
e0ad270c019d3e297c61786efe00974218384caf
[architektonas] / src / geometry.cpp
1 // geometry.cpp: Algebraic geometry helper functions
2 //
3 // Part of the Architektonas Project
4 // (C) 2011 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  08/31/2011  Created this file
12 //
13 // NOTE: All methods in this class are static.
14 //
15
16 #include "geometry.h"
17 #include <math.h>
18 #include "circle.h"
19 #include "dimension.h"
20 #include "line.h"
21 #include "mathconstants.h"
22
23
24 Point Geometry::IntersectionOfLineAndLine(Point p1, Point p2, Point p3, Point p4)
25 {
26         // Find the intersection of the lines by formula:
27         // px = (x1y2 - y1x2)(x3 - x4) - (x1 - x2)(x3y4 - y3x4)
28         // py = (x1y2 - y1x2)(y3 - y4) - (y1 - y2)(x3y4 - y3x4)
29         // d = (x1 - x2)(y3 - y4) - (y1 - y2)(x3 - x4) = 0 if lines are parallel
30         // Intersection is (px / d, py / d)
31
32         double d = ((p1.x - p2.x) * (p3.y - p4.y)) - ((p1.y - p2.y) * (p3.x - p4.x));
33
34         // Check for parallel lines, and return sentinel if so
35         if (d == 0)
36                 return Point(0, 0, -1);
37
38         double px = (((p1.x * p2.y) - (p1.y * p2.x)) * (p3.x - p4.x))
39                 - ((p1.x - p2.x) * ((p3.x * p4.y) - (p3.y * p4.x)));
40         double py = (((p1.x * p2.y) - (p1.y * p2.x)) * (p3.y - p4.y))
41                 - ((p1.y - p2.y) * ((p3.x * p4.y) - (p3.y * p4.x)));
42
43         return Point(px / d, py / d, 0);
44 }
45
46
47 // Returns the parameter of a point in space to this vector. If the parameter
48 // is between 0 and 1, the normal of the vector to the point is on the vector.
49 // Note: lp1 is the tail, lp2 is the head of the line (vector).
50 double Geometry::ParameterOfLineAndPoint(Point tail, Point head, Point point)
51 {
52         // Geometric interpretation:
53         // The parameterized point on the vector lineSegment is where the normal of
54         // the lineSegment to the point intersects lineSegment. If the pp < 0, then
55         // the perpendicular lies beyond the 1st endpoint. If pp > 1, then the
56         // perpendicular lies beyond the 2nd endpoint.
57
58         Vector lineSegment = head - tail;
59         double magnitude = lineSegment.Magnitude();
60         Vector pointSegment = point - tail;
61         double t = lineSegment.Dot(pointSegment) / (magnitude * magnitude);
62         return t;
63 }
64
65
66 Point Geometry::MirrorPointAroundLine(Point point, Point tail, Point head)
67 {
68         // Get the vector of the intersection of the line and the normal on the
69         // line to the point in question.
70         double t = ParameterOfLineAndPoint(tail, head, point);
71         Vector v = Vector(tail, head) * t;
72
73         // Get the point normal to point to the line passed in
74         Point normalOnLine = tail + v;
75
76         // Make our mirrored vector (head - tail)
77         Vector mirror = -(point - normalOnLine);
78
79         // Find the mirrored point
80         Point mirroredPoint = normalOnLine + mirror;
81
82         return mirroredPoint;
83 }
84
85
86 //
87 // point: The point we're rotating
88 // rotationPoint: The point we're rotating around
89 //
90 Point Geometry::RotatePointAroundPoint(Point point, Point rotationPoint, double angle)
91 {
92 //      Vector v = Vector(point, rotationPoint);
93         Vector v = Vector(rotationPoint, point);
94         double px = (v.x * cos(angle)) - (v.y * sin(angle));
95         double py = (v.x * sin(angle)) + (v.y * cos(angle));
96
97         return Vector(rotationPoint.x + px, rotationPoint.y + py, 0);
98 }
99
100
101 double Geometry::Determinant(Point p1, Point p2)
102 {
103         return (p1.x * p2.y) - (p2.x * p1.y);
104 }
105
106
107 /*
108 Intersecting line segments:
109 An easier way:
110 Segment L1 has edges A=(a1,a2), A'=(a1',a2').
111 Segment L2 has edges B=(b1,b2), B'=(b1',b2').
112 Segment L1 is the set of points tA'+(1-t)A, where 0<=t<=1.
113 Segment L2 is the set of points sB'+(1-s)B, where 0<=s<=1.
114 Segment L1 meet segment L2 if and only if for some t and s we have
115 tA'+(1-t)A=sB'+(1-s)B
116 The solution of this with respect to t and s is
117
118 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?))
119
120 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?))
121
122 So check if the above two numbers are both >=0 and <=1.
123 */
124
125
126 #if 0
127 // Finds the intesection between two objects (if any)
128 bool Geometry::Intersects(Object * obj1, Object * obj2, double * t, double * s)
129 {
130 }
131 #endif
132
133 #if 0
134 // Finds the intersection between two lines (if any)
135 int Geometry::Intersects(Line * l1, Line * l2, double * tp/*= 0*/, double * up/*= 0*/)
136 {
137         Vector r(l1->position, l1->endpoint);
138         Vector s(l2->position, l2->endpoint);
139         Vector v1 = l2->position - l1->position;        // q - p
140 //      Vector v2 = l1->position - l2->position;        // p - q
141 //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);
142         double rxs = (r.x * s.y) - (s.x * r.y);
143         double t, u;
144
145         if (rxs == 0)
146         {
147                 double qpxr = (v1.x * r.y) - (r.x * v1.y);
148
149 //printf("  --> R x S = 0! (q - p) x r = %lf\n", qpxr);
150 //printf("  -->(q - p) . r = %lf, r . r = %lf\n", v1.Dot(r), r.Dot(r));
151 //printf("  -->(p - q) . s = %lf, s . s = %lf\n", v2.Dot(s), s.Dot(s));
152 //printf("  -->(q - p) . s = %lf, (p - q) . r = %lf\n", v1.Dot(s), v2.Dot(r));
153
154                 // Lines are parallel, so no intersection...
155                 if (qpxr != 0)
156                         return 0;
157
158 #if 0
159 //this works IFF the vectors are pointing in the same direction. everything else
160 //is fucked!
161                 // If (q - p) . r == r . r, t = 1, u = 0
162                 if (v1.Dot(r) == r.Dot(r))
163                         t = 1.0, u = 0;
164                 // If (p - q) . s == s . s, t = 0, u = 1
165                 else if (v2.Dot(s) == s.Dot(s))
166                         t = 0, u = 1.0;
167                 else
168                         return 0;
169 #else
170                 // Check to see which endpoints are connected... Four possibilities:
171                 if (l1->position == l2->position)
172                         t = 0, u = 0;
173                 else if (l1->position == l2->endpoint)
174                         t = 0, u = 1.0;
175                 else if (l1->endpoint == l2->position)
176                         t = 1.0, u = 0;
177                 else if (l1->endpoint == l2->endpoint)
178                         t = 1.0, u = 1.0;
179                 else
180                         return 0;
181 #endif
182         }
183         else
184         {
185                 t = ((v1.x * s.y) - (s.x * v1.y)) / rxs;
186                 u = ((v1.x * r.y) - (r.x * v1.y)) / rxs;
187         }
188 /*
189 Now there are five cases (NOTE: only valid if vectors face the same way!):
190
191 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.
192
193 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.
194
195 3. If r × s = 0 and (q − p) × r ≠ 0, then the two lines are parallel and non-intersecting.
196
197 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.
198
199 5. Otherwise, the two line segments are not parallel but do not intersect.
200 */
201         // Return parameter values, if user passed in valid pointers
202         if (tp)
203                 *tp = t;
204
205         if (up)
206                 *up = u;
207
208         // If the parameters are in range, we have overlap!
209         if ((t >= 0) && (t <= 1.0) && (u >= 0) && (u <= 1.0))
210                 return 1;
211
212         return 0;
213 }
214
215
216 // Finds the intersection between two lines (if any)
217 int Geometry::Intersects(Line * l1, Dimension * d1, double * tp/*= 0*/, double * up/*= 0*/)
218 {
219         Line l2(d1->position, d1->endpoint);
220         return Intersects(l1, &l2, tp, up);
221 }
222
223
224 // Finds the intersection(s) between a line and a circle (if any)
225 int Geometry::Intersects(Line * l, Circle * c, double * tp/*= 0*/, double * up/*= 0*/, double * vp/*= 0*/, double * wp/*= 0*/)
226 {
227 #if 0
228         Vector center = c->position;
229         Vector v1 = l->position - center;
230         Vector v2 = l->endpoint - center;
231         Vector d = v2 - v1;
232         double dr = d.Magnitude();
233         double determinant = (v1.x * v2.y) - (v1.y * v2.x);
234
235         double discriminant = ((c->radius * c->radius) * (dr * dr)) - (determinant * determinant);
236
237         if (discriminant < 0)
238                 return false;
239
240         
241
242         return true;
243 #else
244 /*
245 I'm thinking a better approach to this might be as follows:
246
247 -- Get the distance of the circle's center from the line segment. If it's
248    > the radius, it doesn't intersect.
249 -- If the parameter is off the line segment, check distance to endpoints. (Not sure
250    how to proceed from here, it's different than the following.)
251    [Actually, you can use the following for all of it. You only know if you have
252     an intersection at the last step, which is OK.]
253 -- If the radius == distance, we have a tangent line.
254 -- If radius > distance, use Pythagorus to find the length on either side of the
255    normal to the spots where the hypotenuse (== radius' length) contact the line.
256 -- Use those points to find the parameter on the line segment; if they're not on
257    the line segment, no intersection.
258 */
259         double t = ParameterOfLineAndPoint(l->position, l->endpoint, c->position);
260 //small problem here: it clamps the result to the line segment. NOT what we want
261 //here! !!! FIX !!! [DONE]
262         Vector p = l->GetPointAtParameter(t);
263         double distance = Vector::Magnitude(c->position, p);
264
265         // If the center of the circle is farther from the line than the radius, fail.
266         if (distance > c->radius)
267                 return 0;
268
269         // Now we have to check for intersection points.
270         // Tangent case: (needs to return something)
271         if ((distance == c->radius) && (t >= 0.0) && (t <= 1.0))
272         {
273                 // Need to set tp & up to something... !!! FIX !!!
274                 if (tp)
275                         *tp = t;
276
277                 if (up)
278                         *up = Vector(c->position, p).Angle();
279
280                 return 1;
281         }
282
283         // The line intersects the circle in two points (possibly). Use Pythagorus
284         // to find them for testing.
285         double offset = sqrt((c->radius * c->radius) - (distance * distance));
286 //need to convert distance to paramter value... :-/
287 //t = position on line / length of line segment, so if we divide the offset by length,
288 //that should give us what we want.
289         double length = Vector::Magnitude(l->position, l->endpoint);
290         double t1 = t + (offset / length);
291         double t2 = t - (offset / length);
292
293 //need to find angles for the circle...
294         Vector cp1 = l->position + (Vector(l->position, l->endpoint) * (length * t1));
295         Vector cp2 = l->position + (Vector(l->position, l->endpoint) * (length * t2));
296         double a1 = Vector(c->position, cp1).Angle();
297         double a2 = Vector(c->position, cp2).Angle();
298
299 //instead of this, return a # which is the # of intersections. [DONE]
300         int intersections = 0;
301
302         // Now check for if the parameters are in range
303         if ((t1 >= 0) && (t1 <= 1.0))
304         {
305                 intersections++;
306         }
307
308         if ((t2 >= 0) && (t2 <= 1.0))
309         {
310                 intersections++;
311         }
312
313         return intersections;
314 #endif
315 }
316
317
318 // Finds the intersection(s) between a circle and a circle (if any)
319 // There can be 0, 1, or 2 intersections.
320 // Returns the angles of the points of intersection in tp thru wp, with the
321 // angles returned as c1, c2, c1, c2 (if applicable--in the 1 intersection case,
322 // only the first two angles are returned: c1, c2).
323 int Geometry::Intersects(Circle * c1, Circle * c2, double * tp/*= 0*/, double * up/*= 0*/, double * vp/*= 0*/, double * wp/*= 0*/, Point * p1/*= 0*/, Point * p2/*= 0*/)
324 {
325         // Get the distance between centers. If the distance plus the radius of the
326         // smaller circle is less than the radius of the larger circle, there is no
327         // intersection. If the distance is greater than the sum of the radii,
328         // there is no intersection. If the distance is equal to the sum of the
329         // radii, they are tangent and intersect at one point. Otherwise, they
330         // intersect at two points.
331         Vector centerLine(c1->position, c2->position);
332         double d = centerLine.Magnitude();
333 //printf("Circle #1: pos=<%lf, %lf>, r=%lf\n", c1->position.x, c1->position.y, c1->radius);
334 //printf("Circle #2: pos=<%lf, %lf>, r=%lf\n", c2->position.x, c2->position.y, c2->radius);
335 //printf("Distance between #1 & #2: %lf\n", d);
336
337         // Check to see if we actually have an intersection, and return failure if not
338         if ((fabs(c1->radius - c2->radius) > d) || ((c1->radius + c2->radius) < d))
339                 return 0;
340
341         // There are *two* tangent cases!
342         if (((c1->radius + c2->radius) == d) || (fabs(c1->radius - c2->radius) == d))
343         {
344                 // Need to return something in tp & up!! !!! FIX !!! [DONE]
345                 if (tp)
346                         *tp = centerLine.Angle();
347
348                 if (up)
349                         *up = centerLine.Angle() + PI;
350
351                 return 1;
352         }
353
354         // Find the distance from the center of c1 to the perpendicular chord
355         // (which contains the points of intersection)
356         double x = ((d * d) - (c2->radius * c2->radius) + (c1->radius * c1->radius))
357                 / (2.0 * d);
358         // Find the the length of the perpendicular chord
359 // Not needed...!
360         double a = sqrt((-d + c2->radius - c1->radius) * (-d - c2->radius + c1->radius) * (-d + c2->radius + c1->radius) * (d + c2->radius + c1->radius)) / d;
361
362         // Now, you can use pythagorus to find the length of the hypotenuse, but we
363         // already know that length, it's the radius! :-P
364         // What's needed is the angle of the center line and the radial line. Since
365         // there's two intersection points, there's also four angles (two for each
366         // circle)!
367         // We can use the arccos to find the angle using just the radius and the
368         // distance to the perpendicular chord...!
369         double angleC1 = acos(x / c1->radius);
370         double angleC2 = acos((d - x) / c2->radius);
371
372         if (tp)
373                 *tp = centerLine.Angle() - angleC1;
374
375         if (up)
376                 *up = (centerLine.Angle() + PI) - angleC2;
377
378         if (vp)
379                 *vp =  centerLine.Angle() + angleC1;
380
381         if (wp)
382                 *wp = (centerLine.Angle() + PI) + angleC2;
383
384         if (p1)
385                 *p1 = c1->position + (centerLine.Unit() * x) + (Vector::Normal(Vector(), centerLine) * (a / 2.0));
386
387         if (p2)
388                 *p2 = c1->position + (centerLine.Unit() * x) - (Vector::Normal(Vector(), centerLine) * (a / 2.0));
389
390         return 2;
391 }
392 #endif
393
394 // should we just do common trig solves, like AAS, ASA, SAS, SSA?
395 // Law of Cosines:
396 // c^2 = a^2 + b^2 -2ab*cos(C)
397 // Solving for C:
398 // cos(C) = (c^2 - a^2 - b^2) / -2ab = (a^2 + b^2 - c^2) / 2ab
399 // Law of Sines:
400 // a / sin A = b / sin B = c / sin C
401
402 // Solve the angles of the triangle given the sides. Angles returned are
403 // opposite of the given sides (so a1 consists of sides s2 & s3, and so on).
404 void Geometry::FindAnglesForSides(double s1, double s2, double s3, double * a1, double * a2, double * a3)
405 {
406         // Use law of cosines to find 1st angle
407         double cosine1 = ((s2 * s2) + (s3 * s3) - (s1 * s1)) / (2.0 * s2 * s3);
408
409         // Check for a valid triangle
410         if ((cosine1 < -1.0) || (cosine1 > 1.0))
411                 return;
412
413         double angle1 = acos(cosine1);
414
415         // Use law of sines to find 2nd & 3rd angles
416 // sin A / a = sin B / b
417 // sin B = (sin A / a) * b
418         double angle2 = asin(s2 * (sin(angle1) / s1));
419         double angle3 = asin(s3 * (sin(angle1) / s1));
420
421         if (a1)
422                 *a1 = angle1;
423
424         if (a2)
425                 *a2 = angle2;
426
427         if (a3)
428                 *a3 = angle3;
429 }
430