**Numworks: Drawing Simple Shapes with Pyplot**

**Welcome
to a special edition of our blog.**

The
goal of today’s blog is provide templates to draw simple shapes
such as ellipses, circles, rectangles, squares, equilateral
triangles, and 45-45-90 right triangles. Adjust the position, screen
size, and colors as you see fit.

The
module is used is pyplot.

**Ellipses
and Circles with drawellipse.py**

Draw
an ellipse given the length of x and y radii. If x and y are equal,
a circle is drawn. The screen is set up to fit the proportion of
the 320 x 220 screen. The vertical axis ranges from y = -44 to y =
44 while the horizontal axis ranges from x = -64 to x = 64. The
ellipse is centered at (0,0).

Numworks
script: https://my.numworks.com/python/ews31415/drawellipse

Script:
drawellipse.py

from
math import *

from matplotlib.pyplot import *

print("Draw
an ellipse\nusing pyplot")

a=eval(input("x axis?
"))

b=eval(input("y axis? "))

# set
axis

axis((-64,64,-44,44))

# set
points

tl=[i/128*2*pi for i in range(129)]

xl=[a*cos(i) for
i in tl]

yl=[b*sin(i) for i in tl]

# draw the
ellipse

c='purple'

plot(xl,yl,c)

show()

**Rectangles
and Squares with drawrect.py**

Draw
a rectangle given its horizontal length (h) and vertical length (v).
If the horizontal and vertical lengths are equal, a square is
drawn. The rectangle is centered at (0, 0). The screen is set up to
fit the proportion of the 320 x 220 screen. The vertical axis
ranges from y = -44 to y = 44 while the horizontal axis ranges from x
= -64 to x = 64.

Numworks
script: https://my.numworks.com/python/ews31415/drawrect

Script:
drawrect.py

from
math import *

from matplotlib.pyplot import *

print("Draw
a rectangle\nusing pyplot")

h=eval(input("horiz.
length? "))

v=eval(input("vert. length? "))

#
set axis

axis((-64,64,-44,44))

# set
points

xl=[-h/2,h/2,h/2,-h/2,-h/2]

yl=[v/2,v/2,-v/2,-v/2,v/2]

#
color

c='green'

# draw rectangle

plot(xl,yl,c)

show()

**Equilateral
Triangles with drawtrieq.py**

Draws
an equilateral (60-60-60) triangle. Enter the length of the side
(s). The base is set on the x-axis. The screen is set up to fit
the proportion of the 320 x 220 screen. The vertical axis ranges
from y = -44 to y = 44 while the horizontal axis ranges from x = -64
to x = 64.

Numworks
script: https://my.numworks.com/python/ews31415/drawtrieq

Script:
drawtrieq.py

from
math import *

from matplotlib.pyplot import *

print("Draw
an equilateral triangle\nusing pyplot")

s=eval(input("side
length? "))

# set axis

axis((-64,64,-44,44))

#
set points

t=s*sqrt(3)/2

xl=[-s/2,s/2,0,-s/2]

yl=[0,0,t,0]

#
draw the triangle

c='brown'

plot(xl,yl,c)

show()

**45-45-90
Degree Right Triangles with drawtrit.py**

Draws
an 45-45-90 degree right triangle. Enter the length of the one of
the legs (s), not the hypotenuse. The base is set on the x-axis.
The screen is set up to fit the proportion of the 320 x 220 screen.
The vertical axis ranges from y = -44 to y = 44 while the horizontal
axis ranges from x = -64 to x = 64.

Numworks
Script: https://my.numworks.com/python/ews31415/drawtrirt

**Script:
drawtrit.py**

from
math import *

from matplotlib.pyplot import *

print("Draw
a 45-45-90 triangle\nusing pyplot")

s=eval(input("short
length? "))

# set axis

axis((-64,64,-44,44))

#
set points

xl=[-s/2,s/2,-s/2,-s/2]

yl=[0,0,s,0]

#
draw the triangle

c='blue'

plot(xl,yl,c)

show()

Enjoy
and I hope you find these scripts useful in your drawing scripts.

Eddie

All
original content copyright, © 2011-2024. Edward Shore.
Unauthorized use and/or unauthorized distribution for commercial
purposes without express and written permission from the author is
strictly prohibited. This blog entry may be distributed for
noncommercial purposes, provided that full credit is given to the
author.