added Algorithms/SierpinskiTriangle.cs
This commit is contained in:
1 parent
6acd8c8ff1
commit
a832521bda
7 files changed
+137
-4
No files matched your search
+12
-1
@@ -1,6 +1,6 @@
|
||||
namespace Algorithms;
|
||||
using System;
|
||||
class Point
|
||||
public class Point
|
||||
{
|
||||
private double x;
|
||||
private double y;
|
||||
@@ -26,6 +26,13 @@ class Point
|
||||
return Math.Sqrt(X() * X() + Y() * Y());
|
||||
}
|
||||
|
||||
public double Distance(Point other)
|
||||
{
|
||||
double xDiff = X() - other.X();
|
||||
double yDiff = Y() - other.Y();
|
||||
return Math.Sqrt(xDiff * xDiff + yDiff*yDiff);
|
||||
}
|
||||
|
||||
public Point MidPointOf(Point other)
|
||||
{
|
||||
double midX = (X() + other.X()) / 2;
|
||||
@@ -33,4 +40,8 @@ class Point
|
||||
return new Point(midX, midY);
|
||||
}
|
||||
|
||||
public override string ToString(){
|
||||
return "{\"x\": " + this.x + ", \"y\": " + this.y + "}";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace Algorithms;
|
||||
|
||||
public class SierpinskiTriangle{
|
||||
private readonly Random random;
|
||||
|
||||
public SierpinskiTriangle(Random random){
|
||||
this.random = random;
|
||||
}
|
||||
|
||||
public List<Point> GenerateRandomPath(Point point, int length){
|
||||
List<Point> path = new List<Point>();
|
||||
Triangle triangle = Triangle.Equilateral(10);
|
||||
for(int i = 0; i < length; i++){
|
||||
path.Add(point);
|
||||
point = point.MidPointOf(triangle.RandomSummit(random));
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+20
-2
@@ -1,5 +1,5 @@
|
||||
namespace Algorithms;
|
||||
internal class Triangle
|
||||
public class Triangle
|
||||
|
||||
{
|
||||
|
||||
@@ -7,7 +7,25 @@ internal class Triangle
|
||||
private Point sommet2;
|
||||
private Point sommet3;
|
||||
|
||||
|
||||
public Point Vertex1 { get {return sommet1;}}
|
||||
public Point Vertex2 { get {return sommet2;}}
|
||||
public Point Vertex3 { get {return sommet3;}}
|
||||
|
||||
public Point GetVertexById(int id){
|
||||
if(id == 0){
|
||||
return Vertex1;
|
||||
}
|
||||
if(id == 1){
|
||||
return Vertex2;
|
||||
}
|
||||
return Vertex3;
|
||||
}
|
||||
|
||||
|
||||
public Point RandomSummit(Random random){
|
||||
int id = random.Next(3);
|
||||
return GetVertexById(id);
|
||||
}
|
||||
|
||||
public Triangle(Point sommet1,Point sommet2,Point sommet3)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user