Initial commit
This commit is contained in:
14
README.md
Normal file
14
README.md
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#Barnsley fern
|
||||||
|
|
||||||
|
_original_barnsley_fern.py_ - Implementation of Barnsley Fern
|
||||||
|
with Python's turtle module
|
||||||
|
|
||||||
|
_enhanced_barnsley_fern.py_ - Draws Barnsley fern with minor
|
||||||
|
variations compared to the original.
|
||||||
|
|
||||||
|
These scripts can be invoked from command line directly. Like:
|
||||||
|
./enhanced_barnsley_fern.py Make sure you chmod +x the file you'd
|
||||||
|
like to run.
|
||||||
|
|
||||||
|
Invoke programs with --help to see command-line options (background color,
|
||||||
|
fern color, size etc.).
|
71
enhanced_barnsley_fern.py
Executable file
71
enhanced_barnsley_fern.py
Executable file
@@ -0,0 +1,71 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import argparse
|
||||||
|
from turtle import Turtle
|
||||||
|
import random
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Draw Barnsley fern using Python's Turtle graphics.")
|
||||||
|
parser.add_argument('-d', '--dots', metavar='number',
|
||||||
|
help='Number of dots to draw. Deefault is 20000',
|
||||||
|
type=int, default=20000)
|
||||||
|
parser.add_argument('-bg', '--background', metavar='color',
|
||||||
|
type=str, default='white',
|
||||||
|
help='Sets background color.')
|
||||||
|
parser.add_argument('-fg', '--foreground', metavar='color',
|
||||||
|
type=str, default='green',
|
||||||
|
help='Sets pen color color.')
|
||||||
|
parser.add_argument('-s', '--scale', metavar='pixels',
|
||||||
|
type=int, default=500,
|
||||||
|
help='Chooses scale. Default is 500 pixels.')
|
||||||
|
parser.add_argument('-ww', '--width', metavar='pixels',
|
||||||
|
type=int, default=600,
|
||||||
|
help='Window width.')
|
||||||
|
parser.add_argument('-wh', '--height', metavar='pixels',
|
||||||
|
type=int, default=800,
|
||||||
|
help='Window height.')
|
||||||
|
args = parser.parse_args()
|
||||||
|
return args
|
||||||
|
|
||||||
|
def draw_fern(pen):
|
||||||
|
x = 0.5
|
||||||
|
y = 0.0
|
||||||
|
for i in range(args.dots):
|
||||||
|
r = random.randint(0, 100)
|
||||||
|
if r <= 1:
|
||||||
|
tempx = 0.5
|
||||||
|
tempy = 0.16 * y
|
||||||
|
elif r <= 8:
|
||||||
|
tempx = 0.2 * x - 0.26 * y + 0.400
|
||||||
|
tempy = 0.23 * x + 0.22 * y - 0.045
|
||||||
|
elif r <= 15:
|
||||||
|
tempx = -0.15 * x + 0.28 * y + 0.575
|
||||||
|
tempy = 0.26 * x + 0.24 * y - 0.086
|
||||||
|
else:
|
||||||
|
tempx = 0.85 * x + 0.04 * y + 0.075
|
||||||
|
tempy = -0.04 * x + 0.850 * y + 0.180
|
||||||
|
if r == 100:
|
||||||
|
r = 0
|
||||||
|
r += 1
|
||||||
|
x, y = tempx, tempy
|
||||||
|
pen.setpos(x * args.scale - args.scale // 2, y * args.scale - args.scale // 2)
|
||||||
|
pen.dot(1)
|
||||||
|
|
||||||
|
def setup_pen():
|
||||||
|
pen = Turtle()
|
||||||
|
pen.speed(0)
|
||||||
|
pen.ht()
|
||||||
|
pen.pu()
|
||||||
|
pen.screen.setup (width=args.width, height=args.height)
|
||||||
|
pen.screen.title('Barnsley fern')
|
||||||
|
pen.screen.bgcolor(args.background)
|
||||||
|
pen.screen.tracer(n=15000) # skips frames to speed up drawing
|
||||||
|
pen.color(args.foreground)
|
||||||
|
return pen
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
args = parse_args()
|
||||||
|
pen = setup_pen()
|
||||||
|
draw_fern(pen)
|
||||||
|
input('All done!\nPress Enter to quit')
|
||||||
|
exit()
|
64
original_barnsley_fern.py
Executable file
64
original_barnsley_fern.py
Executable file
@@ -0,0 +1,64 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import argparse
|
||||||
|
from turtle import RawPen, Screen
|
||||||
|
import random
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Draw Barnsley's fern using Python's Turtle graphics.")
|
||||||
|
parser.add_argument('-d', '--dots', metavar='number',
|
||||||
|
help='Number of dots to draw.',
|
||||||
|
type=int, default=20000)
|
||||||
|
parser.add_argument('-bg', '--background', metavar='color',
|
||||||
|
type=str, default='white',
|
||||||
|
help='Background color.')
|
||||||
|
parser.add_argument('-fg', '--foreground', metavar='color',
|
||||||
|
type=str, default='green',
|
||||||
|
help='Drawing color.')
|
||||||
|
parser.add_argument('-s', '--scale', metavar='pixels',
|
||||||
|
type=int, default=50,
|
||||||
|
help='Choose scale. Default is 50 pixels.')
|
||||||
|
parser.add_argument('-ww', '--width', metavar='pixels',
|
||||||
|
type=int, default=600,
|
||||||
|
help='Window width.')
|
||||||
|
parser.add_argument('-wh', '--height', metavar='pixels',
|
||||||
|
type=int, default=800,
|
||||||
|
help='Window width.')
|
||||||
|
args = parser.parse_args()
|
||||||
|
return args
|
||||||
|
|
||||||
|
def draw(root):
|
||||||
|
pen = RawPen(root)
|
||||||
|
pen.speed(0)
|
||||||
|
pen.ht()
|
||||||
|
pen.pu()
|
||||||
|
pen.screen.bgcolor(args.background)
|
||||||
|
x = 0.0
|
||||||
|
y = 0.0
|
||||||
|
for i in range(args.dots):
|
||||||
|
r = random.randint(0, 100)
|
||||||
|
if r <= 1:
|
||||||
|
tempx = 0.0
|
||||||
|
tempy = 0.16 * y
|
||||||
|
elif r <= 7:
|
||||||
|
tempx = 0.2 * x - 0.26 * y
|
||||||
|
tempy = 0.23 * x + 0.22 * y + 1.6
|
||||||
|
elif r <= 15:
|
||||||
|
tempx = -0.15 * x + 0.28 * y
|
||||||
|
tempy = 0.26 * x + 0.24 * y + 0.44
|
||||||
|
else:
|
||||||
|
tempx = 0.85 * x + 0.04 * y
|
||||||
|
tempy = -0.04 * x + 0.850 * y + 1.6
|
||||||
|
x, y = tempx, tempy
|
||||||
|
pen.setpos(x * args.scale - args.scale // 2, y * args.scale - args.scale * 3)
|
||||||
|
pen.dot(1, args.foreground)
|
||||||
|
print('All done!')
|
||||||
|
input('Press any key to quit')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
args = parse_args()
|
||||||
|
root = Screen()
|
||||||
|
root.setup (width=600, height=800)
|
||||||
|
root.title('Barnsley fern')
|
||||||
|
root.tracer(n=15000)
|
||||||
|
draw(root)
|
Reference in New Issue
Block a user