So far, we've drawn different shapes, but all of them were just black and white. Wouldn't it be fun to add some colors and styles to our sketches?
In this lesson we'll learn how to specify the fill color, the stroke color and the stoke width.
The optional argument fill
can be used to specify the color to fill
the shape with.
c = circle(fill='pink')
show(c)
Notice that the value 'pink'
is put in quotes. That is because we are specifying the value of the color as a string.
There are many colors with standard names. These include black, white, gray, red, blue, green, yellow, maroon, purple, pink and many more.
We can also specify the fill as 'none'
, if we don't want to fill
anything, which the default behavior.
One important thing to remember when using fill
is that the order of drawing shapes matter.
c1 = circle(fill='pink')
c2 = circle(x=50, fill='purple')
show(c1, c2)
As you can see the purple circle is being drawn on top of the pink circle.
The shapes are drawn in the order they are passed to the show
function.
Can you try to make the pink circle come on top of the purple circle?
The stroke color specifies the color used to draw the outline of a shape.
It can be specified for a shape by using the optional argument stroke
.
r = rectangle(stroke='red')
show(r)
We can also use stoke and fill together.
r = rectangle(stroke='red', fill='yellow')
show(r)
We can specify stroke as none
to skip drawing the outline.
c1 = circle(r=150, fill='green', stroke='none')
c2 = circle(r=100, fill='blue', stroke='none')
c3 = circle(r=50, fill='red', stroke='none')
show(c1, c2, c3)
The width of the outline can be specified using the optional argument stroke_width
.
c1 = circle(r=25, stroke_width=1)
c2 = circle(r=50, stroke_width=2)
c3 = circle(r=75, stroke_width=3)
c4 = circle(r=100, stroke_width=4)
c5 = circle(r=125, stroke_width=5)
show(c1, c2, c3, c4, c5)
Draw red and black squares as shown in the figure below.
Draw three overlapping squares as shown in the figure below.
Please note that the squares do not have a border.
Write a program to draw the polygon shown in the figure below.