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
  1 件のコメント
Minh
Minh 2022 年 9 月 30 日
@Sam Chakthanks for your contribution

サインインしてコメントする。


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 Center および File ExchangeLighting, 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