]> Shamusworld >> Repos - architektonas/blob - src/geometry.cpp
Fixed incorrect rotation in Arc object.
[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 //      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));
91
92         return Vector(rotationPoint.x + px, rotationPoint.y + py, 0);
93 }
94
95
96 double Geometry::Determinant(Point p1, Point p2)
97 {
98         return (p1.x * p2.y) - (p2.x * p1.y);
99 }
100
101
102 /*
103 Intersecting line segments:
104 An easier way:
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
112
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?))
114
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?))
116
117 So check if the above two numbers are both >=0 and <=1.
118 */
119
120
121 #if 0
122 // Finds the intesection between two objects (if any)
123 bool Geometry::Intersects(Object * obj1, Object * obj2, double * t, double * s)
124 {
125 }
126 #endif
127
128 // Finds the intersection between two lines (if any)
129 int Geometry::Intersects(Line * l1, Line * l2, double * tp/*= 0*/, double * up/*= 0*/)
130 {
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);
137         double t, u;
138
139         if (rxs == 0)
140         {
141                 double qpxr = (v1.x * r.y) - (r.x * v1.y);
142
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));
147
148                 // Lines are parallel, so no intersection...
149                 if (qpxr != 0)
150                         return 0;
151
152 #if 0
153 //this works IFF the vectors are pointing in the same direction. everything else
154 //is fucked!
155                 // If (q - p) . r == r . r, t = 1, u = 0
156                 if (v1.Dot(r) == r.Dot(r))
157                         t = 1.0, u = 0;
158                 // If (p - q) . s == s . s, t = 0, u = 1
159                 else if (v2.Dot(s) == s.Dot(s))
160                         t = 0, u = 1.0;
161                 else
162                         return 0;
163 #else
164                 // Check to see which endpoints are connected... Four possibilities:
165                 if (l1->position == l2->position)
166                         t = 0, u = 0;
167                 else if (l1->position == l2->endpoint)
168                         t = 0, u = 1.0;
169                 else if (l1->endpoint == l2->position)
170                         t = 1.0, u = 0;
171                 else if (l1->endpoint == l2->endpoint)
172                         t = 1.0, u = 1.0;
173                 else
174                         return 0;
175 #endif
176         }
177         else
178         {
179                 t = ((v1.x * s.y) - (s.x * v1.y)) / rxs;
180                 u = ((v1.x * r.y) - (r.x * v1.y)) / rxs;
181         }
182 /*
183 Now there are five cases (NOTE: only valid if vectors face the same way!):
184
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.
186
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.
188
189 3. If r × s = 0 and (q − p) × r ≠ 0, then the two lines are parallel and non-intersecting.
190
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.
192
193 5. Otherwise, the two line segments are not parallel but do not intersect.
194 */
195         // Return parameter values, if user passed in valid pointers
196         if (tp)
197                 *tp = t;
198
199         if (up)
200                 *up = u;
201
202         // If the parameters are in range, we have overlap!
203         if ((t >= 0) && (t <= 1.0) && (u >= 0) && (u <= 1.0))
204                 return 1;
205
206         return 0;
207 }
208
209
210 // Finds the intersection between two lines (if any)
211 int Geometry::Intersects(Line * l1, Dimension * d1, double * tp/*= 0*/, double * up/*= 0*/)
212 {
213         Line l2(d1->position, d1->endpoint);
214         return Intersects(l1, &l2, tp, up);
215 }
216
217
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*/)
220 {
221 #if 0
222         Vector center = c->position;
223         Vector v1 = l->position - center;
224         Vector v2 = l->endpoint - center;
225         Vector d = v2 - v1;
226         double dr = d.Magnitude();
227         double determinant = (v1.x * v2.y) - (v1.y * v2.x);
228
229         double discriminant = ((c->radius * c->radius) * (dr * dr)) - (determinant * determinant);
230
231         if (discriminant < 0)
232                 return false;
233
234         
235
236         return true;
237 #else
238 /*
239 I'm thinking a better approach to this might be as follows:
240
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.
252 */
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);
258
259         // If the center of the circle is farther from the line than the radius, fail.
260         if (distance > c->radius)
261                 return 0;
262
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))
266                 return 1;
267
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);
277
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();
283
284 //instead of this, return a # which is the # of intersections. [DONE]
285         int intersections = 0;
286
287         // Now check for if the parameters are in range
288         if ((t1 >= 0) && (t1 <= 1.0))
289         {
290                 intersections++;
291         }
292
293         if ((t2 >= 0) && (t2 <= 1.0))
294         {
295                 intersections++;
296         }
297
298         return intersections;
299 #endif
300 }
301
302