The Joy of Programming
Combining Two Shapes

In the previous lessons, we've seen how to create many shapes and show all of them. In this lesson we are going to see how to combine multiple shapes into a single shape and how to use variables to capture the relations between the shapes used to make a compound shape.

Combining Two Shapes

The + operator is used to join two shapes together.

shape = circle(r=100) + rectangle(w=200, h=200)
show(shape)

In the above example, the shape is a compund shape by joining a circle and a rectangle.

Now, let's try to create a donut by adding two circles.

donut = circle(r=100) + circle(r=50)
show(donut)

The above example creates a donut shape using two circles, with the outer circle of radius 100 and the inner circle of radius 50, exactly half of the outer circle.

However, there is small issue with the above program. If we want to make the donut bigger, we need to change the radius of the both the circles while making sure that the radius inner circle is half of the outer circle.

Wouldn't it be better if change the number just once and the rest of it is taken care automatically? In the next example, we'll see how to achieve that using variables.

r = 100
donut = circle(r=r) + circle(r=r/2)
show(donut)

The result of this program is exactly same as the previous program, but now we can change the size of the donut a lot easier. We just need to change just one number and everything else already captured in the program.

Exercise 4.1: Eye With Variables

Draw an eye as shown in the figure below. Determine size of each shape using the variable width, which specifies the width of combined shape.


Exercise 4.2: Donut Again

Draw an draw donut with (100, 100) as the center, as shown in the figure below. The program has a variable size indicating the width and height of the resulting shape. Please use the variable size to compute the size of each circle drawn for the donut.


Questions
4.2 clear aayilla please answer
JG Jacob George
3 years ago
Post
Dismiss
Answer please
Answer please
PS Prathyush S
3 years ago
Post
Dismiss
size =100 donut = circle(r=50,x=100,y=100) + circle(r=25,x=100,y=100) show(donut)
size =100 donut = circle(r=50,x=100,y=100) + circle(r=25,x=100,y=100) show(donut)
AS Arjun Sukumaran
3 years ago
Post
Dismiss
size = 100 donut=circle(r=50,x=100,y=100)+circle(r=100,x=100,y=100) show(donut)
size = 100 donut=circle(r=50,x=100,y=100)+circle(r=100,x=100,y=100) show(donut)
Ruhile Mohammed
1 year ago
Post
Dismiss
size =100 donut = circle(r=size,x=100,y=100) + circle(r=size/2,x=100,y=100) show(donut)
size =100 donut = circle(r=size,x=100,y=100) + circle(r=size/2,x=100,y=100) show(donut)
Want to discuss?
Post it here, our mentors will help you out.
Ex 4.2
R Rishi
3 years ago
Post
Dismiss
Didn't understand the question.
Didn't understand the question.
SM Samuel Mathew
3 years ago
Post
Dismiss
4.2. question doesnt make much sense to me
4.2. question doesnt make much sense to me
Ak _00
3 years ago
Post
Dismiss
Can someone please explain 4.2
Can someone please explain 4.2
MN Muhammed Nabeel
3 years ago
Post
Dismiss
The code --> size = x donut = circle(100,100,size) + circle(100,100,2*size) show (donut) The qustions states us to make donut whose centre is the point (100,100)
The code --> size = x donut = circle(100,100,size) + circle(100,100,2*size) show (donut) The qustions states us to make donut whose centre is the point (100,100)
Want to discuss?
Post it here, our mentors will help you out.