In this lesson we'll see how to draw lines and polygons.
Let's look at how to draw a line.
z = line()
show(z)
The above example draws a horizontal line connecting points (-100, 0) and (100, 0).
We can also specify the coordinates of both the start and end points.
z = line(x1=0, y1=0, x2=100, y2=100)
show(z)
The above example, draws a line from point (0, 0) to point (100, 100)
If we want to draw a triangle, we can do that by drawing three lines.
x1, y1 = 0, 0
x2, y2 = 100, 0
x3, y3 = 0, 100
z1 = line(x1, y1, x2, y2)
z2 = line(x2, y2, x3, y3)
z3 = line(x3, y3, x1, y1)
show(z1, z2, z3)
Write a program to draw the diagonals of a square using
the line
function.
Write a program to draw the following shape using lines.
As you could have seen in the previous example, it is get cumbersome
to draw polygons using just lines. The polygon
function is a better
way to create polygons.
The polygon
function takes a list of points as argument and returns
a polygon shape.
p1 = point(x=0, y=0)
p2 = point(x=100, y=0)
p3 = point(x=0, y=100)
shape = polygon([p1, p2, p3])
show(shape)
Write a program to draw the following shape using the polygon
function.
Write a program to draw the following shape using the polygon
function.
Write a program to draw the following shape as a single polygon
.
Hint: You just need to specify the two corners in the correct order.