Drawings Demo

Here's some of what the drawing.py module can do.

In [1]:
from drawing import Drawing, Line, Point, Circle, Rectangle, CrudeLine, Polygon, Text
In [12]:
# 1. Create a drawing object to hold all the pieces.
demo = Drawing(width=800, height=300, background="#000000ff")

# 2. Add some shapes to it.

demo.add( Line(Point(20, 40), Point(600, 150), color='yellow', line_width=4) )
demo.add( Circle(Point(300, 120), 50, color='#aa00cc99', outline='cyan') )
demo.add( Polygon( (Point(380, 280), Point(400, 200), 
                    Point(500, 230), Point(590, 200), Point(630, 250)),
                    color='blue', outline='white') )
(p1, p2, p3) = (Point(100,220), Point(150, 100), Point(200, 130))
demo.add( CrudeLine(p1, p2) )
demo.add( CrudeLine(p2, p3) )
demo.add( CrudeLine(p3, p1) )
demo.add( Text(Point(550, 80), "It's a demo!", face='40px Averia', color='floralwhite' ))

# 3. Render and display the drawing below this cell.

demo.draw()
In [13]:
# Save a png image of the drawing to a file. 
# (This only works in another cell below the drawing, after it's been displayed.)
demo.write_file('demo.png')
demo.display()
In [14]:
print("Crude Stars")        

def star(center, size, color, drawing, crudity):
    from numpy import pi, sin, cos
    for i in range(5):
        theta0 = 4 * pi * i / 5
        theta1 = 4 * pi * (i+1) / 5
        # Points, like mathemetcial vectors, can be added togehter and multiplied by scalars.
        point_a = center + size * Point(cos(theta0), sin(theta0)) # vector + scalar * vector
        point_b = center + size * Point(cos(theta1), sin(theta1))
        drawing.add( CrudeLine(point_a, point_b, color=color, crudity=crudity ) )

sky = Drawing()
points = ((300,300), (100,180), (150,400), (380,120))
sizes = (50, 40, 60, 80)
colors = ('blue', 'green', 'cyan', 'red')
for (xy, size, color) in zip(points, sizes, colors):
    star(Point(*xy), size, color, sky, 5)
sky.draw()
Crude Stars
In [15]:
sky.write_file('stars.png')
sky.display()
In [16]:
# Here's the example from the drawing.py documentation.
d = Drawing(200, 200, background='#999999')
d.add( Rectangle(Point(10, 10), Point(180, 160), color='#aaccee') )
d.add( Polygon([Point(30, 40), Point(40, 100), Point(150, 130)], outline='black', color=None) )
d.add( Circle(Point(100, 100), radius=40, color='#996633cc'))
d.add( Text(Point(50, 185), "It's a drawing!", face="18px Helvetica", color='darkblue' ) )
d.draw()
In [17]:
d.display()
In [18]:
# Let's see what some blue-ish points look like ...
from random import randint
from drawing import rgb_color
dots = Drawing(500, 500)
for i in range(40000):
    dots.add(Point(randint(0, 500), randint(0, 500), 
                   color=rgb_color(randint(0,40), 
                                   randint(40,80), 
                                   randint(80,120))
                  ))
dots.draw()
In [19]:
dots.to_file('dots.png')
dots.display()
In [20]:
# Sol Levitt's wall drawing 86 
#
# See https://massmoca.org/event/walldrawing86/
# "Ten thousand lines about 10 inches (25 cm) long, covering the wall evenly."
#
# Let's assume that the wall is 10 feet by 10 feet and about" means 90% to 110% of 10 inches.
# The result is going to be strongly depenendent on the thickness of the line compared
# to the size of the canvas ... I'm not sure I have the resolution here to get pencil width.
# At least, with the values here, the line density is much higher than on the massmocha image.

from random import randint, random
from numpy import pi, sin, cos

wall_inches = 12 * 12  # 12 feet
line_inches = 10       
wall_pixels = 2000                                    
line_pixels = wall_pixels * line_inches / wall_inches
min_length = 0.9 * line_pixels
max_length = 1.1 * line_pixels
line_color = '#333333cc'       # fairly dark, mostly opaque
background = '#dddddd'    # fairly white
n_lines = 10000

wall86 = Drawing(width=wall_pixels, height=wall_pixels, background=background)

for i in range(n_lines):
    x0 = randint(0, wall_pixels)
    y0 = randint(0, wall_pixels)
    theta = 2 * pi * random()
    length = min_length + (max_length - min_length) * random()
    x1 = x0 + length * cos(theta)
    y1 = y0 + length * sin(theta)
    random_line = CrudeLine(
        Point(x0,y0), Point(x1,y1), 
        line_width=1, crudity=4, color=line_color)
    wall86.add( random_line )
    
wall86.draw()
In [21]:
wall86.write_file('wall86.png')
wall86.display()
In [ ]: