diff doodle/tk/geometry.d @ 41:f2e4e1d29b98

Bah
author daveb
date Tue, 01 Jun 2010 17:21:01 +0930
parents 1f97022e5c6d
children 1b4c9ba58673
line wrap: on
line diff
--- a/doodle/tk/geometry.d	Mon Apr 12 14:01:54 2010 +0930
+++ b/doodle/tk/geometry.d	Tue Jun 01 17:21:01 2010 +0930
@@ -4,6 +4,7 @@
     import std.stdio;
     import std.math;
     import doodle.tk.misc;
+    import doodle.core.logging;
 }
 
 // In doodle x and y increase right/east and up/north respectively.
@@ -30,6 +31,8 @@
     }
 
     this(in double x, in double y) {
+        assert(!isnan(x));
+        assert(!isnan(y));
         _x = x;
         _y = y;
     }
@@ -78,6 +81,8 @@
     }
 
     this(in double x, in double y) {
+        assert(!isnan(x));
+        assert(!isnan(y));
         _x = x;
         _y = y;
     }
@@ -95,10 +100,12 @@
     }
 
     Vector opMul_r(in double d) const {
+        assert(!isnan(d));
         return Vector(d * _x, d * _y);
     }
 
     Vector opDiv(in double d) const {
+        assert(!isnan(d));
         return Vector(_x / d, _y / d);
     }
 
@@ -164,7 +171,7 @@
 
     Point max_corner() const { return _position + _size; }
 
-    bool valid() const { return _size.x > 0.0 & _size.y > 0.0; }
+    bool valid() const { return _size.x > 0.0 && _size.y > 0.0; }
 
     bool invalid() const { return !valid(); }
 
@@ -245,6 +252,10 @@
     return Rectangle(new_position, r.size);
 }
 
+Rectangle resize(in Rectangle r, in Vector new_size) {
+    return Rectangle(r.position, new_size);
+}
+
 // Operations about the bottom left corner
 
 Rectangle expand(in Rectangle r, in Vector expand_amount) {
@@ -259,19 +270,23 @@
 
 Rectangle feather(in Rectangle r, double amount) {
     assert(amount >= 0.0);
+    assert(!isnan(amount));
     return Rectangle(Point(r.position.x - amount, r.position.y - amount),
                      Vector(r.size.x + 2.0 * amount, r.size.y + 2.0 * amount));
 }
 
-Rectangle resize(in Rectangle r, in Vector new_size) {
-    return Rectangle(r.position, new_size);
-}
-
 private {
-    // This is a commmon building block for intersection of lines and segments
-    // Two lines "a" and "b" are defined by two points each .
-    // "ua" and "ub" define the intersection as a fraction along
-    // the two respective line segments (FIXME this comment sucks)
+    // This function computes the intersection of two lines.
+    // The lines are defined by a start point and an end point, however they
+    // notionally extend infinitely in each direction.
+    // The out parameters specify the fraction along the line-segment at which
+    // intersection occurred.
+    //
+    // This is a commmon building block for computing intersection between lines, segments,
+    // rectangles, etc.
+    //
+    // The function returns false if the lines are parallel or nearly so.
+    //
     // Influenced by http://ozviz.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
     bool compute_intersection(in Point pa1, in Point pa2, out double ua,
                               in Point pb1, in Point pb2, out double ub) {
@@ -279,7 +294,7 @@
 
         if (abs(den) < 1e-9) {          // TODO consolidate constants used for numerical stability
             // Lines are parallel or nearly so
-            writefln("Warning, parallel lines!");
+            warning("Warning, parallel lines!");
             return false;
         }
         else {
@@ -293,6 +308,11 @@
             return true;
         }
     }
+
+    /+
+    double compute_angle(in Point p1, in Point p2) {
+    }
+    +/
 }
 
 //