The Joy of Programming
Lines and Polygons

In this lesson we'll see how to draw lines and polygons.

Line

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)
``````

Exercise 2.14: Diagonals

Write a program to draw the diagonals of a square using the `line` function.

Exercise 2.15: Hash

Write a program to draw the following shape using lines.

Polygon

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)
``````

Exercise 2.16: Triangle

Write a program to draw the following shape using the `polygon` function.

Exercise 2.17: Diamond

Write a program to draw the following shape using the `polygon` function.

Exercise 2.18: Another Polygon

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.

Questions
2 years ago
Post
Dismiss
how to draw semicirlce
how to draw semicirlce
Want to discuss?
2 years ago
Post
Dismiss
How to solve 2.14?
How to solve 2.14?
AA
2 years ago
Post
Dismiss
s1 = rectangle(h=200,w=200) x1,y1 = -100,100 x2,y2 = 100,-100 x3,y3 = 100,100 x4,y4 = -100,-100 z1 = line(x1,y1,x2,y2) z2 = line(x3,y3,x4,y4) show(s1,z1,z2)
s1 = rectangle(h=200,w=200) x1,y1 = -100,100 x2,y2 = 100,-100 x3,y3 = 100,100 x4,y4 = -100,-100 z1 = line(x1,y1,x2,y2) z2 = line(x3,y3,x4,y4) show(s1,z1,z2)
Want to discuss?
FJ
2 years ago
Post
Dismiss
how to solve 2.15?
how to solve 2.15?
FJ
2 years ago
Post
Dismiss
how to solve 2.15
how to solve 2.15
2 years ago
Post
Dismiss
you can give the x and y variables negative values ```# your code here x1, y1 = -100, 50 x2, y2 = 100, 50 x3, y3 = -50, 100 x4, y4 = -50, -100 l1 = line(x1, y1, x2, y2) l2 = line(x1, -y1, x2, -y2) l3 = line(x3, y3, x4, y4) l4 = line(-x3, y3, -x4, y4) # draw the diagonals show(l1,l2,l3,l4)```
you can give the x and y variables negative values ```# your code here x1, y1 = -100, 50 x2, y2 = 100, 50 x3, y3 = -50, 100 x4, y4 = -50, -100 l1 = line(x1, y1, x2, y2) l2 = line(x1, -y1, x2, -y2) l3 = line(x3, y3, x4, y4) l4 = line(-x3, y3, -x4, y4) # draw the diagonals show(l1,l2,l3,l4)```
MJ
2 years ago
Post
Dismiss
l1=line(x1=-50,y1=100,x2=-50,y2=-100) l2=line(x1=50,y1=100,x2=50,y2=-100) l3=line(x1=-100,y1=50,x2=100,y2=50) l4=line(x1=-100,y1=-50,x2=100,y2=-50) show(l1,l2,l3,l4)
l1=line(x1=-50,y1=100,x2=-50,y2=-100) l2=line(x1=50,y1=100,x2=50,y2=-100) l3=line(x1=-100,y1=50,x2=100,y2=50) l4=line(x1=-100,y1=-50,x2=100,y2=-50) show(l1,l2,l3,l4)
Want to discuss?
how to solve 2.14
BD
2 years ago
Post
Dismiss
how to solve question number 2.14
how to solve question number 2.14
NA
2 years ago
Post
Dismiss
r1=rectangle(w=200,h=200) z1=line(x1=-100,y1=-100,x2=100,y2=100) z2=line(x1=-100,y1=100,x2=100,y2=-100) show(r1,z1,z2)
r1=rectangle(w=200,h=200) z1=line(x1=-100,y1=-100,x2=100,y2=100) z2=line(x1=-100,y1=100,x2=100,y2=-100) show(r1,z1,z2)
RT
2 years ago
Post
Dismiss
how to code a hash?
how to code a hash?
2 years ago
Post
Dismiss
how to solve question number 2.17
how to solve question number 2.17
BS
2 years ago
Post
Dismiss
p1=point(x=-100,y=0) p2=point (x=100,y=0) p3=point(x=0,y=100) p4=point(x=0,y=-100) shape=polygon ([p1,p3,p2,p4]) show(shape)
p1=point(x=-100,y=0) p2=point (x=100,y=0) p3=point(x=0,y=100) p4=point(x=0,y=-100) shape=polygon ([p1,p3,p2,p4]) show(shape)
AJ
2 years ago
Post
Dismiss
How to solve 2.18
How to solve 2.18
PK
2 years ago
Post
Dismiss
how to solve 2.15 ?
how to solve 2.15 ?
MJ
2 years ago
Post
Dismiss
# here is the square s = rectangle(w=200, h=200) show(s) # draw the diagonals d1=line(x1=100,y1=-100,x2=-100,y2=100) d2=line(x1=100,y1=100,x2=-100,y2=-100) show(d1,d2)
# here is the square s = rectangle(w=200, h=200) show(s) # draw the diagonals d1=line(x1=100,y1=-100,x2=-100,y2=100) d2=line(x1=100,y1=100,x2=-100,y2=-100) show(d1,d2)
Want to discuss?
BS
2 years ago
Post
Dismiss
How to slove 2.15 hash
How to slove 2.15 hash
2 years ago
Post
Dismiss
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)
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)
AN
2 years ago
Post
Dismiss
How to solve 2.17, diamond ?
How to solve 2.17, diamond ?
DP
2 years ago
Post
Dismiss
p1=point(x=-100,y=0) p2=point(x=0,y=-100) p3=point(x=100,y=0) p4=point(x=0,y=100) fig=polygon([p1,p2,p3,p4]) show(fig)
p1=point(x=-100,y=0) p2=point(x=0,y=-100) p3=point(x=100,y=0) p4=point(x=0,y=100) fig=polygon([p1,p2,p3,p4]) show(fig)
2 years ago
Post
Dismiss
Bhagavath, for 2.15, you need to draw 4 lines. Identify x1,y1 and x2,y2 for each of the 4 lines and draw it.
Bhagavath, for 2.15, you need to draw 4 lines. Identify x1,y1 and x2,y2 for each of the 4 lines and draw it.
2 years ago
Post
Dismiss
Ameena, for 2.17, you need to draw a polygon with 4 points. If you identify all the four points when you'll be able to draw it.
Ameena, for 2.17, you need to draw a polygon with 4 points. If you identify all the four points when you'll be able to draw it.
Want to discuss?