Skip to content

Instantly share code, notes, and snippets.

@obelisk68
Created January 18, 2018 09:02
Show Gist options
  • Save obelisk68/e0e063ec5d96a817c687a81fc71e7707 to your computer and use it in GitHub Desktop.
Save obelisk68/e0e063ec5d96a817c687a81fc71e7707 to your computer and use it in GitHub Desktop.
シェルピンスキーのギャスケット
require 'oekaki'
Width, Height = 500, 450
Oekaki.app width: Width, height: Height, title: "Sierpinski gasket" do
draw do
clear
x1, y1 = Width / 2, Height - Width * 0.5 * Math.sqrt(3)
x2, y2 = 0, Height
x3, y3 = Width, Height
po0 = [[x1, y1], [x2, y2], [x3, y3]]
color(0xff * 256, 0xd7 * 256, 0) #gold
polygon(true, po0)
color(0, 0, 0)
sg = lambda do |n, po|
y = (po[0][1] + po[1][1]) / 2.0
l = (po[2][0] - po[1][0]) / 2.0
p1 = [po[1][0] + l / 2, y]
p2 = [po[2][0] - l / 2, y]
p3 = [po[1][0] + l, po[1][1]]
polygon(true, [p1, p2, p3])
return if n == 1
sg.call(n - 1, [po[0], p1, p2])
sg.call(n - 1, [p1, po[1], p3])
sg.call(n - 1, [p2, p3, po[2]])
end
sg.call(7, po0)
end
end
#http://obelisk.hatenablog.com/entry/2018/01/18/174837
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment