initial commit
This commit is contained in:
47
kochger.py
Executable file
47
kochger.py
Executable file
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import division
|
||||
try:
|
||||
import tkinter as tk
|
||||
except:
|
||||
import Tkinter as tk
|
||||
import math
|
||||
|
||||
C = math.sqrt( 3 )
|
||||
|
||||
def koch( x, y, r, smin ):
|
||||
pts, pts2 = [ ( x, y + r ),
|
||||
( x - C * r / 2, y - r / 2 ),
|
||||
( x + C * r / 2, y - r / 2 ) ], []
|
||||
s = 3 / C * r
|
||||
while s > smin:
|
||||
for i in range( len( pts ) ):
|
||||
( x1, y1 ), ( x5, y5 ) = pts[ i - 1 ], pts[ i ]
|
||||
dx, dy = ( x5 - x1 ) / 3, ( y5 - y1 ) / 3
|
||||
pts2 += [ ( x1 + dx, y1 + dy ),
|
||||
( ( x1 + x5 + C * dy ) / 2, ( y1 + y5 - C * dx ) / 2 ),
|
||||
( x5 - dx, y5 - dy ),
|
||||
( x5, y5 ) ]
|
||||
pts, pts2 = pts2, []
|
||||
s /= 4
|
||||
print len(pts), r
|
||||
return pts
|
||||
|
||||
def redraw( data ):
|
||||
x, y = data.width / 2, data.height / 2
|
||||
r = min( 2 * x / C, y )
|
||||
print x,y,r
|
||||
pts = koch( x, y, r, 1 )
|
||||
print len(pts)
|
||||
data.widget.delete( tk.ALL )
|
||||
data.widget.create_oval( x - r, y - r, x + r, y + r, outline="#F88" )
|
||||
for i in range( len( pts ) ):
|
||||
( x1, y1 ), ( x2, y2 ) = pts[ i - 1 ], pts[ i ]
|
||||
data.widget.create_line( x1, y1, x2, y2 )
|
||||
|
||||
window = tk.Tk()
|
||||
window.title( "Koch" )
|
||||
canvas = tk.Canvas( window )
|
||||
canvas.pack( expand=True, fill=tk.BOTH )
|
||||
canvas.bind( "<Configure>", redraw )
|
||||
window.mainloop()
|
||||
Reference in New Issue
Block a user