Closest point on a circle to a line segment python(圆上最接近线段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_x
、point1_y
和point2_x
、point2_y<的线段/代码>
- 我有一个 circle,中心为
circle_x
、circle_y
和 radiusradius
- I have a line segment of two points
point1_x
,point1_y
andpoint2_x
,point2_y
- I have a circle with center
circle_x
,circle_y
and radiusradius
是否有一个可以开箱即用的 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的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:圆上最接近线段python的点
基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01