圈 - 线的交点不正常? [英] Circle - Line Intersection not working properly?
本文介绍了圈 - 线的交点不正常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了这个圈子线相交检测之后 http://mathworld.wolfram.com/Circle -LineIntersection.html ,但它似乎喜欢还是我失去了一些东西。
公共静态布尔相交
(Vector2f CirclePos,浮CircleRad,Vector2f点1,Vector2f点2)
{
Vector2f P1 = Vector2f.MemCpy(点1);
Vector2f P2 = Vector2f.MemCpy(点2);
//正常化点
p1.X - = CirclePos.X;
p1.Y - = CirclePos.Y;
p2.X - = CirclePos.X;
p2.Y - = CirclePos.Y;
浮动DX = p2.X - p1.X;
浮动DY = p2.Y - p1.Y;
浮动博士=(浮点)的Math.sqrt((双)(DX * DX)+(双)(DY * DY));
浮动D = p1.X * p2.Y * p2.X - p1.Y;
浮动DI =(CircleRad * CircleRad)*(*博士DR) - (D * D);
如果(DI℃下)返回false;
,否则真正的回报;
}
返回true唯一的一次是当点2
是withing圆。我在做什么错了?
解决方案
浮D = p1.X * p2.Y * p2.X - p1.Y;
您已经混了你的运营商在这条线。
I wrote this circle-line intersection detection after http://mathworld.wolfram.com/Circle-LineIntersection.html, but it appears like it or I am missing something.
public static bool Intersect
(Vector2f CirclePos, float CircleRad, Vector2f Point1, Vector2f Point2)
{
Vector2f p1 = Vector2f.MemCpy(Point1);
Vector2f p2 = Vector2f.MemCpy(Point2);
// Normalize points
p1.X -= CirclePos.X;
p1.Y -= CirclePos.Y;
p2.X -= CirclePos.X;
p2.Y -= CirclePos.Y;
float dx = p2.X - p1.X;
float dy = p2.Y - p1.Y;
float dr = (float)Math.Sqrt((double)(dx * dx) + (double)(dy * dy));
float D = p1.X * p2.Y * p2.X - p1.Y;
float di = (CircleRad * CircleRad) * (dr * dr) - (D * D);
if (di < 0) return false;
else return true;
}
The only occasion it returns true is when Point2
is withing the circle. What am I doing wrong?
解决方案
float D = p1.X * p2.Y * p2.X - p1.Y;
You've mixed up your operators on this line.
这篇关于圈 - 线的交点不正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文