y从用户坐标作为双变量并计算它们如果点形成三角形但我无法得到正确的结果。 我认为使用双变量存在问题,例如,如果我将整数变量放到x1,y1则不计算mAB
concat()
测试结果:
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<conio.h>
int main()
{
double x1,y1,x2,y2,x3,y3;
printf("Enter x , y coordinates of first vertice!\n");
scanf("%lf",&x1);
scanf("%lf",&y1);
/*
if(isInteger(x1)==0)
{
printf("\ndouble");
}
else
{
printf("\nint");
}
*/
/*********************************************/
printf("Enter x , y coordinates of second vertice!\n");
scanf("%lf",&x2);
scanf("%lf",&y2);
/*********************************************/
printf("Enter x , y coordinates of third vertice!\n");
scanf("%lf",&x3);
scanf("%lf",&y3);
/*********************************************/
/*********************************************/
double mAB = (fabs(x1-x2) / fabs(y1-y2));
double mAC = (fabs(x1-x3) / fabs(y1-y3));
printf("\n mAB %lf", mAB);
printf("\n mAC %lf", mAC);
if(mAB == mAC)
{
printf("These points does not forms a triangle!!!!");
}
else
{
/*
* 1-2 AB
* 1-3 AC
* 2-3 BC
*/
double distancexAB = (x2 - x1) * (x2 - x1);
double distanceyAB = (y2 - y1) * (y2 - y1);
double distanceAB = csqrt(fabs(distancexAB - distanceyAB));
/*********************************************/
double distancexAC = (x3 - x1) * (x3 - x1);
double distanceyAC = (y3 - y1) * (y3 - y1);
double distanceAC = csqrt(fabs(distancexAC - distanceyAC));
/*********************************************/
double distancexBC = (x2 - x3) * (x2 - x3);
double distanceyBC = (y2 - y3) * (y2 - y3);
double distanceBC = csqrt(fabs(distancexBC - distanceyBC));
/*********************************************/
printf("\n AB %lf", distanceAB);
printf("\n AC %lf", distanceAC);
printf("\n BC %lf", distanceBC);
double perimeter = distanceAB+distanceAC+distanceBC;
printf("\n Perimeter: %lf", perimeter);
}
getch();
return 0;
}
第二次测试
Enter x , y coordinates of first vertice!
1 1
Enter x , y coordinates of second vertice!
2 2
Enter x , y coordinates of third vertice!
3 3
mAB 1.000000
mAC 1.000000These points does not forms a triangle!!!!
答案 0 :(得分:3)
fabs()
不需要。 sqrt()
就足够了。 @Alexander Daum。hypot()
。
hypot
函数计算x
和y
的平方和的平方根,而不会出现过度上溢或下溢。 C11§7.12.7.32
double distancexAB = (x2 - x1) * (x2 - x1);
double distanceyAB = (y2 - y1) * (y2 - y1);
// double distanceAB = csqrt(fabs(distancexAB - distanceyAB));
double distanceAB = sqrt(distancexAB + distanceyAB);
// or even more simple
double distanceAB = hypot(x2 - x1, y2 - y1);
OP使用弱代码来检测斜率是否平行。 2个问题:
fabs()
失去了斜坡的标志
OP的方法除以0.0。
// Alternative code:
double delta_x12 = x1 - x2;
double delta_y12 = y1 - y2;
double delta_x13 = x1 - x3;
double delta_y13 = y1 - y3;
if (delta_x12*delta_y13 == delta_y12*delta_x13) {
printf("These points do not form a triangle.");
}
如果OP想要计算区域,代码可以使用Heron's formula并使用它来确定“点不形成三角形”。
double a = hypot(x1 - x2, y1 - y2);
double b = hypot(x2 - x3, y2 - y3);
double c = hypot(x2 - x1, y3 - y1);
double perimeter = a + b + c;
double s /* semi-perimeter */ = perimeter/2;
double area2 = s*(s-a)*(s-b)*(s-c);
// due to small inaccuracies, area2 may be negative.
double area = area2 > 0.0 ? sqrt(area2) : 0.0;
if (area == 0) {
printf("These points do not form a triangle.");
}
答案 1 :(得分:0)
在计算距离的地方,你必须写:
double distanceAB = sqrt(distancexAB + distanceyAB);
而不是
double distanceAB = csqrt(fabs(distancexAB - distanceyAB));
因为距离是hypothenuse并且使用sqrt而不是csqrt,因为csqrt用于复杂变量。
你不需要工厂,因为distancexAB
和distanceyAB
是正方形,因此它们不能为负数,只要不发生溢出,两个正数之和就是正数。