Finding closest 90 degree angle spot

故事扮演 提交于 2019-12-11 18:25:34

问题


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

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
}

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.

EDIT:

So this is my problem

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.


回答1:


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 - dot(vec(BC), vec(AB)) * vec(AB)/|vec(AB)|^2

By this calculation, the projected point D will be (50.8825952820492, 6.00363622113846).

The following is a picture for points A, B, C and D :



来源:https://stackoverflow.com/questions/27251745/finding-closest-90-degree-angle-spot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!