y坐标是用户作为双变量的坐标,如果点形成三角形,则计算它们,但我无法获得正确的结果。我认为使用双变量存在一个问题,例如,如果我将整数变量放在x1,y1,它不会计算mab
#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!!!!
第二次测试
Enter x , y coordinates of first vertice!
3.4 5.6
Enter x , y coordinates of second vertice!
1.2 3.4
Enter x , y coordinates of third vertice!
1.8 9.8
mAB 1.000000
mAC 0.380952
AB 0.000000
AC 3.883298
BC 6.371813
Perimeter: 10.255111
- 通过添加平方之和而不是减去点来计算点之间的距离。
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想要计算该区域,则代码可以使用苍鹭的公式,并使用它来确定"点不形成三角形"。
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.");
}
在计算距离的情况下,您必须写:
double distanceAB = sqrt(distancexAB + distanceyAB);
而不是
double distanceAB = csqrt(fabs(distancexAB - distanceyAB));
因为距离是假设的,并且使用SQRT代替CSQRT,因为CSQRT用于复杂变量。
您不需要Fabs,因为distancexAB
和distanceyAB
是正方形,因此它们不能为负,并且两个正数的总和将是正方形的,只要不出现溢出。