diff --git a/Algorithms.Tests/PointTest.cs b/Algorithms.Tests/PointTest.cs index a406121..630b887 100644 --- a/Algorithms.Tests/PointTest.cs +++ b/Algorithms.Tests/PointTest.cs @@ -9,6 +9,9 @@ public class PointTest { Point point = new Point(0, 1); Assert.AreEqual("{\"x\": 0, \"y\": 1}", point.ToString()); + + point = new Point(0, 1.5); + Assert.AreEqual("{\"x\": 0, \"y\": 1.5}", point.ToString()); } [TestMethod] diff --git a/Algorithms.Tests/SierpinskiTriangleTest.cs b/Algorithms.Tests/SierpinskiTriangleTest.cs index 7a6480c..60b43db 100644 --- a/Algorithms.Tests/SierpinskiTriangleTest.cs +++ b/Algorithms.Tests/SierpinskiTriangleTest.cs @@ -10,5 +10,12 @@ public class SierpinskiTriangleTest SierpinskiTriangle st = new SierpinskiTriangle(new Random(666)); Assert.AreEqual(10, st.GenerateRandomPath(new Point(2, 3), 10).Count()); } + + [TestMethod] + public void testGenerateVegaLiteSpec(){ + SierpinskiTriangle st = new SierpinskiTriangle(new Random(666)); + string vegaLite = st.GenerateVegaLiteSpec(new Point(2, 3), 2); + Assert.AreEqual(360, vegaLite.Length); + } } \ No newline at end of file diff --git a/Algorithms/Point.cs b/Algorithms/Point.cs index 53e4584..84c1695 100644 --- a/Algorithms/Point.cs +++ b/Algorithms/Point.cs @@ -1,5 +1,7 @@ namespace Algorithms; using System; +using System.Globalization; + public class Point { private double x; @@ -41,7 +43,11 @@ public class Point } public override string ToString(){ - return "{\"x\": " + this.x + ", \"y\": " + this.y + "}"; + return "{\"x\": " + + this.x.ToString(CultureInfo.InvariantCulture) + + ", \"y\": " + + this.y.ToString(CultureInfo.InvariantCulture) + + "}"; } } \ No newline at end of file diff --git a/Algorithms/Program.cs b/Algorithms/Program.cs index 3f62ea9..088a193 100644 --- a/Algorithms/Program.cs +++ b/Algorithms/Program.cs @@ -10,13 +10,8 @@ class EntryPoint public static void Main(String[] args) { - - - IntChainLink lastLink = new IntChainLink(3); - IntChainLink secondLink = new IntChainLink(2, lastLink); - IntChainLink firstLink = new IntChainLink(1, secondLink); - Console.WriteLine(firstLink.Head()); // 1 - Console.WriteLine(firstLink.Rest().Head()); // 2 - Console.WriteLine(firstLink.Rest().Rest().Head()); // 3 + SierpinskiTriangle st = new SierpinskiTriangle(new Random(666)); + string vegaLite = st.GenerateVegaLiteSpec(new Point(2, 3), 2000); + Console.WriteLine(vegaLite); } } diff --git a/Algorithms/SierpinskiTriangle.cs b/Algorithms/SierpinskiTriangle.cs index 6a6147f..32de63f 100644 --- a/Algorithms/SierpinskiTriangle.cs +++ b/Algorithms/SierpinskiTriangle.cs @@ -1,22 +1,56 @@ +using System.Net; +using System.Threading.Tasks.Dataflow; + namespace Algorithms; -public class SierpinskiTriangle{ +public class SierpinskiTriangle +{ private readonly Random random; - public SierpinskiTriangle(Random random){ + public SierpinskiTriangle(Random random) + { this.random = random; } - public List GenerateRandomPath(Point point, int length){ + private string generateVegaLiteSpec(string path) + { + return $$""" + { + "$schema": "https://vega.github.io/schema/vega-lite/v5.json", + "data": {"values": {{path}} }, + "width": 600, + "height": 600, + "mark": {"type": "point", "filled": true, "size": 2}, + "encoding": { + "x": {"field": "x", "type": "quantitative"}, + "y": {"field": "y", "type": "quantitative"} + } + } + """; + } + + private string PathToString(List path) + { + return "[ " + string.Join(", ", path) + " ]"; + } + + public List GenerateRandomPath(Point point, int length) + { List path = new List(); Triangle triangle = Triangle.Equilateral(10); - for(int i = 0; i < length; i++){ + for (int i = 0; i < length; i++) + { path.Add(point); point = point.MidPointOf(triangle.RandomSummit(random)); } - return path; } - + + public string GenerateVegaLiteSpec(Point point, int length){ + List path = GenerateRandomPath(point, length); + string pathAsString = PathToString(path); + return generateVegaLiteSpec(pathAsString); + } + } \ No newline at end of file