]> Shamusworld >> Repos - architektonas/blob - src/geometry.cpp
41928aa629b7b253798ad19a1a0b73c3b56c19e5
[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
22
23 Point Geometry::IntersectionOfLineAndLine(Point p1, Point p2, Point p3, Point p4)
24 {
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)
30
31         double d = ((p1.x - p2.x) * (p3.y - p4.y)) - ((p1.y - p2.y) * (p3.x - p4.x));
32
33         // Check for parallel lines, and return sentinel if so
34         if (d == 0)
35                 return Point(0, 0, -1);
36
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)));
41
42         return Point(px / d, py / d, 0);
43 }
44
45
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)
50 {
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.
56
57         Vector lineSegment = head - tail;
58         double magnitude = lineSegment.Magnitude();
59         Vector pointSegment = point - tail;
60         double t = lineSegment.Dot(pointSegment) / (magnitude * magnitude);
61         return t;
62 }
63
64
65 Point Geometry::MirrorPointAroundLine(Point point, Point tail, Point head)
66 {
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;
71
72         // Get the point normal to point to the line passed in
73         Point normalOnLine = tail + v;
74
75         // Make our mirrored vector (head - tail)
76         Vector mirror = -(point - normalOnLine);
77
78         // Find the mirrored point
79         Point mirroredPoint = normalOnLine + mirror;
80
81         return mirroredPoint;
82 }
83
84
85 Point Geometry::RotatePointAroundPoint(Point point, Point rotationPoint, double angle)
86 {
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));
90
91         return Vector(rotationPoint.x + px, rotationPoint.y + py, 0);
92 }
93
94
95 double Geometry::Determinant(Point p1, Point p2)
96 {
97         return (p1.x * p2.y) - (p2.x * p1.y);
98 }
99
100
101 /*
102 Intersecting line segments:
103 An easier way:
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
111
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?))
113
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?))
115
116 So check if the above two numbers are both >=0 and <=1.
117 */
118
119
120 #if 0
121 // Finds the intesection between two objects (if any)
122 bool Geometry::Intersects(Object * obj1, Object * obj2, double * t, double * s)
123 {
124 }
125 #endif
126
127 // Finds the intersection between two lines (if any)
128 int Geometry::Intersects(Line * l1, Line * l2, double * tp/*= 0*/, double * up/*= 0*/)
129 {
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);
136         double t, u;
137
138         if (rxs == 0)
139         {
140                 double qpxr = (v1.x * r.y) - (r.x * v1.y);
141
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));
146
147                 // Lines are parallel, so no intersection...
148                 if (qpxr != 0)
149                         return 0;
150
151 #if 0
152 //this works IFF the vectors are pointing in the same direction. everything else
153 //is fucked!
154                 // If (q - p) . r == r . r, t = 1, u = 0
155                 if (v1.Dot(r) == r.Dot(r))
156                         t = 1.0, u = 0;
157                 // If (p - q) . s == s . s, t = 0, u = 1
158                 else if (v2.Dot(s) == s.Dot(s))
159                         t = 0, u = 1.0;
160                 else
161                         return 0;
162 #else
163                 // Check to see which endpoints are connected... Four possibilities:
164                 if (l1->position == l2->position)
165                         t = 0, u = 0;
166                 else if (l1->position == l2->endpoint)
167                         t = 0, u = 1.0;
168                 else if (l1->endpoint == l2->position)
169                         t = 1.0, u = 0;
170                 else if (l1->endpoint == l2->endpoint)
171                         t = 1.0, u = 1.0;
172                 else
173                         return 0;
174 #endif
175         }
176         else
177         {
178                 t = ((v1.x * s.y) - (s.x * v1.y)) / rxs;
179                 u = ((v1.x * r.y) - (r.x * v1.y)) / rxs;
180         }
181 /*
182 Now there are five cases (NOTE: only valid if vectors face the same way!):
183
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.
185
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.
187
188 3. If r × s = 0 and (q − p) × r ≠ 0, then the two lines are parallel and non-intersecting.
189
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.
191
192 5. Otherwise, the two line segments are not parallel but do not intersect.
193 */
194         // Return parameter values, if user passed in valid pointers
195         if (tp)
196                 *tp = t;
197
198         if (up)
199                 *up = u;
200
201         // If the parameters are in range, we have overlap!
202         if ((t >= 0) && (t <= 1.0) && (u >= 0) && (u <= 1.0))
203                 return 1;
204
205         return 0;
206 }
207
208
209 // Finds the intersection between two lines (if any)
210 int Geometry::Intersects(Line * l1, Dimension * d1, double * tp/*= 0*/, double * up/*= 0*/)
211 {
212         Line l2(d1->position, d1->endpoint);
213         return Intersects(l1, &l2, tp, up);
214 }
215
216
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*/)
219 {
220 #if 0
221         Vector center = c->position;
222         Vector v1 = l->position - center;
223         Vector v2 = l->endpoint - center;
224         Vector d = v2 - v1;
225         double dr = d.Magnitude();
226         double determinant = (v1.x * v2.y) - (v1.y * v2.x);
227
228         double discriminant = ((c->radius * c->radius) * (dr * dr)) - (determinant * determinant);
229
230         if (discriminant < 0)
231                 return false;
232
233         
234
235         return true;
236 #else
237 /*
238 I'm thinking a better approach to this might be as follows:
239
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.
251 */
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);
257
258         // If the center of the circle is farther from the line than the radius, fail.
259         if (distance > c->radius)
260                 return 0;
261
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))
265                 return 1;
266
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);
276
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();
282
283 //instead of this, return a # which is the # of intersections. [DONE]
284         int intersections = 0;
285
286         // Now check for if the parameters are in range
287         if ((t1 >= 0) && (t1 <= 1.0))
288         {
289                 intersections++;
290         }
291
292         if ((t2 >= 0) && (t2 <= 1.0))
293         {
294                 intersections++;
295         }
296
297         return intersections;
298 #endif
299 }
300
301