什么是一个很好的算法来检查线是否与圆相交?沿着圆周边缘发生什么坐标?圆和线段碰撞
Q
圆和线段碰撞
-3
A
回答
0
按照here的描述计算圆的中心和直线之间的距离。
检查最近的点是否在线段的端点之间。如果在它们之间,检查距离是否小于半径;否则检查其中一个边缘点是否在圆内。
0
使用this link的数学算法,这里是返回所有交点的函数。输入是圆的中心点和半径,以及高程m和y轴的偏移c。
添加到链接的数学解决方案我已添加代码为垂直行。对于这些集合m = float.PositiveInfinity;
和c = y-offset
(即该行的x坐标)!
List<PointF> Intersections(PointF cc, float r, float m, float c)
{
List<PointF> points = new List<PointF>();
// vertical lines:
if (float.IsPositiveInfinity(m))
{
float b = (float) Math.Sqrt(r * r + (c - cc.X) * (c - cc.X));
points.Add(new PointF(c, cc.Y + b));
points.Add(new PointF(c, cc.Y - b));
return points;
}
float A = (m * m + 1f);
float B = 2*(m * c - m * cc.Y - cc.X);
float C = cc.Y * cc.Y - r * r + cc.X * cc.X - 2f * c * cc.Y + c * c;
float disc = B * B - 4f * A * C;
if (disc < 0) return points;
float x1 = (float)(-B - Math.Sqrt(disc))/2f/A;
float y1 = m * x1 + c;
points.Add(new PointF(x1, y1));
if (disc != 0)
{
float x2 = (float)(-B + Math.Sqrt(disc))/2f/A;
float y2 = m * x2 + c;
points.Add(new PointF(x2, y2));
}
return points;
}
下面是线路的两个点形式的过载:
List<PointF> Intersections(PointF cc, float r, PointF p1, PointF p2)
{
float m = 0; float c = 0;
if (p1.X == p2.X) { m = float.PositiveInfinity; c = p1.X; }
else { m = (p2.Y - p1.Y)/(p2.X - p1.X);c = p1.Y - m * p1.X; }
return Intersections(cc, r, m, c);
}
下面是使用一个小的试验床的空Form
:
float radius = 200; PointF center = new PointF(480,360);
PointF pt1 = PointF.Empty; PointF pt2 = PointF.Empty;
List<PointF> pointsM = new List<PointF>();
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{ // drag around and watch the points
pt2 = e.Location;
pointsM = Intersections(center, radius, pt1, pt2, false);
Invalidate();
}
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{ // right-click to set the first point
if (e.Button == MouseButtons.Right) pt1 = e.Location;
Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawEllipse(Pens.Blue, center.X - radius, center.Y - radius,
radius * 2, radius * 2);
e.Graphics.DrawLine(Pens.Green, pt1, pt2);
e.Graphics.FillEllipse(Brushes.Blue, center.X - 4, center.Y - 4, 8, 8);
foreach (PointF pt in pointsM)
e.Graphics.FillEllipse(Brushes.DarkOrange, pt.X - 4, pt.Y - 4, 8, 8);
e.Graphics.FillEllipse(Brushes.Green, pt1.X - 4, pt1.Y - 4, 8, 8);
e.Graphics.FillEllipse(Brushes.DeepPink, pt2.X - 4, pt2.Y - 4, 8, 8);
Text = pointsM.Count + " intersection points.";
}
相关问题
- 1. 圆弧和线段碰撞检测
- 2. 角上的圆线段碰撞
- 3. 计算点和圆段碰撞
- 4. 圆碰撞Javascript
- 5. 圆圈碰撞
- 6. 圆和矩形碰撞
- 7. 球和椭圆碰撞
- 8. 碰撞与椭圆
- 9. 线终点上的球线段碰撞
- 10. 球线碰撞
- 11. 碰撞和团结碰撞
- 12. 圆圈碰撞问题
- 13. 冲击点:圆形碰撞
- 14. 两个圆圈的碰撞
- 15. 圆圈的碰撞检测
- 16. 圆圈碰撞预测
- 17. 框与圆碰撞AS3
- 18. 圆弧的碰撞检测
- 19. Java圆圈碰撞检测
- 20. 矩形和圆形之间的碰撞
- 21. 矩形和圆形碰撞检测
- 22. 圆和半径之间的碰撞
- 23. 圆和矩形的碰撞检测
- 24. 矩形和圆碰撞Java使用.intersection
- 25. 圆形和矩形碰撞Android
- 26. 圆形碰撞中的圆形
- 27. Matlab - 用于检测线段和圆之间的碰撞的函数失败
- 28. SpriteKit线条和圆的碰撞角度为奇数
- 29. 检查线和一个圆圈在帆布的碰撞android
- 30. python tkinter中的圆形和线条碰撞检测
你可以接通的方程这条线('ax + by = c')代入圆的方程。但我几乎不会说这是一种算法... – Glorfindel
我已经使用链接的代码来编写一个C#实现。 – TaW
你解决了你的问题吗? – TaW