Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
验证C中的三角形_C - Fatal编程技术网

验证C中的三角形

验证C中的三角形,c,C,我是C语言的新手,所以我尝试创建一个程序来计算三角形的面积 当三角形存在时,计算面积很容易,但是直线的验证部分起作用 例如: A(0,-4)B(1,0)C(4,12)不会产生直线误差 #include <stdio.h> #include <math.h> double square(double num){ return (num*num); } int main() { double x[5],y[5],a,b,c,angle,area;

我是C语言的新手,所以我尝试创建一个程序来计算三角形的面积

当三角形存在时,计算面积很容易,但是直线的验证部分起作用

例如:

A(0,-4)B(1,0)C(4,12)不会产生直线误差

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

double square(double num){
    return (num*num);
}

int main()
{

    double x[5],y[5],a,b,c,angle,area;

    printf("Hello there! Calculating the area of triangle.\n");

    printf("Enter a coordinate A :\n");
    scanf("%lf,%lf",&x[0],&y[0]);

    printf("Enter another coordinate B :\n");
    scanf("%lf,%lf",&x[1],&y[1]);

    printf("Enter another coordinate C :\n");
    scanf("%lf,%lf",&x[2],&y[2]);

    // AB as base (a) , c is opposite side 

    a = sqrt( square((x[0]-x[1])) + square((y[0]-y[1])) );
    b = sqrt( square((x[0]-x[2])) + square((y[0]-y[2])) );
    c = sqrt( square(x[1]-x[2]) + square((y[1]-y[2])) );

    double num = (square(a)+square(b)-square(c))/(2*a*b);
    angle = acos(num);
    area = .5*a*b*sin(angle);

   //printf("%lf %lf %lf %lf %lf \n",a,b,c,num,angle);

    if (num == 1 || num ==-1){
        printf("That's a straight line.");
    }else{
        printf("Area of triangle is %lf\n",area);
    }

    return 0;

}
但是 A(4,12)B(1,0)C(0,-4)产生直线误差

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

double square(double num){
    return (num*num);
}

int main()
{

    double x[5],y[5],a,b,c,angle,area;

    printf("Hello there! Calculating the area of triangle.\n");

    printf("Enter a coordinate A :\n");
    scanf("%lf,%lf",&x[0],&y[0]);

    printf("Enter another coordinate B :\n");
    scanf("%lf,%lf",&x[1],&y[1]);

    printf("Enter another coordinate C :\n");
    scanf("%lf,%lf",&x[2],&y[2]);

    // AB as base (a) , c is opposite side 

    a = sqrt( square((x[0]-x[1])) + square((y[0]-y[1])) );
    b = sqrt( square((x[0]-x[2])) + square((y[0]-y[2])) );
    c = sqrt( square(x[1]-x[2]) + square((y[1]-y[2])) );

    double num = (square(a)+square(b)-square(c))/(2*a*b);
    angle = acos(num);
    area = .5*a*b*sin(angle);

   //printf("%lf %lf %lf %lf %lf \n",a,b,c,num,angle);

    if (num == 1 || num ==-1){
        printf("That's a straight line.");
    }else{
        printf("Area of triangle is %lf\n",area);
    }

    return 0;

}
#包括
#包括
双平方(双数值){
返回(num*num);
}
int main()
{
双x[5],y[5],a,b,c,角度,面积;
printf(“您好!正在计算三角形的面积。\n”);
printf(“输入坐标a:\n”);
扫描频率(“%lf,%lf”、&x[0]、&y[0]);
printf(“输入另一个坐标B:\n”);
扫描频率(“%lf,%lf”、&x[1]、&y[1]);
printf(“输入另一个坐标C:\n”);
扫描频率(“%lf,%lf”、&x[2]、&y[2]);
//AB为基础(a),c为对侧
a=sqrt(平方((x[0]-x[1]))+平方((y[0]-y[1]);
b=sqrt(平方((x[0]-x[2]))+square((y[0]-y[2]);
c=sqrt(平方(x[1]-x[2])+平方((y[1]-y[2]));
双数=(平方(a)+平方(b)-平方(c))/(2*a*b);
角度=acos(数值);
面积=.5*a*b*sin(角度);
//printf(“%lf%lf%lf%lf%lf\n”,a,b,c,num,angle);
如果(num==1 | | num==1){
printf(“这是一条直线”);
}否则{
printf(“三角形的面积为%lf\n”,面积);
}
返回0;
}

您可以使用不同的测试,也可以使用不同的面积公式。如果使用Heron公式,那么一旦得到a、b和c边的长度,就可以计算:

double p = (a + b + c)/2;
double area = sqrt(p*(p-a)*(p-b)*(p-c));
您可以通过检查p是否大于每个边来检测三角形是否有效

double p = (a + b + c)/2;
if ((p > a) && (p > b) && (p > c)) {
    double area = sqrt(p*(p-a)*(p-b)*(p-c));
    printf("Area of triangle is %lf\n", area);
} else {
    printf("I don't consider that a triangle.\n");
}

代码中的问题是
double num=(平方(a)+平方(b)-平方(c))/(2*a*b)的计算值略大于1。这可能发生在浮点计算中。在您的情况下,如果(num>1)num=1,您可以安全地添加
在该行之后,因为对于三角形,余弦方程始终会给您一个大于0小于1的值


但是,如果两点重叠并且
a
b
变为零,则会出现问题。如果您的代码需要这样的输入,您必须检查并将其作为特例处理。(如果两个点重叠,那么它们无论如何都是共线的。您可以通过检查
a、b、c
中的任何一个是否为零来检查重叠)

三个共线点确实形成了面积为零的退化三角形;IAC,你可能想看看有时候它确实返回0和nan。好的,我来解释一下,
a=sqrt(square((x[0]-x[1]))+square((y[0]-y[1])可以得到更精确的长度-->
a=hypot(x[0]-x[1],y[0]-y[1])。也更容易阅读。我怀疑也可能出现“略小于-1”的情况。@chux实际上不会。对于任何给定的三角形,
a+b>c
都成立。因此,
square(a)+square(b)-square(c)
将始终为正。边缘情况为
a+b=c
,其中点在一条直线上。在这种情况下,角度将为零。当两点重叠时,会出现一个问题,因为被零除可能会发生。这是一个很好的几何解释,但我们使用FP math,与纯数学相比,它具有所有不精确性、次法线、无穷大和舍入。我看到了一个小小的损失保证<代码> FAB(Num I同意您的观点)。因为OP是初学者,我没有提到任何关于浮点比较的东西。这里有一个IF OP需要更多的了解。我在C++中使用以下函数来进行FP比较<代码>模板内联布尔-ISAPROxLIST(t x,t y){返回STD::FAX(X-Y)<代码> STD::FAB(x-y)