⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gissegline.cpp

📁 程序编写环境为Visual Studio.NET 2002
💻 CPP
字号:
#include "StdAfx.h"
#include "gissegline.h"
#include <math.h>

CGisSegLine::CGisSegLine(void)
{
}

CGisSegLine::~CGisSegLine(void)
{
}
//-----------------------------------------------------------------------------------------
int CGisSegLine::GetDistance(MPoint* point, MPoint* ptHFoot, double* distance)
{
	double Px,Py,Ax,Ay,Bx,By;
	const double ZERODIST=0.00000001;
	double AB2,PA2,PB2,AB,PA,PB,S,AREA;
	double med,k1,k2,b1,b2;
	Px = point->x;
	Py = point->y;
	Ax = m_ptStartPoint.x;
	Ay = m_ptStartPoint.y;
	Bx = m_ptEndPoint.x;
	By = m_ptEndPoint.y;
	AB2 = (Ax-Bx)*(Ax-Bx)+(Ay-By)*(Ay-By);
	PB2 = (Px-Bx)*(Px-Bx)+(Py-By)*(Py-By);
	PA2 = (Ax-Px)*(Ax-Px)+(Ay-Py)*(Ay-Py);
	if(AB2 < ZERODIST)
	{
		med = sqrt(PA2);
		*distance = med;
		ptHFoot->x = Ax;
		ptHFoot->y = Ay;
		return  -1 ;
	}
	if(PA2 < ZERODIST)
	{
		med = sqrt(PA2);
		*distance = med;
		ptHFoot->x = Ax;
		ptHFoot->y = Ay;
		return  -2 ;
	}
	if(PB2 < ZERODIST)
	{
		med = sqrt(PB2);
		*distance = med;
		ptHFoot->x = Bx;
		ptHFoot->y = By;
		return  -2 ;
	}

	if( PA2 + AB2 < PB2 || AB2 + PB2 < PA2)	
	{                       
		if(PA2 > PB2)
		{
			med = PB2;
			ptHFoot->x = Bx;
			ptHFoot->y = By;
		}
		else
		{
			med = PA2;
			ptHFoot->x = Ax;
			ptHFoot->y = Ay;
		}
		med = sqrt(med);	
		*distance = med;
		return  -3 ;
	}
	else		
	{
		AB = sqrt(AB2);
		PA = sqrt(PA2);
		PB = sqrt(PB2);
		S = (AB + PA + PB) / 2.0;
		AREA = S;
		AREA *= (S-PA);
		AREA *= (S-PB);
		AREA *= (S-AB);
		AREA = sqrt(AREA);
		med = (2.0 * AREA) / AB;
		*distance = med;

		med = Ay-By;		
		if(med == 0.0)
		{
			ptHFoot->x = Px;
			ptHFoot->y = Ay;
			return  -4 ;
		}	
		med = Ax-Bx;		
		if(med == 0.0)
		{
			ptHFoot->y = Py;
			ptHFoot->x = Ax;
			return  -4 ;
		}	
		k1 = (Ay-By) / (Ax-Bx);	
		k2 =-1.0 / k1;			
		b1 = Ay - k1 * Ax;
		b2 = Py - k2 * Px;
		S = (b2 - b1) / (k1 - k2);
		ptHFoot->x = S;
		S = k1 * S + b1;
		ptHFoot->y = S;
		return 0;
	}
}
//-----------------------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -