投影点坐标

http://www.jb51.net/article/44271.htm

// VerticalPointFromCircletoLine.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <math.h>

typedef struct
{
float X;
float Y;
}Point;


void GetProjectivePoint(Point A, double k, Point R, Point* pTarget)
{
if (k == 0) //垂线斜率不存在情况
{
pTarget->X = R.X;
pTarget->Y = A.Y;
}
else
{
pTarget->X = (float)((k * A.X + R.X / k + R.Y - A.Y) / (1 / k + k));
pTarget->Y = (float)(-1 / k * (pTarget->X - R.X) + R.Y);
}
}

int _tmain(int argc, _TCHAR* argv[])
{
double k = 0;
Point A ={4,0};
Point B ={0,6};
Point R ={6,6};
Point Target ={0};
k = (B.Y-A.Y)/(B.X-A.X);
GetProjectivePoint(A,k,R,&Target);

return 0;
}

#include "stdafx.h"
#include <math.h>

typedef struct
{
double x;
double y;
} Point;

void GetProjectivePoint(Point* pA, Point* pB, Point* pCenterofCircle, Point* pTarget)
{
if( ( NULL == pA) || ( NULL == pB) || ( NULL == pCenterofCircle) || ( NULL == pTarget) )
{
return ;
}

double nDX = 0.0;
double nDY = 0.0;
double nSqr = 0.0;
double fSign = 0.0;
double fF1 = 0.0;
double fF2 = 0.0;

fSign = ( pB->y - pA->y )*( pCenterofCircle->x - pA->x ) - ( pCenterofCircle->y - pA->y )*(pB->x -pA->x);

//clockwise
//圆心在link的右侧
if( fSign > 0 )
{

}
//counterclockwise
//圆心在link的左侧
if( fSign < 0 )
{

}

//圆心在Link的延长线上
nDX = pB->x - pA->x;
nDY = pB->y - pA->y;
nSqr = nDX * nDX + nDY * nDY;
fF1 = (double)pCenterofCircle->x * (double)nDX + (double)pCenterofCircle->y * (double)nDY;
fF2 = (double)pA->x * (double)pB->y - (double)pA->y * (double)pB->x;

/* 取绝对值 */
pTarget->x = (abs(nDX * fF1 + nDY * fF2) / nSqr);
pTarget->y = (abs(nDY * fF1 - nDX * fF2) / nSqr);

return;

}
int _tmain(int argc, _TCHAR* argv[])
{

Point A ={4,0};
Point B ={0,6};
Point R ={6,6};
Point Target ={0};

GetProjectivePoint(&A,&B,&R,&Target);
return 0;
}

原文地址:https://www.cnblogs.com/hanleng/p/5589596.html