matlab introduction code
ITP 168 – Introduction to MATLAB Spring 2016
Page 1 of 4
Homework 6: Shapes
Setup Given what we know about structures, we will write a script file that will ask the user if they wish to
create 3 shapes: circle, right triangle, and square. Each of these shapes will be a structure. Each of those
structures will be nested WITHIN a general structure: shape. The hierarchy looks like this:
Shape Structure
o Attribute: Circle Structure
Attribute: Radius
Attribute: Center (2-element vector)
Attribute: Area
o Attribute: Right Triangle Structure
Attribute: Side Lengths (2-element vector) (you cannot arbitrarily prescribe all 3
side lengths)
Attribute: Center (2-element vector)
Attribute: Area
o Attribute: Square Structure
Attribute: Side Length
Attribute: Center (2-element vector)
Attribute: Area
Part 1: Creating Shapes The script will ask the user, in succession, if they would like to create a circle, right triangle, or square. If
the user responds with a ‘y’, create the structure they desire within the shape structure and assign
random values to all attributes EXCEPT the area. You must calculate the area yourself. You must use a
string variable for the user input.
Use randi() to assign random values to the attributes. You can specify your own maximum value for the
randi() function.
Part 2: How many shapes? Check to see if the user has created any shapes. If they have not created any shapes, inform them that
they have no shapes, and therefore cannot continue on with the calculations and end the program. If
they only created one shape, inform them that they have only created one shape, and tell them which
shape they created and end the program.
If they have created more than one shape, move on to Part 3.
ITP 168 – Introduction to MATLAB Spring 2016
Page 2 of 4
Part 3: Distance Calculation Now that we know the user has created at least 2 shapes, print out the list of shapes to the user. (Hint:
print out the list of fieldnames, because you will be required to use dynamic field names).
Ask the user to choose a shape, and be sure to inform them they must type it exactly as you have
displayed it. Store this user input as a string variable. Check to see if the user input matches any of the
fieldnames of the shape structure. If it does not match any of the fieldnames of the shape structure,
inform the user they must type in their choice again.
Repeat this process again for the second shape (Note: user can enter the same shape twice, that’s
okay!)
Once you have the two shape names stored as strings, use dynamic field names to calculate the distance
between their centers. For a shape with center [X1 Y1] and another shape with center [X2 Y2], the
distance between the centers is:
𝐷𝑖𝑠𝑡𝑎𝑛𝑐𝑒 = √(𝑋2 − 𝑋1)2 + (𝑌2 − 𝑌1)22
Print out the distance. Format with 3 digits after the decimal.
Part 4: Area Ratio Follow the same process as in Part 3 to ask the user for two valid shapes.
Once you have the two valid shapes, calculate the ratio of the area of shape 1 to shape 2. For example if
the user chooses the square and the circle, calculate the ratio of the areas of the square to the circle.
Print out the ratio. Format with 3 digits after the decimal.
Sample Output Below is the sample output. Note that the script is run several times in succession. User input is
highlighted in red:
>> A5
Would you like to create a circle? (Y/N): y
Would you like to make a triangle? (Y/N): y
Would you like to make a square? (Y/N): y
You created these shapes:
circle
triangle
square
Choose two shapes to calculate the distance between centers.
You must spell them EXACTLY as they were written
ITP 168 – Introduction to MATLAB Spring 2016
Page 3 of 4
First shape: circle
Second shape: Triangle
No shape by that name!
Second shape: triangle
The distance between the circle and the triangle is 9.434
Choose two shapes to calculate the ratio of the areas.
You must spell them EXACTLY as they were written
First shape: circle
Second shape: circle
The ratio of the areas (circle:circle) is 1.000
>> A5
Would you like to create a circle? (Y/N): n
Would you like to make a triangle? (Y/N): n
Would you like to make a square? (Y/N): n
No shapes created
>> A5
Would you like to create a circle? (Y/N): n
Would you like to make a triangle? (Y/N): n
Would you like to make a square? (Y/N): y
You only created a single shape: square
>> A5
Would you like to create a circle? (Y/N): y
Would you like to make a triangle? (Y/N): n
Would you like to make a square? (Y/N): y
You created these shapes:
circle
square
Choose two shapes to calculate the distance between centers.
You must spell them EXACTLY as they were written
First shape: circle
Second shape: square
The distance between the circle and the square is 2.236
Choose two shapes to calculate the ratio of the areas.
You must spell them EXACTLY as they were written
First shape: square
Second shape: circle
The ratio of the areas (square:circle) is 0.063
ITP 168 – Introduction to MATLAB Spring 2016
Page 4 of 4
Grading Item Points
Part 1: Creating Shapes 10 Part 2: How Many? 10 Part 3: Find Distance 20 Part 4: Ratio of Areas 20 Total* 60
*Points will be deducted for poor code style
Deliverables
1. A compressed HW6.zip folder containing the file named HW6.m. It must be submitted through
Blackboard.
This assignment is due: Friday, March 11 2016 at 11:59:59pm