问题描述
我已经走了很远,但是似乎有些不起作用.
I've gotten pretty far but something just doesn't seem to work.
A = 50.88259382849774,6.003988087177277
B = 50.88269282423443,6.0036662220954895
C = 50.882530369581545,6.003847271203995
C坐标与90度线(x)略有不同,我制作的此函数应将C定位在最接近x线的位置.
The C coordinate is a little off from the 90 degree line (x) and this function I made should position C on the closest way to the x line.
this.snapCoords = function(a, b, c){
var result = (b.x-a.x)*(c.x-b.x)+(b.y-a.y)*(c.y-b.y);
var negative = false;
if(result < 0){
result = result*-1;
negative = true;
}
result = Math.sqrt(result);
result = result/2;
if(negative === false){
var d = {x: c.x+result, y: c.y-result};
}
else{
var d = {x: c.x-result, y: c.y+result};
}
console.log(d); // returns : 50.88246729610898,6.003910344676565
}
它确实获得了90度(x)线,但没有最接近的方式.我的功能肯定仍然有问题,但我无法弄清楚.
It does get the the 90 degree (x) line but not the closest way. Something must still be wrong in my function but I can't figure it out.
所以这是我的问题
我的函数将第三个坐标放置在C上,该坐标为90度,但不在它应该以任何方式延伸到另一点的位置(红点).
My function puts the third coordinate on C which is 90 degrees but not where it should be (the red spot) it somehow extends to a further point.
推荐答案
我认为OP试图将点C投影到通过点B的线上,并且垂直于线AB.在这种情况下,投影的数学不正确.您可以找到投影点D为
I think the OP is trying to project the point C onto the line passing thru point B and is perpendicular to line AB. If this is the case, the math for the projection is not correct. You can find the projected point D as
D = C-点(vec(BC),vec(AB))* vec(AB)/| vec(AB)| ^ 2
D= C - dot(vec(BC), vec(AB)) * vec(AB)/|vec(AB)|^2
通过此计算,投影点D将为(50.8825952820492,6.00363622113846).
By this calculation, the projected point D will be (50.8825952820492, 6.00363622113846).
下面是A,B,C和D点的图片:
The following is a picture for points A, B, C and D :
这篇关于寻找最接近的90度角点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!