Calculate the area of geometry

15 views (last 30 days)
Minh
Minh on 30 Sep 2022
Answered: Hala on 18 Oct 2022
I have a difficult problem and I need help, "Write a function to calculate the area and perimeter of shapes: triangle, rectangle, circle?"
  2 Comments
Minh
Minh on 30 Sep 2022
@Walter Rober i read it , i just want to say this problem is too difficult for me , i want to combine the same script but i can only do one idea calculate the area of each shape.

Sign in to comment.

Answers (4)

Walter Roberson
Walter Roberson on 30 Sep 2022
The first thing you need to do is decide how the user is going to represent the specific shape for calculation purposes.
  • are you going to have them input coordinates of each point?
  • are you going to have them input parameters such as height and width of the square, or radius of the circle?
  • are you going to have them input side lengths for the triangle? Side Angle Side ? From one side and two angles https://sciencing.com/calculate-triangle-one-side-given-8464316.html ? From three angles?
  • are you going to have the user draw the shape on the screen?
Once you have made the decision about representation, we can assist you in what user interface calls to make to fetch the input.
  1 Comment
Minh
Minh on 30 Sep 2022
My purpose:
- point coordinates will be randomly selected.
- requires manual input of parameters such as the height and width of the square or the radius of the circle.
- the area of the triangle will be calculated from the perimeter of the triangle.

Sign in to comment.


Sam Chak
Sam Chak on 30 Sep 2022
Edited: Sam Chak on 30 Sep 2022
If you know the point coordinates of the geometry, perhaps you can explore the area() function.
x = [0 2 1.5];
y = [0 0 1];
pgon = polyshape(x, y);
plot(pgon)
axis equal
A = area(pgon) % Area
A = 1
P = perimeter(pgon) % Perimeter
P = 4.9208
  1 Comment
Minh
Minh on 30 Sep 2022
@Sam Chakthanks for your contribution

Sign in to comment.


Minh
Minh on 30 Sep 2022
#include<stdio.h>
#include<math.h>
main(){
int choice;
printf("Enter\n1 to find area of Triangle\n2 for finding area of Circle\n3 for finding area of Rectangle\n");
scanf("%d",&choice);
switch(choice) {
case 1: {
int a,b,c;
float s,area;
printf("Enter sides of triangle\n");
scanf("%d%d %d",&a,&b,&c);
s=(float)(a+b+c)/2;
area=(float)(sqrt(s*(s-a)*(s-b)*(s-c)));
printf("Area of Triangle is %f\n",area);
break;
}
case 2: {
float len,breadth,area;
printf("Enter Length and Breadth of Rectangle\n");
scanf("%f %f",&len,&breadth);
area=(float)len*breadth;
printf("Area of Rectangle is %f\n",area);
break;
}
case 3: {
float radius,area;
printf("Enter Radius of Circle\n");
scanf("%f",&radius);
area=(float)3.14159*radius*radius;
printf("Area of Circle %f\n",area);
break;

Hala
Hala on 18 Oct 2022
explaining to them that it can calculate the area of right-angled triangle, with two inputs (width & height). The program displays the output (the area of the triangle) to the user in the command window. Part 2

Tags

Products


Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!