使⽤Java求某点到直线上的投影点
使⽤Java求已知A,B点坐标,求C点到AB所连直线的投影点
/**
* 求直线外⼀点到直线上的投影点
*
* @param pLine 线上⼀点
* @param k 斜率
* @param pOut 线外⼀点
* @param pProject 投影点
*/
public static void getProjectivePoint(PointF pLine, double k, PointF pOut, PointF pProject) {
if (k == 0) {//垂线斜率不存在情况
pProject.x = pOut.x;
pProject.y = pLine.y;
} el {
pProject.x = (float) ((k * pLine.x + pOut.x / k + pOut.y - pLine.y) / (1 / k + k));德庆龙母庙
pProject.y = (float) (-1 / k * (pProject.x - pOut.x) + pOut.y);
}
}
/**
* 求pOut在pLine以及pLine2所连直线上的投影点
*九月九日忆
* @param pLine
* @param pLine2
* @param pOut
* @param pProject
二级士官是什么级别*/
public static void getProjectivePoint(PointF pLine, PointF pLine2, PointF pOut, PointF pProject) { double k = 0;
try {
k = getSlope(pLine.x, pLine.y, pLine2.x, pLine2.y);
} catch (Exception e) {
k = 0;
}
getProjectivePoint(pLine, k, pOut, pProject);
}
/**
* 通过两个点坐标计算斜率
* 已知A(x1,y1),B(x2,y2)
* 1、若x1=x2,则斜率不存在;
* 2、若x1≠x2,则斜率k=[y2-y1]/[x2-x1]
*
跑步手表* @param x1
* @param y1
* @param x2
* @param y2
心照不宣近义词
* @throws Exception 如果x1==x2,则抛出该异常
*/
public static double getSlope(double x1, double y1, double x2, double y2) throws Exception { if (x1 == x2) {
秋季食疗
throw new Exception(“Slope is not existence,and div by zero!”);
工作任务单}
白雪却嫌春色晚故穿庭树作飞花
return (y2 - y1) / (x2 - x1);
}