我给了一个带有两个端点的线段:(x1,y1)(x2,y2)和一个随机点:(x3,y3)。
如果我将线段转换为极坐标,我需要能够以编程方式计算出最接近点(x3,y3)的线段上的点。
编辑: 我的问题出了问题。问题不是试图找到三者之间最接近的点。问题是......给定一条带有开始和结束的AB线...找到最靠近点(x3,y3)的线上的任意点。
答案 0 :(得分:1)
我想这太晚了,以防有人需要它,我只需转换回笛卡尔坐标,这样公式就更容易看了:
public static double distance(double r1,double t1,double r2,double t2,double r3,double t3)
{
double x1 = (r1*Math.cos(t1));
double x2 = (r1*Math.cos(t2));
double x3 = (r1*Math.cos(t3));
double y1 = (r1*Math.sin(t1));
double y2 = (r1*Math.sin(t2));
double y3 = (r1*Math.sin(t3));
return Math.abs((x2-x1)*(y1-y3)-(x1-x3)*(y2-y1))/Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
答案 1 :(得分:0)
public static Location closest(double x1, double y1, double x2, double y2, double x3, double y3){
Location a = new Location("provider");
Location b = new Location("provider");
Location c = new Location("provider");
//I can't remember which one goes where, you may need to swap all the x/ys around
//Do conversion or something here.
a.setLongitude(x1);
a.setLatitude(y1);
b.setLongitude(x2);
b.setLatitude(y2);
c.setLongitude(x3);
c.setLongitude(y3);
return closest(a, b, c);
}
public static Location closest(Location a, Location b, Location c){
return a.distanceTo(c) < b.distanceTo(c) ? a : b;
}
答案 2 :(得分:0)
这个问题涉及小学水平代数: 最近点只有3个选项:
使用毕达哥拉斯很容易找到(x3,y3)与两点之间的距离:
d1 = d(<x1,y1>,<x3,y3>) = sqrt( (x1-x3)^2 + (y1-y3)^2)
和
d2 = d(<x2,y2>,<x3,y3>) = sqrt( (x2-x3)^2 + (y2-y3)^2)
现在对于“更复杂”的部分:如果片段上的另一个点更接近(x3,y3) - 如果我们连接这些点 - 我们将有一个与原始对角线对应的新片段段 - 我们必须将它与一度方程(对于一条线)一起使用来找到第三个点并查看它是在段还是在外(如果它在段之外,那么我们将采用(x3)之间的最小距离,y3)到另外两点。
线方程(原谅我的英语 - 我用我的母语学习它,所以请耐心等待)是:
(I) y = mx + d
其中m很容易计算:
m = (y1-y2)/(x1-x2)
现在我们知道m
的价值,为了找到d
,我们将使用前两个点之一的值,例如:
y1 = mx1 +d => d = y1 - mx1
对角线的m'
为-1/m
,如果我们使用(x3,y3)的值,我们会找到d'
。所以现在我们知道(I)的值以及对角线的等式:
(II) y=m'x + d'
如果我们将使用这两个方程式,我们可以找到原始线段穿过的线上的点,它最接近(x3,y3) - 如果这一点位于线段上 - 我们就完成了,否则,如前所述 - 我们将在d1
和d2