凸多边形对角线连线交叉点位置坐标的计算
如何计算凸多边形内部交叉点位置的坐标?比如(以凸五边形为例):假设这五边形的每个顶点的位置顺次为:
X1:500,700
X2:50,500
X3:100,100
X4:1000,100
X5: 1100,500
如图,内部交叉点共计5个,请问如何通过该五边形的顶点位置坐标来计算内部交叉点位置的坐标。
#include<stdio.h> int main() { double x,y,x0,y0,x1,y1,x2,y2,x3,y3,k1,k2; scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&x0,&y0,&x1,&y1,&x2,&y2,&x3,&y3); k1=(y0-y1)/(x0-x1); k2=(y2-y3)/(x2-x3); x=(k1*x0-k2*x2+y2-y0)/(k1-k2); y=y0+(x-x0)*k1; printf("%lf %lf\n",x,y); return 0; }
#include<stdio.h> int main() { double x,y,x0,y0,x1,y1,x2,y2,x3,y3,k1,k2; scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&x0,&y0,&x1,&y1,&x2,&y2,&x3,&y3); k1=(y0-y1)/(x0-x1); k2=(y2-y3)/(x2-x3); x=(k1*x0-k2*x2+y2-y0)/(k1-k2); y=y0+(x-x0)*k1; if ( ( ( (x>x0)&&(x<x1))&&( (y>y0)&&(y<y1) )||( (x<x0)&&(x>x1))&&( (y<y0)&&(y>y1) ) )&& ( ( (x>x2)&&(x<x3))&&( (y>y2)&&(y<y3) )||( (x<x2)&&(x>x3))&&( (y<y2)&&(y>y3) ) ) ) { printf("该两条线段相交\n"); printf("%lf %lf\n",x,y); } else printf("该两条线段不相交\n"); return 0; }
[此贴子已经被作者于2020-7-29 08:28编辑过]