从3个坐标点找到三角形的区域,返回0

时间:2015-09-29 06:52:22

标签: c debugging math area

所以我的程序假设取3个值并将其存储在变量Ax,Ay等中...但由于某种原因它会继续返回该区域的0.00并且从我看到所有这些代码应该返回一个正确的区域,它应用距离公式和苍鹭的面积公式

#include <stdio.h>
#include <math.h>

int main(void) {
    float Ax,Ay,Bx,By,Cx,Cy,A,B,C,side,area;
    printf("hello inPut x,y coords");
    printf("input 3 numbers for your x coordinates");
    scanf("%f%f%f",&Ax,&Bx,&Cx );
    printf("input 3 numbers for your y coordinates");
    scanf("%f%f%f",&Ay,&By,&Cy);

    A=sqrt(((Bx-Ax)*(Bx-Ax))+((By-Ay)*(By-Ay)));
    B=sqrt(((Cx-Bx)*(Cx-Bx))+((Cy-By)*(Cy-By)));
    C=sqrt(((Ax-Cx)*(Ax-Cx))+((Ay-Cy)*(Ay-Cy)));



    side=((A+B+C)/2);

    area=sqrt(side*(side-A)*(side-B)*(side-C));
    printf("n AREA OF TRIANGLE IS %f",area);

}

它想要做的伪代码是:

Take 3 numbers like 
5,7,8
These first three numbers are now stored in Ax,Bx,Cx (Ax=5,Bx=7,Cx=8)
Take 3 more numberss for the y coordinate (1,2,3)
Store numbers in Ay By Cy (Ay =1, By=2, Cy=3)

take these values and calculate the distance between them by using this formula:

sqrt((Bx-Ax)^2+(By-Ay)^2)
repeat this 2 more time for the other numbers

now it calculates the perimeter of the triangle, and halves it , then inputs it into the heron formula to get the area.

Ideally these values should give an area > 0.

1 个答案:

答案 0 :(得分:2)

您的代码运行正常。我尝试使用这个三分体1.2 2.8 7.8和1.3 2.3 0.1,这是好的。也许您使用“,”而不是“。”小数分离......