开发者

Formula to determine if a line segment intersects a circle (flat)

Apologies if this is considered a repeat question, but the answers I've seen on here are too complex for my needs.

I simply need to find out i开发者_如何学Pythonf a line segment intersects a circle. I don't need to find the distance to the line from the circle center, I don't need to solve for the points of intersection.

The reason I need something simple is that I have to code this in SQL and am unable to call out to external libraries, and need to write this formula in a WHERE clause... basicaly it has to be done in a single statement that I can plug values in to.

Assuming 2 points A (Ax,Ay) and B (Bx,By) to describe the line segment, and a circle with center point C (Cx,Cy) and radius R, the formula I am currently using is:

( RR ( (Ax-Bx)(Ax-Bx) + (Ay-By)(Ay-By) ) ) -( ((Ax-Cx)(By-Cy))-((Bx-Cx)(Ay-Cy)) ) > 0

This formula is taken from link text, and is based on a 0,0 centered circle.

The reason I am posting is that I am getting weird results and I wondered if I did something stupid. :(


although this doesn't exactly answer your question: Do you really have to calculate this on the fly on a SQL-Select? This means that the DB-system has to calculate the formula for every single row in the table (or every single row for which the remaining where conditions hold, respectively) which might result in bad performance.

Instead, you might consider creating a separate boolean column and calculate its value in an on-insert/on-update trigger. There, in turn, you wouldn't even need to put the test in a single line formula. Using a separate column has another advantage: You can create an index on that column which allows you to get your set of intersecting/non-intersecting records very fast.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜