comparison doodle/tk/geometry.d @ 66:43cc2135ced0

Some code cleanups
author "David Bryant <bagnose@gmail.com>"
date Thu, 12 Aug 2010 22:43:42 +0930
parents 6c3993f4c3eb
children 31d10176415d
comparison
equal deleted inserted replaced
65:b4676efb884a 66:43cc2135ced0
138 static this() { 138 static this() {
139 DEFAULT = Rectangle(Point.DEFAULT, Vector.DEFAULT); 139 DEFAULT = Rectangle(Point.DEFAULT, Vector.DEFAULT);
140 } 140 }
141 141
142 /* 142 /*
143 static Rectangle from_arbitrary_corners(in Point corner1, in Point corner) { 143 static Rectangle from_arbitrary_corners(in Point corner1, in Point corner) {
144 } 144 }
145 */ 145 */
146 146
147 this(in Point position, in Vector size) { 147 this(in Point position, in Vector size) {
148 this(position.x, position.y, size.x, size.y); 148 this(position.x, position.y, size.x, size.y);
149 } 149 }
150 150
234 234
235 Rectangle move(in Rectangle r, in Vector displacement) { 235 Rectangle move(in Rectangle r, in Vector displacement) {
236 return Rectangle(r.position + displacement, r.size); 236 return Rectangle(r.position + displacement, r.size);
237 } 237 }
238 238
239 Rectangle reposition(in Rectangle r, in Point new_position) { 239 Rectangle reposition(in Rectangle r, in Point newPosition) {
240 return Rectangle(new_position, r.size); 240 return Rectangle(newPosition, r.size);
241 } 241 }
242 242
243 Rectangle resize(in Rectangle r, in Vector new_size) { 243 Rectangle resize(in Rectangle r, in Vector new_size) {
244 return Rectangle(r.position, new_size); 244 return Rectangle(r.position, new_size);
245 } 245 }
275 // 275 //
276 // The function returns false if the lines are parallel or nearly so. 276 // The function returns false if the lines are parallel or nearly so.
277 // 277 //
278 // Influenced by http://ozviz.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/ 278 // Influenced by http://ozviz.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
279 bool computeIntersection(in Point pa1, in Point pa2, out double ua, 279 bool computeIntersection(in Point pa1, in Point pa2, out double ua,
280 in Point pb1, in Point pb2, out double ub) { 280 in Point pb1, in Point pb2, out double ub) {
281 double den = (pb2.y - pb1.y) * (pa2.x - pa1.x) - (pb2.x - pb1.x) * (pa2.y - pa1.y); 281 double den = (pb2.y - pb1.y) * (pa2.x - pa1.x) - (pb2.x - pb1.x) * (pa2.y - pa1.y);
282 282
283 if (abs(den) < 1e-9) { // TODO consolidate constants used for numerical stability 283 if (abs(den) < 1e-9) { // TODO consolidate constants used for numerical stability
284 // Lines are parallel or nearly so 284 // Lines are parallel or nearly so
285 warning("Warning, parallel lines!"); 285 warning("Warning, parallel lines!");
286 return false; 286 return false;
287 } 287 }
288 else { 288 else {
289 // It will be safe to divide by den 289 // It will be safe to divide by den
290 double num_a = (pb2.x - pb1.x) * (pa1.y - pb1.y) - (pb2.y - pb1.y) * (pa1.x - pb1.x); 290 double numA = (pb2.x - pb1.x) * (pa1.y - pb1.y) - (pb2.y - pb1.y) * (pa1.x - pb1.x);
291 double num_b = (pa2.x - pa1.x) * (pa1.y - pb1.y) - (pa2.y - pa1.y) * (pa1.x - pb1.x); 291 double numB = (pa2.x - pa1.x) * (pa1.y - pb1.y) - (pa2.y - pa1.y) * (pa1.x - pb1.x);
292 292
293 ua = num_a / den; 293 ua = numA / den;
294 ub = num_b / den; 294 ub = numB / den;
295 295
296 return true; 296 return true;
297 } 297 }
298 } 298 }
299 299
300 /+ 300 /+
301 double compute_angle(in Point p1, in Point p2) { 301 double compute_angle(in Point p1, in Point p2) {
302 } 302 }
303 +/ 303 +/
304 } 304 }
305 305
306 // 306 //
307 // A line (notionally infinitely extending in both directions) in 2D space. 307 // A line (notionally infinitely extending in both directions) in 2D space.