怎么编一个判断点在三角形内的C语言程序?
发布网友
发布时间:2022-04-23 20:27
我来回答
共1个回答
热心网友
时间:2023-08-05 09:57
以下代码定义了三个函数:
point:定义点的位置,
area:定义三角形的大小
main:主程序,通过调用point,area函数判断点是否在三角形内
#include<stdio.h>
#include<math.h>
struct point
{
double x;
double y;
};
int area(float m,float n,float t)
{
float p,S;
p=(m+n+t)/2;
S=sqrt(p*(p-m)*(p-n)*(p-t));
return S;
}
void main()
{
struct point a,b,c,d,p;
float AB,BC,AC;
float S0,S1,S2,S3;
float DA,DB,DC;
scanf("%lf,%lf\n",&a.x,&a.y);
scanf("%lf,%lf\n",&b.x,&b.y);
scanf("%lf,%lf\n",&c.x,&c.y);
AB=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
BC=sqrt((b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y));
AC=sqrt((a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y));
if((AB+BC)>AC && (AB+AC)>BC && (AC+BC)>AB){
printf("请输入D点坐标:\n");
scanf("%lf,%lf\n",&d.x,&d.y);
DA=sqrt((d.x-a.x)*(d.x-a.x)+(d.y-a.y)+(d.y-a.y));
DB=sqrt((d.x-b.x)*(d.x-b.x)+(d.y-b.y)+(d.y-b.y));
DC=sqrt((d.x-c.x)*(d.x-c.x)+(d.y-c.y)+(d.y-c.y));
S0=area(AB,BC,AC);
S1=area(DA,DB,AB);
S2=area(DA,DC,AC);
S3=area(DB,DC,BC);
if(S1+S2+S3-S0==0)
printf("点D位于三角形内部\n");
else
printf("点D位于三角形外部\n");
}
else
printf("不能构成三角形!");
}