一个JS算法,通过检查点是否在固定大小的圆圈内来找到点的位置

问题描述 投票:0回答:1

所以,我在一个未知位置有一个点,我需要以编程方式找到它。我们尝试找到它的位置的唯一方法是调用一个特殊的

isThePointWithin500MetersOfLocation(x, y)
函数,如果该点位于所传递位置的 500 米范围内,该函数将返回 true。我需要找出重点在哪里,或者至少尽可能接近它。可以使用什么算法来实现此目的?

isThePointWithin500MetersOfLocation
函数需要约 30 秒的时间来执行,因此对其的调用越少越好。

javascript algorithm math geometry
1个回答
0
投票

这里正在测试一个点在另一个点半径内。

testForPointInCircle = function(cx,cy, radius, tx,ty) {
        var dx = tx - cx;
        var dy = ty - cy;
        var distance = dx * dx + dy * dy;
        return distance < (radius * radius);
}
    
var p1 = {x:0, y:500};
var p2 = {x:25, y:450};

window.console.log( testForPointInCircle(p1.x,p1.y, 70, p2.x,p2.y) + "   " + 
testForPointInCircle(p1.x,p1.y, 10, p2.x,p2.y) + "  " + p1.y );

var p3 = {x:10, y:1000};

//If you don't know the test point, you could call the function in a loop with a guess until you get true.

while( !testForPointInCircle(p3.x,p3.y, 25, p1.x,p1.y) ) {
    p1.y += 5;
}
window.console.log(p1.y);

© www.soinside.com 2019 - 2024. All rights reserved.