简体   繁体   English

JS在圆上按一个角度移动点

[英]JS move point by an angle on circle

I have program where I click three times, each click creating a point on canvas.我有一个程序,我点击三次,每次点击都会在画布上创建一个点。 I then calculate angle between those three points like this:然后我像这样计算这三个点之间的角度:

function find_angle(A, B, C) {
    var AB = Math.sqrt(Math.pow(B.x - A.x, 2) + Math.pow(B.y - A.y, 2));
    var BC = Math.sqrt(Math.pow(B.x - C.x, 2) + Math.pow(B.y - C.y, 2));
    var AC = Math.sqrt(Math.pow(C.x - A.x, 2) + Math.pow(C.y - A.y, 2));
    return Math.acos((BC * BC + AB * AB - AC * AC) / (2 * BC * AB));
}

程度

In example picture above, the calculated angle is 93°.在上面的示例图片中,计算出的角度为 93°。 I need to move point 3 by -3° so the points make exactly 90°.我需要将point 3移动 -3°,以便这些点正好成 90°。 I have this function for it:我有这个功能:

var angleToCorrect = alpha * (Math.PI / 180) - 90 * (Math.PI / 180);
correct_angle(point2, point3, angleToCorrect)

...

function correct_angle(p2, p3, angle) {

    var x = p2.x - p3.x;
    var y = p2.y - p3.y;
    var r = Math.sqrt(x * x + y * y); //circle radius. origin of the circle is point 2


    return {
        X: p2.x + (Math.cos(angle) * r),
        Y: p2.y + (Math.sin(angle) * r)

    };

}

Now, this function should return new x and y for point 3 with corrected angle to 90°.现在,此函数应该返回point 3xypoint 3校正为 90°。 Yet the coordinates don't agree with what I expect.然而坐标与我的期望不一致。 Can someone point out what I'm doing wrong?有人可以指出我做错了什么吗?

To calculate the new position it isn't enough to provide just two of the points since the angle is measured between the three.要计算新位置,仅提供两个点是不够的,因为角度是在三个点之间测量的。

So inside this function you have to figure out what the current angle of the vector between point 1 and point 2 is.所以在这个函数中,你必须弄清楚点 1 和点 2 之间向量的当前角度是多少。 Javascript offers a nifty built-in function for this Math.atan2() Now that we know the angle (in radians) we need to add the new angle to it. Javascript 为这个Math.atan2()提供了一个漂亮的内置函数 现在我们知道了角度(以弧度为单位),我们需要向它添加新的角度。 This makes sure we can place point 3 correctly.这确保我们可以正确放置点 3。

function correct_angle(p1, p2, p3, angle)
{
    var currentAngle=Math.atan2(p1.y-p2.y, p1.x-p2.x);

    currentAngle+=angle;

    var x = p2.x - p3.x;
    var y = p2.y - p3.y;
    var r = Math.sqrt(x * x + y * y);

    return {
        X: p2.x + (Math.cos(currentAngle) * r),
        Y: p2.y + (Math.sin(currentAngle) * r)

    };
}

The angle parameter of the function should be the target angle in radians (90 or 1.5707963267949 in your case)该函数的角度参数应该是以弧度为单位的目标角度(在您的情况下为 90 或 1.5707963267949)

Here's an interactive example:这是一个交互式示例:

 Point = function(x, y) { this.x = x; this.y = y; } var pointA = new Point(162, 39); var pointB = new Point(105, 161); var pointC = new Point(211, 242); var context = document.getElementById("canvas").getContext("2d"); function correct() { var newPoint = correct_angle(pointA, pointB, pointC, 1.5707963267949); pointC.x = newPoint.X; pointC.y = newPoint.Y; draw(); } function correct_angle(p1, p2, p3, angle) { var currentAngle = Math.atan2(p1.y - p2.y, p1.x - p2.x); currentAngle += angle; var x = p2.x - p3.x; var y = p2.y - p3.y; var r = Math.sqrt(x * x + y * y); return { X: p2.x + (Math.cos(currentAngle) * r), Y: p2.y + (Math.sin(currentAngle) * r) }; } function draw() { context.clearRect(0, 0, 400, 300); context.fillStyle = "red"; context.beginPath(); context.arc(pointA.x, pointA.y, 10, 0, 2 * Math.PI); context.fill(); context.beginPath(); context.arc(pointB.x, pointB.y, 10, 0, 2 * Math.PI); context.fill(); context.beginPath(); context.arc(pointC.x, pointC.y, 10, 0, 2 * Math.PI); context.fill(); } draw();
 <canvas id="canvas" width="400" height="300" style="background-color:#dddddd;"></canvas> <button onclick="correct()" style="float:left"> correct me </button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM