Implemented sierpinski triangle problem
This commit is contained in:
1 parent
a832521bda
commit
f0606731d5
5 files changed
+59
-14
No files matched your search
@@ -9,6 +9,9 @@ public class PointTest
|
|||||||
{
|
{
|
||||||
Point point = new Point(0, 1);
|
Point point = new Point(0, 1);
|
||||||
Assert.AreEqual("{\"x\": 0, \"y\": 1}", point.ToString());
|
Assert.AreEqual("{\"x\": 0, \"y\": 1}", point.ToString());
|
||||||
|
|
||||||
|
point = new Point(0, 1.5);
|
||||||
|
Assert.AreEqual("{\"x\": 0, \"y\": 1.5}", point.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
|||||||
@@ -11,4 +11,11 @@ public class SierpinskiTriangleTest
|
|||||||
Assert.AreEqual(10, st.GenerateRandomPath(new Point(2, 3), 10).Count());
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+7
-1
@@ -1,5 +1,7 @@
|
|||||||
namespace Algorithms;
|
namespace Algorithms;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
public class Point
|
public class Point
|
||||||
{
|
{
|
||||||
private double x;
|
private double x;
|
||||||
@@ -41,7 +43,11 @@ public class Point
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString(){
|
public override string ToString(){
|
||||||
return "{\"x\": " + this.x + ", \"y\": " + this.y + "}";
|
return "{\"x\": "
|
||||||
|
+ this.x.ToString(CultureInfo.InvariantCulture)
|
||||||
|
+ ", \"y\": "
|
||||||
|
+ this.y.ToString(CultureInfo.InvariantCulture)
|
||||||
|
+ "}";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -10,13 +10,8 @@ class EntryPoint
|
|||||||
|
|
||||||
public static void Main(String[] args)
|
public static void Main(String[] args)
|
||||||
{
|
{
|
||||||
|
SierpinskiTriangle st = new SierpinskiTriangle(new Random(666));
|
||||||
|
string vegaLite = st.GenerateVegaLiteSpec(new Point(2, 3), 2000);
|
||||||
IntChainLink lastLink = new IntChainLink(3);
|
Console.WriteLine(vegaLite);
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,22 +1,56 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Threading.Tasks.Dataflow;
|
||||||
|
|
||||||
namespace Algorithms;
|
namespace Algorithms;
|
||||||
|
|
||||||
public class SierpinskiTriangle{
|
public class SierpinskiTriangle
|
||||||
|
{
|
||||||
private readonly Random random;
|
private readonly Random random;
|
||||||
|
|
||||||
public SierpinskiTriangle(Random random){
|
public SierpinskiTriangle(Random random)
|
||||||
|
{
|
||||||
this.random = random;
|
this.random = random;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Point> 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<Point> path)
|
||||||
|
{
|
||||||
|
return "[ " + string.Join(", ", path) + " ]";
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Point> GenerateRandomPath(Point point, int length)
|
||||||
|
{
|
||||||
List<Point> path = new List<Point>();
|
List<Point> path = new List<Point>();
|
||||||
Triangle triangle = Triangle.Equilateral(10);
|
Triangle triangle = Triangle.Equilateral(10);
|
||||||
for(int i = 0; i < length; i++){
|
for (int i = 0; i < length; i++)
|
||||||
|
{
|
||||||
path.Add(point);
|
path.Add(point);
|
||||||
point = point.MidPointOf(triangle.RandomSummit(random));
|
point = point.MidPointOf(triangle.RandomSummit(random));
|
||||||
}
|
}
|
||||||
|
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string GenerateVegaLiteSpec(Point point, int length){
|
||||||
|
List<Point> path = GenerateRandomPath(point, length);
|
||||||
|
string pathAsString = PathToString(path);
|
||||||
|
return generateVegaLiteSpec(pathAsString);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user