Răsfoiți Sursa

added bounds.distance(point)

Nicolas Cannasse 4 ani în urmă
părinte
comite
ccc1f33b46
1 a modificat fișierele cu 16 adăugiri și 0 ștergeri
  1. 16 0
      h2d/col/Bounds.hx

+ 16 - 0
h2d/col/Bounds.hx

@@ -66,6 +66,22 @@ class Bounds {
 		return p.x >= xMin && p.x < xMax && p.y >= yMin && p.y < yMax;
 	}
 
+	/**
+	 * Same as distance but does not perform sqrt
+	 */
+	 public inline function distanceSq( p : Point ) {
+		var dx = p.x < xMin ? xMin - p.x : p.x > xMax ? p.x - xMax : 0.;
+		var dy = p.y < yMin ? yMin - p.y : p.y > yMax ? p.y - yMax : 0.;
+		return dx * dx + dy * dy;
+	}
+
+	/**
+	 * Returns the distance betwen the point and the bounds. Or 0 if the point is inside the bounds.
+	 */
+	public inline function distance( p : Point ) {
+		return Math.sqrt(distanceSq(p));
+	}
+
 	/**
 		Adds Bounds `b` to the Bounds, expanding min/max when necessary.
 	**/