Calculate the area of geometry

40 次查看(过去 30 天)
Minh
Minh 2022-9-30
评论: Minh 2022-9-30
I have a difficult problem and I need help, "Write a function to calculate the area and perimeter of shapes: triangle, rectangle, circle?"
  2 个评论
Minh
Minh 2022-9-30
@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.

请先登录,再进行评论。

回答(3 个)

Walter Roberson
Walter Roberson 2022-9-30
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 个评论
Minh
Minh 2022-9-30
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.

请先登录,再进行评论。


Sam Chak
Sam Chak 2022-9-30
编辑:Sam Chak 2022-9-30
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

Minh
Minh 2022-9-30
#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;

类别

Help CenterFile Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息

标签

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by