圆上最接近线段python的点

时间:2023-04-15
本文介绍了圆上最接近线段python的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要找到一个盒子和一个圆之间的最近距离,但是,我意识到这可以分解为一条线段和一个圆之间的最近距离.

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.

  • 我有两点point1_xpoint1_ypoint2_xpoint2_y<的线段/代码>
  • 我有一个 circle,中心为 circle_xcircle_yradius radius
  • 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

是否有一个可以开箱即用的 python 库,如果没有,有人可以提供一个函数来支持它吗?

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?)

推荐答案

有一种方法可以找到圆到矩形的最近距离(此处为轴方向).
矩形边将平面分成 9 块.我们可以找到包含圆心的部分(中心、左上、左等),并计算所需的距离.矩形ABCD和圆心E:

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:

德尔福代码:

//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;

这篇关于圆上最接近线段python的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!