1

I need to find the closest distance between a box and a circle, however, I realize this can be broken up into the closest distance between a line segment and a circle.

Given

  • I have a line segment of two points point1_x,point1_y and point2_x, point2_y
  • I have a circle with center circle_x, circle_y and radius radius

Question

Is there a python library that will support this out of the box and if not can somebody present a function to do so?

(I belive I have to locate the tangent point on the circle with the same slope as the line?)

Community
  • 1
  • 1
Jeef
  • 26,861
  • 21
  • 78
  • 156
  • There is no such thing as "closest distance" (more strictly: you really don't need to introduce such a notion). Your problem is defining the distance between a segment and a circle, which is not a programming question. Please read some more about geometry. This question is off-topic here. – BartoszKP Dec 23 '14 at 01:36
  • 1
    @BartoszKP I don't know if it's necessarily off-topic... Even simple problems in geometry can become complicated when introduced to the world of finite precision computation, so solving the problem in theory and solving it in code can mean two different things. So asking for a library which does some of the task might be appropriate. – jme Dec 23 '14 at 01:46
  • [SymPy](http://docs.sympy.org/dev/index.html) can help with checking for intersection between line and circle. If no intersection then you need to check distance from center of circle to point1, point2 and the perpendicular (if on line) – softwarenewbie7331 Dec 23 '14 at 02:01
  • Actually there are two tangent points on the circle with the same slope as the line, so you'll have to figure out which one is closer to the line. – martineau Dec 23 '14 at 03:40
  • @jme But OP didn't "introduce it to the world of finite precision computation", so he asked a question about a basic geometrical problem, not about *programming*. And not only that - the question lacks any information about OP's research on the topic. And not only that - OP is asking for a "Python library" to do that. So, this question is not only off-topic, it's off-topic in three possible ways. – BartoszKP Dec 23 '14 at 11:50
  • @BartoszKP you are unhelpful. There are two great answers here so obviously people were able to understand the question. – Jeef Dec 23 '14 at 16:50
  • @Jeef I didn't say that the question is hard to understand. I said that's it's off-topic, and explained why. Please refer to the [help] and note that I'm not the only one who voted to close the question. Sorry if my explanations are still unhelpful, but that's really all there is to it. – BartoszKP Dec 23 '14 at 17:31
  • @Jeef Also, not sure what is your definition of a "great answer", but surely I wouldn't call Delphi code as a response to a Python question a "great answer" :) – BartoszKP Dec 23 '14 at 17:38

2 Answers2

3

There exists a method to find the closest distance from circle to rectangle (axis-oriented here).
Rectangle sides divide plane into 9 pieces. We can find what piece (central, left-top, left etc) contains circle center, and calculate needed distance. Rectangle ABCD and circle center E:

enter image description here

Delphi code:

//returns closest distance from circle to rectangle
//0 if intersection or inclusion occurs
function CircleRectDistance(CX, CY, CR: Integer; RR: TRect): Double;
var
  wh, hh, dx, dy, t, SquaredDist: Double;
begin
  SquaredDist := 0;

  //halfwidth and halfheight
  wh := 0.5 * (RR.Right - RR.Left);
  hh := 0.5 * (RR.Bottom - RR.Top);

  //distances to rectangle center
  dx := CX - 0.5 * (RR.Left + RR.Right);
  dy := CY - 0.5 * (RR.Top + RR.Bottom);

  //rectangle sides divide plane to 9 parts,
  t := dx + wh;
  if t < 0 then
    SquaredDist := t * t
  else begin
    t := dx - wh;
    if t > 0 then
      SquaredDist := t * t
  end;
  t := dy + hh;
  if t < 0 then
    SquaredDist := SquaredDist + t * t
  else begin
    t := dy - hh;
    if t > 0 then
      SquaredDist := SquaredDist + t * t
  end;

  if SquaredDist < CR * CR  then
    Result := 0
  else
    Result := Sqrt(SquaredDist)- CR;
end;
MBo
  • 77,366
  • 5
  • 53
  • 86
2

The euclid module. Install with:

pip install euclid

Then use it like this:

>>> from euclid import *

>>> circ = Circle(Point2(3., 2.), 2.)
>>> line = Line2(Point2(0., 0.), Point2(-1., 1.))
>>> line.distance(circ)
1.5355339059327378
elyase
  • 39,479
  • 12
  • 112
  • 119