计算通过中心的圆与直线的交点 [英] Calculate the point of intersection of circle and line through the center
问题描述
我如何得到一个线和一个圆的交点?我有很多关于这个主题的信息,但我的要求是不匹配的。
How do i get the point of intersection of a line and a circle.. I got lots of information on this topic, but my requirement is not matching..
我有一条线,其一个终点位于圆的起点处,另一个终点位于圆的外部。现在我需要这条直线和圆的交点。
I got a line whose one end point lies at the origin of the circle.. and other end lies somewhere outside the circle.. Now i need the point of intersection of this line and circle..
我试图用下面的公式从圆外找到最接近的边缘点,但无法破解它 -
I have tried to find closest edge point from outside the circle using below formula however unable to crack it -
closestCirclePoint = function(px, py, x, y, ray){
var tg = (x += ray, y += ray, 0);
return function(x, y, x0, y0){return Math.sqrt((x -= x0) * x + (y -= y0) * y);}(px, py, x, y) > ray ?
{x: Math.cos(tg = Math.atan2(py - y, px - x)) * ray + x, y: Math.sin(tg) * ray + y}
//{x: (px - x) / (length / ray) + x, y: (py - y) / (length / ray) + y}
: {x: px, y: py};
};
任何方法都可以解决这个问题。
在此先感谢!
Any way to get the solution for this problem..?? Thanks in advance!
推荐答案
Lindenhovius的JavaScript版本答案如下所示:
A JavaScript Version of Lindenhovius answer would look like this:
/**
* Finds the intersection between a circles border
* and a line from the origin to the otherLineEndPoint.
* @param {Vector} origin - center of the circle and start of the line
* @param {number} radius - radius of the circle
* @param {Vector} otherLineEndPoint - end of the line
* @return {Vector} - point of the intersection
*/
function findIntersect (origin, radius, otherLineEndPoint) {
var v = otherLineEndPoint.subtract(origin);
var lineLength = v.length();
if (lineLength === 0) throw new Error("Length has to be positive");
v = v.normalize();
return origin.add(v.multiplyScalar(radius));
}
但是你需要实现向量class* your self: p>
But you need to implement the vector "class"* your self:
function Vector (x, y) {
this.x = x || 0;
this.y = y || 0;
}
Vector.prototype.add = function (vector) {
return new Vector(this.x + vector.x, this.y + vector.y);
};
Vector.prototype.subtract = function (vector) {
return new Vector(this.x - vector.x, this.y - vector.y);
};
Vector.prototype.multiply = function (vector) {
return new Vector(this.x * vector.x, this.y * vector.y);
};
Vector.prototype.multiplyScalar = function (scalar) {
return new Vector(this.x * scalar, this.y * scalar);
};
Vector.prototype.divide = function (vector) {
return new Vector(this.x / vector.x, this.y / vector.y);
};
Vector.prototype.divideScalar = function (scalar) {
return new Vector(this.x / scalar, this.y / scalar);
};
Vector.prototype.length = function () {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
};
Vector.prototype.normalize = function () {
return this.divideScalar(this.length());
};
* JavaScript中没有真正的类 - 只是构造函数和,事情随ES6而改变,但这是另一个话题。
*there are no real classes in JavaScript — just constructor functions and prototypes, things change with ES6 but that's another topic.
这篇关于计算通过中心的圆与直线的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!