Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,41 @@
|
|||
Shoes.app(:height=>540,:width=>540, :title=>"Sierpinski Triangle") do
|
||||
def triangle(slot, tri, color)
|
||||
x, y, len = tri
|
||||
slot.append do
|
||||
fill color
|
||||
shape do
|
||||
move_to(x,y)
|
||||
dx = len * Math::cos(Math::PI/3)
|
||||
dy = len * Math::sin(Math::PI/3)
|
||||
line_to(x-dx, y+dy)
|
||||
line_to(x+dx, y+dy)
|
||||
line_to(x,y)
|
||||
end
|
||||
end
|
||||
end
|
||||
@s = stack(:width => 520, :height => 520) {}
|
||||
@s.move(10,10)
|
||||
|
||||
length = 512
|
||||
@triangles = [[length/2,0,length]]
|
||||
triangle(@s, @triangles[0], rgb(0,0,0))
|
||||
|
||||
@n = 1
|
||||
animate(1) do
|
||||
if @n <= 7
|
||||
@triangles = @triangles.inject([]) do |sum, (x, y, len)|
|
||||
dx = len/2 * Math::cos(Math::PI/3)
|
||||
dy = len/2 * Math::sin(Math::PI/3)
|
||||
triangle(@s, [x, y+2*dy, -len/2], rgb(255,255,255))
|
||||
sum += [[x, y, len/2], [x-dx, y+dy, len/2], [x+dx, y+dy, len/2]]
|
||||
end
|
||||
end
|
||||
@n += 1
|
||||
end
|
||||
|
||||
keypress do |key|
|
||||
case key
|
||||
when :control_q, "\x11" then exit
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
T_HEIGHT = sqrt(3) / 2
|
||||
TOP_Y = 1 / sqrt(3)
|
||||
BOT_Y = sqrt(3) / 6
|
||||
TRIANGLE_SIZE = 800
|
||||
|
||||
def settings
|
||||
size(TRIANGLE_SIZE, (T_HEIGHT * TRIANGLE_SIZE))
|
||||
smooth
|
||||
end
|
||||
|
||||
def setup
|
||||
sketch_title 'Sierpinski Triangle'
|
||||
fill(255)
|
||||
background(0)
|
||||
no_stroke
|
||||
draw_sierpinski(width / 2, height / 1.5, TRIANGLE_SIZE)
|
||||
end
|
||||
|
||||
def draw_sierpinski(cx, cy, sz)
|
||||
if sz < 5 # Limit no of recursions on size
|
||||
draw_triangle(cx, cy, sz) # Only draw terminals
|
||||
else
|
||||
cx0 = cx
|
||||
cy0 = cy - BOT_Y * sz
|
||||
cx1 = cx - sz / 4
|
||||
cy1 = cy + (BOT_Y / 2) * sz
|
||||
cx2 = cx + sz / 4
|
||||
cy2 = cy + (BOT_Y / 2) * sz
|
||||
draw_sierpinski(cx0, cy0, sz / 2)
|
||||
draw_sierpinski(cx1, cy1, sz / 2)
|
||||
draw_sierpinski(cx2, cy2, sz / 2)
|
||||
end
|
||||
end
|
||||
|
||||
def draw_triangle(cx, cy, sz)
|
||||
cx0 = cx
|
||||
cy0 = cy - TOP_Y * sz
|
||||
cx1 = cx - sz / 2
|
||||
cy1 = cy + BOT_Y * sz
|
||||
cx2 = cx + sz / 2
|
||||
cy2 = cy + BOT_Y * sz
|
||||
triangle(cx0, cy0, cx1, cy1, cx2, cy2)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue