简体   繁体   English

圆与轴对齐的矩形之间的交点

[英]Intersection between circle and axis aligned rectangle

I am writing a converter for 2D geometry data. 我正在编写2D几何数据的转换器。 One of the elements I have to convert is an arc. 我必须转换的元素之一是弧。 In the source system the arc is described with two axis aligned rectangles. 在源系统中,用两个轴对齐的矩形描述了弧。 The first rectangle is a boundingbox for the circle of which the arc is part of. 第一个矩形是圆弧的边界框。 The second rectangle is a boundingbox for the arc itself. 第二个矩形是弧本身的边界框。 The constraint is, that the arc's boundingbox must intersect two edges of the circle's boundingbox. 约束条件是,弧的边界框必须与圆的边界框的两个边相交。 I know the coordinates of the top left and bottom right points of each rectangle. 我知道每个矩形的左上角和右下角的坐标。

The target system describes an arc as follows: A rectangle is a bounding box around the circle of which the arc is part of. 目标系统对弧的描述如下:矩形是包围圆的边界框,圆是弧的一部分。 So far it is the same as above. 到目前为止,与上面相同。 Then I have two points designating the start and end points of the arc. 然后,我有两个点指定弧的起点和终点。 Both points must be exactly on the circle's "edge". 这两个点必须完全在圆的“边缘”上。

The question is: do you see an easy way to calculate the start and end-point of the arc? 问题是:您看到一种简单的方法来计算圆弧的起点和终点吗? I have already seen this question but it seems really complicated. 我已经看过这个问题,但看起来确实很复杂。 Perhaps there is an easier way to do this... 也许有一种更简单的方法可以执行此操作...

My thoughts so far: 到目前为止,我的想法是:

It seems I must calculate the intersections of the edges of the arc's boundingbox with the circle. 看来我必须计算弧的边界框的边缘与圆的交点。 Two cases are possible (due to the restrictions mentioned above): only one edge intersects with the circle or two edges intersect with the circle. 可能有两种情况(由于上述限制):只有一个边缘与圆相交,或者两个边缘与圆相交。 Additionally all other edges will always be outside of the circle. 此外,所有其他边缘将始终在圆的外部。

Update: the start and end points cut the circle in two parts. 更新:起点和终点将圆分为两部分。 Which of the two parts is draw as the arc, is determined by the order of the start and end points. 将两个部分中的哪一个绘制为圆弧,取决于起点和终点的顺序。 The arc is always drawn counter clockwise from start to end point. 从起点到终点始终总是逆时针绘制弧。

Call the outer box coordinates top , left , bottom and right . 将外框坐标称为topleftbottomright The centre and radius are then: 中心和半径为:

centre.x = (left + right)/2
centre.y = (top + bottom)/2
radius = (right - left)/2

From this we can write an equation that must be satisfied for all points on the circle perimeter: 由此,我们可以写出一个圆周上所有点都必须满足的方程:

(x - centre.x) * (x - centre.x) + (y - centre.y) * (y - centre.y) = radius * radius

The inner box coordinates give you possible values of x and y . 内部框坐标为您提供xy可能值。 Substitute one in at a time and solve as a standard quadratic equation . 一次替换一个,然后作为标准二次方程求解 If there is a real solution and the corresponding point is within the span of the inner box, that's a point. 如果有一个真正的解决方案,并且对应的点在内部框的范围之内,那就是一个点。 Given the constraints of the original data, you should find there are two points -- those are your answer. 考虑到原始数据的限制,您应该发现有两点-就是您的答案。

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

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