//rotate this point about another point public void rotate(double angle, Point2D point) { x -= point.x; y -= point.y; double ox = x; double oy = y; x = Math.Cos(angle) * ox - Math.Sin(angle) * oy; y = Math.Sin(angle) * ox + Math.Cos(angle) * oy; x += point.x; y += point.y; }
public double manhattenDistance(Point2D point) { double dx = Math.Abs(point.x - x); double dy = Math.Abs(point.y - y); return dx + dy; }
//what is the squared dist to another point public double distance_sq(Point2D point) { double dx = point.x - x; double dy = point.y - y; return dx * dx + dy * dy; }
//what is the distance from this line to a point public double distance(Point2D n) { return Math.Sqrt(distance_sq(n)); }
public Point2D(Point2D another) { x = another.x; y = another.y; }
//calculate the point of intersection between two line segments public Point2D intersection(Line2D L, out bool found) { Point2D pt = new Point2D(0.0, 0.0); Point2D A = p1; Point2D B = p2; Point2D C = L.p1; Point2D D = L.p2; double rTop = (A.y - C.y) * (D.x - C.x) - (A.x - C.x) * (D.y - C.y); double rBot = (B.x - A.x) * (D.y - C.y) - (B.y - A.y) * (D.x - C.x); double sTop = (A.y - C.y) * (B.x - A.x) - (A.x - C.x) * (B.y - A.y); double sBot = (B.x - A.x) * (D.y - C.y) - (B.y - A.y) * (D.x - C.x); if ((rBot == 0 || sBot == 0)) { found = false; return pt; } double r = rTop / rBot; double s = sTop / sBot; if ((r > 0) && (r < 1) && (s > 0) && (s < 1)) { pt.x = A.x + r * (B.x - A.x); pt.y = A.y + r * (B.y - A.y); found = true; return pt; } else { found = false; return pt; } }
//what is the squared distance from this line to a point public double distance_sq(Point2D n) { double utop = (n.x - p1.x) * (p2.x - p1.x) + (n.y - p1.y) * (p2.y - p1.y); double ubot = p1.distance_sq(p2); double u = utop / ubot; if (u < 0 || u > 1) { double d1 = p1.distance_sq(n); double d2 = p2.distance_sq(n); if (d1 < d2) return d1; return d2; } Point2D p = new Point2D(0.0, 0.0); p.x = p1.x + u * (p2.x - p1.x); p.y = p1.y + u * (p2.y - p1.y); return p.distance_sq(n); }
public Circle2D(Point2D a, double rad) { radius = rad; p = a; }
public Circle2D(Circle2D other) { p = other.p; radius = other.radius; }
public Line2D(Line2D other) { p1 = other.p1; p2 = other.p2; }
public Line2D(Point2D a, Point2D b) { p1 = a; p2 = b; }
//what is the distance to another point public double distance(Point2D point) { return Math.Sqrt(distance_sq(point)); }