/// <summary>Finds the lowest distance of point P to a line segment defined by points A and B</summary> public static float distance_from_point_to_segment(crds2 a, crds2 b, crds2 p) { float l2 = a.dist_squared(b); if (l2 < 0.000001f) return a.dist(p); // case : a == b float t = crds2.dot(p-a, b-a) / l2; if (t < 0f) return a.dist(p); if (t > 1f) return b.dist(p); crds2 proj = a.lerp(b, t); return proj.dist(p); }
/// <summary>same as segment-segment int. but points only define the line, don't limit it</summary> public static bool line_line_intersection(crds2 A, crds2 B, crds2 C, crds2 D, out crds2 intersection_point) { intersection_point = crds2.zero; var z = (D.y - C.y) * (B.x - A.x) - (D.x - C.x) * (B.y - A.y); if (z.approximately(0f)) return false; var U1 = ((D.x - C.x) * (A.y - C.y) - (D.y - C.y) * (A.x - C.x)) / z; var U2 = ((B.x - A.x) * (A.y - C.y) - (B.y - A.y) * (A.x - C.x)) / z; intersection_point = A.lerp(B, U1); return true; }
/// <summary> Returns the point of intersection of two segments, where first segment is defined by A and B, and 2nd segment by C and D. </summary> /// <param name="A">Point A</param><param name="B">Point B</param><param name="C">Point C</param><param name="D">Point D</param><param name="intersection_point">Intersection point, if any</param> /// <returns>True if there is an intersection, and if so, fills intersection_point with the exact location</returns> public static bool segment_segment_intersection(crds2 A, crds2 B, crds2 C, crds2 D, out crds2 intersection_point) { intersection_point = crds2.zero; var z = (D.y - C.y) * (B.x - A.x) - (D.x - C.x) * (B.y - A.y); if (z.approximately(0f)) return false; var U1 = ((D.x - C.x) * (A.y - C.y) - (D.y - C.y) * (A.x - C.x)) / z; var U2 = ((B.x - A.x) * (A.y - C.y) - (B.y - A.y) * (A.x - C.x)) / z; if ((U1 >= 0f && U1 <= 1f) && (U2 >= 0f && U2 <= 1f)) { intersection_point = A.lerp(B, U1); return true; } return false; }
public static void set_extremes(crds2 upper_left, crds2 lower_right, float min_zoom, float max_zoom) { restrictions_low.x = upper_left.x; restrictions_low.y = upper_left.y; restrictions_high.x = lower_right.x; restrictions_high.y = lower_right.y; restrictions_low.w = min_zoom * ZOOM_FACTOR; restrictions_high.w = max_zoom * ZOOM_FACTOR; set_target(upper_left.lerp(lower_right, 0.5f)); }