Sierpinski Triangle sketch

This commit is contained in:
Xevion
2019-10-27 14:17:37 -05:00
parent a92c8e4a14
commit 0b82357401
3 changed files with 60 additions and 1 deletions

View File

@@ -19,3 +19,5 @@ All my projects attempting to create vivid or interesting visuals using the Proc
- **Rotating_HSB_Squares** - Slowly builds a mandela-like structure out of squares that slowly change color through the HSB color spectrum.
- **StackingEquilateralTriangles** - Another interesting rotating triangle sketch.
- **Sierpinski_Triangle** - Builds the Sierpinski Triangle

View File

@@ -0,0 +1,55 @@
import time
class Point:
def __init__(self, x, y, sdraw=False):
self.x, self.y = x, y
def s(self):
ellipse(self.x, self.y, 5, 5)
def mid(self, other):
return Point((self.x + other.x) / 2, (self.y + other.y) / 2)
def siepinski(i, a, b, c):
if i <= 0:
return
else:
triangle(a.x, a.y, b.x, b.y, c.x, c.y)
x = a.mid(b)
y = b.mid(c)
z = c.mid(a)
siepinski(i - 1, a, x, z)
siepinski(i - 1, x, b, y)
siepinski(i - 1, z, y, c)
# siepinski(i - 1, x, y, z)
def setup():
size(1000, 1000)
noLoop()
def equilateral(center, side):
altitude = side * (sqrt(3) / 2.0)
AM = (2.0/3.0) * altitude
BF = (1.0 / 2.0) * side
FM = (1.0 / 3.0) * altitude
a = Point(center.x, center.y + AM)
b = Point(center.x - BF, center.y - FM)
c = Point(center.x + BF, center.y - FM)
return a, b, c
i = 1
def draw():
global i
a, b, c = equilateral(Point(0, 0), 750)
translate(width/2, height/2)
siepinski(i, a, b, c)
time.sleep(1)
i += 1
def mouseClicked():
loop()

View File

@@ -0,0 +1,2 @@
mode=Python
mode.id=jycessing.mode.PythonMode