我们必须让用户输入6个点,然后计算它们之间形成的三角形的面积。
这是我的代码:
//Purpose of the program is to have the user input 6 points, have a triangle made from those points and then calculate the area of said triangle
int main()
{
//declaring variables. point1x is the x value for point 1 etc etc
float point1x;
float point1y;
float point2x;
float point2y;
float point3x;
float point3y;
float side1;
float side2;
float side3;
float svariable;
float triangleArea;
//getting user to enter in the points
printf ("Please enter point 1x: ");
scanf ("%point1x", &point1x);
printf ("Please enter point 1y: ");
scanf ("%point1y", &point1y);
printf ("Please enter point 2x: ");
scanf ("%point2x", &point2x);
printf ("Please enter point 2y: ");
scanf ("%point2y", &point2y);
printf ("Please enter point 3x: ");
scanf ("%point3x", &point3x);
printf ("Please enter point 3y: ");
scanf ("%point3y", &point3y);
//calculating the lengths of the triangle
side1=sqrt(((point1x-point2x)*(point1x-point2x))+((point1y-point2y)*(point1y-point2y)));
side2=sqrt(((point1x-point3x)*(point1x-point3x))+((point1y-point3y)*(point1y-point3y)));
side3=sqrt(((point2x-point3x)*(point2x-point3x))+((point2y-point3y)*(point2y-point3y)));
//svalue is needed for Heron's formula
svariable=(((side1)*(side2)*(side3))/2);
//implementing herons formula
triangleArea=(sqrt(svariable*(((svariable-side1)*(svariable-side2)*(svariable-side3)))));
printf("the triangle area is: %f", triangleArea);
}
我不知道出了什么问题以及为什么它不起作用。我也无法在数学中看到问题。我正在使用Heron的配方。
答案 0 :(得分:1)
在数学方面,你误解了苍鹭的公式:
svariable=(((side1)*(side2)*(side3))/2);
应该是
svariable= (side1 + side2 + side3) / 2.0;
答案 1 :(得分:0)
可能还有其他问题,但最明显的一个问题是您错误地使用了scanf
。这是正确的用法,这里有一些documentation。
printf ("Please enter point 1x: ");
scanf ("%f", &point1x);
答案 2 :(得分:0)
首先,您需要加入stdio.h
和math.h
#include <stdio.h>
#include <math.h>
在scanf
调用中,您将要将字符串更改为期望浮点数的格式字符串。
示例:scanf("%f", &point1x);
为您的所有scanf
电话执行此操作。
答案 3 :(得分:0)
我会使用交叉产品功能计算区域:
V1 X V2 = area contained by those two vectors
因此,为了使三角形的面积在3个点之间,使用任意两个向量(边)就足够了如下:
v1 = p1p2
v2 = p1p3
v1_x = (x2 - x1)
v1_y = (y2 - y1)
v2_x = (x3 - x1)
v2_y = (y3 - y1)
CROSS = v1_X * v2_y - v1_y * v2_x
和
area = CROSS /2