├── AllGeometry.cs ├── DistanceToLine.cs └── PointInTriangle.cs /AllGeometry.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public static class Geometry 4 | { 5 | 6 | /// Distance of point C from the line passing through points A and B. 7 | public static double DistanceFromPointToLine(Point a, Point b, Point c) 8 | { 9 | double s1 = -b.y + a.y; 10 | double s2 = b.x - a.x; 11 | return Math.Abs((c.x - a.x) * s1 + (c.y - a.y) * s2) / Math.Sqrt(s1*s1 + s2*s2); 12 | } 13 | 14 | /// Psuedo distance of point C from the line passing through points A and B. 15 | /// This is not the actual distance value, but a further point will always have a higher value than a nearer point. 16 | /// Faster than calculating the actual distance. Useful for sorting. 17 | public static double PseudoDistanceFromPointToLine(Point a, Point b, Point c) 18 | { 19 | return Math.Abs((c.x - a.x) * (-b.y + a.y) + (c.y - a.y) * (b.x - a.x)); 20 | } 21 | 22 | /// Determines which side point C lies of the line passing through points A and B 23 | /// Returns +1 if on one side of line, -1 if on the other, and 0 if it lies exactly on the line 24 | public static int SideOfLine(Point a, Point b, Point c) 25 | { 26 | return Math.Sign((c.x - a.x) * (-b.y + a.y) + (c.y - a.y) * (b.x - a.x)); 27 | } 28 | 29 | /// Determines whether point P is inside the triangle ABC 30 | public static bool PointInTriangle(Point a, Point b, Point c, Point p) 31 | { 32 | double s1 = c.y - a.y; 33 | double s2 = c.x - a.x; 34 | double s3 = b.y - a.y; 35 | double s4 = p.y - a.y; 36 | 37 | double w1 = (a.x * s1 + s4 * s2 - p.x * s1) / (s3 * s2 - (b.x-a.x) * s1); 38 | double w2 = (s4- w1 * s3) / s1; 39 | return w1 >= 0 && w2 >= 0 && (w1 + w2) <= 1; 40 | } 41 | } 42 | 43 | public struct Point 44 | { 45 | public readonly double x; 46 | public readonly double y; 47 | 48 | public Point(double x, double y) 49 | { 50 | this.x = x; 51 | this.y = y; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /DistanceToLine.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public static class Geometry 4 | { 5 | 6 | // Explanation of below methods: 7 | // youtu.be/KHuI9bXZS74?list=PLFt_AvWsXl0cD2LPxcjxVjWTQLxJqKpgZ 8 | 9 | 10 | /// Distance of point C from the line passing through points A and B. 11 | public static double DistanceFromPointToLine(Point a, Point b, Point c) 12 | { 13 | double s1 = -b.y + a.y; 14 | double s2 = b.x - a.x; 15 | return Math.Abs((c.x - a.x) * s1 + (c.y - a.y) * s2) / Math.Sqrt(s1*s1 + s2*s2); 16 | } 17 | 18 | /// Psuedo distance of point C from the line passing through points A and B. 19 | /// This is not the actual distance value, but a further point will always have a higher value than a nearer point. 20 | /// Faster than calculating the actual distance. Useful for sorting. 21 | public static double PseudoDistanceFromPointToLine(Point a, Point b, Point c) 22 | { 23 | return Math.Abs((c.x - a.x) * (-b.y + a.y) + (c.y - a.y) * (b.x - a.x)); 24 | } 25 | 26 | /// Determines which side point C lies of the line passing through points A and B 27 | /// Returns +1 if on one side of line, -1 if on the other, and 0 if it lies exactly on the line 28 | public static int SideOfLine(Point a, Point b, Point c) 29 | { 30 | return Math.Sign((c.x - a.x) * (-b.y + a.y) + (c.y - a.y) * (b.x - a.x)); 31 | } 32 | } 33 | 34 | public struct Point 35 | { 36 | public readonly double x; 37 | public readonly double y; 38 | 39 | public Point(double x, double y) 40 | { 41 | this.x = x; 42 | this.y = y; 43 | } 44 | } -------------------------------------------------------------------------------- /PointInTriangle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public static class Geometry 4 | { 5 | 6 | // Explanation of PointInTriangle method: 7 | // youtu.be/HYAgJN3x4GA?list=PLFt_AvWsXl0cD2LPxcjxVjWTQLxJqKpgZ 8 | 9 | /// Determines whether point P is inside the triangle ABC 10 | public static bool PointInTriangle(Point A, Point B, Point C, Point P) 11 | { 12 | double s1 = C.y - A.y; 13 | double s2 = C.x - A.x; 14 | double s3 = B.y - A.y; 15 | double s4 = P.y - A.y; 16 | 17 | double w1 = (A.x * s1 + s4 * s2 - P.x * s1) / (s3 * s2 - (B.x-A.x) * s1); 18 | double w2 = (s4- w1 * s3) / s1; 19 | return w1 >= 0 && w2 >= 0 && (w1 + w2) <= 1; 20 | } 21 | 22 | } 23 | 24 | public struct Point 25 | { 26 | public readonly double x; 27 | public readonly double y; 28 | 29 | public Point(double x, double y) 30 | { 31 | this.x = x; 32 | this.y = y; 33 | } 34 | } 35 | --------------------------------------------------------------------------------