diff --git a/mono/Contours/MainForm.cs b/mono/Contours/MainForm.cs index 7d52e58..04568b2 100644 --- a/mono/Contours/MainForm.cs +++ b/mono/Contours/MainForm.cs @@ -11,6 +11,9 @@ namespace Contours { ComboBox cbViews; public MainForm() { + Width = 800; + Height = 600; + bTest = new Button(); bTest.Left = 20; bTest.Top = 20; @@ -124,6 +127,8 @@ namespace Contours { } } + System.Drawing.Drawing2D.Matrix m = e.Graphics.Transform; + e.Graphics.TranslateTransform(50, 100); if (testContours != null) { foreach(List> group in testContours) { Color color = Color.Black; @@ -133,6 +138,7 @@ namespace Contours { } } } + e.Graphics.Transform = m; } } } diff --git a/mono/Contours/Test.cs b/mono/Contours/Test.cs index cb00ac3..48e458c 100644 --- a/mono/Contours/Test.cs +++ b/mono/Contours/Test.cs @@ -15,26 +15,68 @@ namespace Contours { public static readonly List tests = new List(); - void check(string name, List>> calculatedContours) { - output.Add(name, calculatedContours); + void check(string name, Shape shape) { + if (!input.ContainsKey(name)) return; + List>> contours = null; + try { + contours = shape.getContours(); + } catch(System.Exception) { } + output.Add(name, contours); results.Add(name, compareContours(input[name], output[name])); if (!results[name]) result = false; } + Shape tryCreateShape(List>> contours) { + try { + Shape shape = new Shape(); + shape.setContours(contours); + return shape; + } catch (System.Exception) { + return null; + } + } + + delegate Shape CombineShapesFunc(Shape a, Shape b); + Shape tryCombineShapes(CombineShapesFunc func, List>> a, List>> b) { + try { + Shape sa = new Shape(); + Shape sb = new Shape(); + sa.setContours(a); + sb.setContours(b); + return func(sa, sb); + } catch (System.Exception) { + return null; + } + } + public bool run() { result = true; - Shape a = new Shape(); - Shape b = new Shape(); - a.setContours(input["a"]); - b.setContours(input["b"]); - check("add", Shape.add(a, b).getContours()); - check("subtract", Shape.subtract(a, b).getContours()); - check("xor", Shape.xor(a, b).getContours()); - check("intersection", Shape.intersection(a, b).getContours()); + + List>> a = null; + List>> b = null; + + if (input.ContainsKey("dirtyA")) a = input["dirtyA"]; else + if (input.ContainsKey("a")) a = input["a"]; + if (input.ContainsKey("dirtyB")) b = input["dirtyB"]; else + if (input.ContainsKey("b")) b = input["b"]; + + if (a != null) + check("a", tryCreateShape(a)); + if (b != null) + check("b", tryCreateShape(b)); + + if (a != null && b != null) { + check("add", tryCombineShapes(Shape.add, a, b)); + check("subtract", tryCombineShapes(Shape.subtract, a, b)); + check("xor", tryCombineShapes(Shape.xor, a, b)); + check("intersection", tryCombineShapes(Shape.intersection, a, b)); + } + return result; } public static bool compareContours(List a, List b) { + if (a == null || b == null) return false; if (a.Count == b.Count) { for(int offset = 0; offset < a.Count; ++offset) { bool equal = true; @@ -48,6 +90,7 @@ namespace Contours { } public static bool compareContours(List> a, List> b) { + if (a == null || b == null) return false; if (a.Count != b.Count) return false; if (a.Count == 0) return true; if (!compareContours(a[0], b[0])) return false; @@ -64,6 +107,7 @@ namespace Contours { } public static bool compareContours(List>> a, List>> b) { + if (a == null || b == null) return false; if (a.Count != b.Count) return false; bool[] compared = new bool[a.Count]; for(int i = 0; i < a.Count; ++i) { @@ -173,8 +217,9 @@ namespace Contours { string loadName() { string name = ""; loadKey("("); - while(tryLoadKey(")") == null) + while(text[position] != ')') name += text[position++]; + ++position; return name.Trim(); } diff --git a/mono/Contours/tests.txt b/mono/Contours/tests.txt index 86d1dce..57c0167 100644 --- a/mono/Contours/tests.txt +++ b/mono/Contours/tests.txt @@ -10,8 +10,8 @@ subtract: ((((0, 0), (200, 0), (200, 100), (100, 100), (100, 200), (0, 200)))) xor: - (( ((0, 0), (200, 0), (200, 100), (100, 100), (100, 200), (0, 200)), - ((200, 200), (200, 100), (300, 100), (300, 300), (100, 300), (100, 200)) )) + ( (((0, 0), (200, 0), (200, 100), (100, 100), (100, 200), (0, 200))), + (((200, 200), (200, 100), (300, 100), (300, 300), (100, 300), (100, 200))) ) intersection: ((((100, 100), (200, 100), (200, 200), (100, 200)))) }