1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | using System; using System.Drawing; public class Test { public static void Main() { Console.WriteLine(LinesIntersect("0,0-2,8:8,0-0,20")); // No intersect Console.WriteLine(LinesIntersect("0,10-2,0:10,0-0,5")); // Intersect Console.WriteLine(LinesIntersect("0,0-0,10:2,0-2,10")); // Parallel, vertical Console.WriteLine(LinesIntersect("0,0-5,5:2,0-7,5")); // Parallel, diagonal Console.WriteLine(LinesIntersect("0,0-5,5:2,2-7,7")); // Collinear, overlap Console.WriteLine(LinesIntersect("0,0-5,5:7,7-10,10")); // Collinear, no overlap } // Utility wrapper around LinesIntersect, expecting a string in the form: // A-B:C-D // Where A, B, C and D are cartesian coordinates in the form: // XX.XX,YY.YY static bool LinesIntersect(string s) { PointF A, B, C, D; string[] split = s.Split(',', '-', ':'); A = new PointF(float.Parse(split[0]), float.Parse(split[1])); B = new PointF(float.Parse(split[2]), float.Parse(split[3])); C = new PointF(float.Parse(split[4]), float.Parse(split[5])); D = new PointF(float.Parse(split[6]), float.Parse(split[7])); return LinesIntersect(A, B, C, D); } // Determines if the lines AB and CD intersect. static bool LinesIntersect(PointF A, PointF B, PointF C, PointF D) { PointF CmP = new PointF(C.X - A.X, C.Y - A.Y); PointF r = new PointF(B.X - A.X, B.Y - A.Y); PointF s = new PointF(D.X - C.X, D.Y - C.Y); float CmPxr = CmP.X * r.Y - CmP.Y * r.X; float CmPxs = CmP.X * s.Y - CmP.Y * s.X; float rxs = r.X * s.Y - r.Y * s.X; if (CmPxr == 0f) { // Lines are collinear, and so intersect if they have any overlap return ((C.X - A.X < 0f) != (C.X - B.X < 0f)) || ((C.Y - A.Y < 0f) != (C.Y - B.Y < 0f)); } if (rxs == 0f) return false; // Lines are parallel. float rxsr = 1f / rxs; float t = CmPxs * rxsr; float u = CmPxr * rxsr; return (t >= 0f) && (t <= 1f) && (u >= 0f) && (u <= 1f); } } |
-
upload with new input
-
result: Success time: 0.03s memory: 33792 kB returned value: 0
3
False True False False True False
-
result: Success time: 0.03s memory: 33832 kB returned value: 0
3
False True False False True False
-
result: Success time: 0.03s memory: 33920 kB returned value: 0
fsaga
False True False False True False
-
result: Success time: 0.03s memory: 33832 kB returned value: 0
3
False True False False True False
-
result: Success time: 0.03s memory: 33808 kB returned value: 0
False True False False True False


