這裏有一個形象展示的問題:計算圓上一點的座標,沿圓的中心與另一點之間的一條直線?
比方說,我有Point A
在[0,0]
,並且在Point B
[50, 30]
。我想要找到沿着半徑爲15
的圓的Point X
的座標,原點爲Point A
,該座標也在Point A
和Point B
之間的一條線上。
指示最好的方法來做到這一點?
這裏有一個形象展示的問題:計算圓上一點的座標,沿圓的中心與另一點之間的一條直線?
比方說,我有Point A
在[0,0]
,並且在Point B
[50, 30]
。我想要找到沿着半徑爲15
的圓的Point X
的座標,原點爲Point A
,該座標也在Point A
和Point B
之間的一條線上。
指示最好的方法來做到這一點?
因爲這已經是t agged JavaScript的,這裏有一個簡單的實現:
// disclaimer: code written in browser
function Point2D(x, y) {
this.x = x;
this.y = y;
}
function findCircleInteresction(center, radius, target) {
var vector = new Point2D(target.x - center.x, target.y - target.y);
var length = Math.sqrt(Math.pow(vector.x, 2) + Math.pow(vector.y, 2));
var normal = new Point2D(vector.x/length, vector.y/length);
var result = new Point2D(center.x + (normal.x * radius), center.y + (normal.y * radius));
return result;
}
findCircleInteresction(new Point2D(0, 0), 15, new Point2D(50, 30));
Point2D
就是一個類來使物體與x
和y
性能。
findCircleInteresction
三個參數:
- center
的圓心
- radius
圓
的半徑 - target
點外圓
在findCircleInteresction
:
- 計算之間的vector
center
和target
- 得到結果的
- 計算normal
(歸一化)的vector
- 找到其中載體通過將圓的乘以圓
此半徑的中心加的歸一化的矢量分量與圓相交的點代碼可以進行大量優化,並且未經測試,但我認爲它說明了這個想法。
你會想這是兩個重疊的三角形,其中一個側面爲Bx-Ax
和By-Ay
。你想要找到的座標X
,這將是一個具體的邊三角形Xx-Ax
和Xy-Ay
,但已知斜邊R
,這是你的圓的半徑。請注意,兩個三角形的角度都等於x座標軸。
所以要得到三角形的角度,可以採用arctan(By-Ay/Bx-Ax)
現在用這個角度稱之爲T
,可以用你知道的半徑爲R
解決小腿問題。
要得到的x座標,你會採取Rcos(T)
爲了得到y座標,你會採取Rsin(T)
把它放在一起,你有Xx = Rcos(T)
和Xy = Rsin(T)
如果你不願意使用數學庫,這種方法可以使用,你可以使用比例(如Pointy
評論)
計算從A到B的距離。除以該半徑,然後將該比率乘以AB向量得到AX向量。 – Pointy
什麼意思? –