计算点到直线垂足坐标

原文地址

GetFootOfPerpendicular.cpp

#include <stdio.h>
#include <math.h>
#include <float.h>
typedef struct {
	double x;
	double y;
} POINT;
POINT GetFootOfPerpendicular(POINT pt,POINT begin,POINT end){
	POINT FootP;
	double dx = begin.x - end.x;
	double dy = begin.y - end.y;
	if(fabs(dx) <= FLT_EPSILON && fabs(dy) <= FLT_EPSILON){
		FootP = begin;
		return FootP;
	}
	double u = (pt.x - begin.x)*(begin.x - end.x) +(pt.y - begin.y)*(begin.y - end.y);
	u = u/((dx*dx)+(dy*dy));
	FootP.x = begin.x + u*dx;
	FootP.y = begin.y + u*dy;
	return FootP;
}
int main(void){
	POINT pa={0,0},pb={1,1},p1={2,0},p2={3,0};
	POINT p = GetFootOfPerpendicular(p1,pa,pb);
	printf("p1 垂足 %f,%f\n",p.x,p.y);
	p = GetFootOfPerpendicular(p2,pa,pb);
	printf("p2 垂足 %f,%f\n",p.x,p.y);
}

 

发表评论

邮箱地址不会被公开。 必填项已用*标注