이 충돌 감지 기능을 사용하여 A) 선분이 원과 교차하는지 여부와 B) 충돌이 발생하는 선분이 얼마나 떨어져 있는지를 결정합니다.충돌이 발생하는 선을 따라 백분율을 얻으려면 어떻게해야합니까?
저는 수학에별로 좋지 않습니다. 이것은 제가 작성한 함수가 아니기 때문에 충돌을 탐지하는 데는 효과가있는 것처럼 보이지만, 요격 지점에 도달하면 값이 커집니다. 기대하지 않았다. 충돌이 백분율로 발생하는 선 아래로 거리를 가져 오려고합니다. 예를 들어 선분이 100px
이고 원이 충돌하는 경우 50px
이 내려 오면 50px
을 반환하고, 그로부터 비율을 쉽게 계산할 수 있습니다. 더 라인 자체보다 값의 결과 var distance = Math.hypot(p1.x - i2.x, p1.y - i2.y);
와
Collision points: [111.91311159515845, 90.88529912057992], [92.30169719247377, 112.87385466298396]
: 50px
긴, 내가 갖는이 로그의 라인 사이의 충돌을
function interceptOnCircle(p1, p2, c, r) {
console.log(arguments);
//p1 is the first line point
//p2 is the second line point
//c is the circle's center
//r is the circle's radius
var p3 = {
x: p1.x - c.x,
y: p1.y - c.y
}; //shifted line points
var p4 = {
x: p2.x - c.x,
y: p2.y - c.y
};
var m = (p4.y - p3.y)/(p4.x - p3.x); //slope of the line
var b = p3.y - m * p3.x; //y-intercept of line
var underRadical = Math.pow(r, 2) * Math.pow(m, 2) + Math.pow(r, 2) - Math.pow(b, 2); //the value under the square root sign
if (underRadical < 0) {
//line completely missed
return false;
} else {
var t1 = (-m * b + Math.sqrt(underRadical))/(Math.pow(m, 2) + 1); //one of the intercept x's
var t2 = (-m * b - Math.sqrt(underRadical))/(Math.pow(m, 2) + 1); //other intercept's x
var i1 = {
x: t1 + c.x,
y: m * t1 + b + c.y
}; //intercept point 1
var i2 = {
x: t2 + c.x,
y: m * t2 + b + c.y
}; //intercept point 2
console.log('Collision points: [' + i1.x + ', ' + i1.y + '], [' + i2.x + ', ' + i2.y + ']')
var distance = Math.hypot(p1.x - i2.x, p1.y - i2.y);
return distance;
}
}
.
distance
값은 0과 50 사이가 될 것으로 예상됩니다. 충돌이 발생하는 행 (
p1
)을 따라 백분율을 얻으려면 어떻게해야합니까?
편집 : 테스트있어 선이 올바른 길이 단지 확인하기 위해, 나는 문제가 당신이 두 개의 원본 점 사이의 사람들에게 솔루션을 제한하지 않는 것이있을 수 있습니다 생각 console.log(Math.hypot(p1.x - p2.x, p1.y - p2.y)); //returns 49
감사합니다. Worked : 코드로 답을 수정하도록 제안했습니다. –