【问题标题】:Middlepoint on circle between 2 points两点之间圆上的中点
【发布时间】:2022-10-23 22:19:56
【问题描述】:

我正在尝试在 2 点之间的圆上找到中间点,pictorial drawing

有给定的半径、p1、p2 和圆的中点。

p1 和 p2 之间的距离是一个直径,我正在尝试编写 python 公式来返回这两个点之间的圆上的点。我知道这是一个相当愚蠢的问题,但我现在试着做 3 个小时,而我在网上能找到的只是这两点之间的距离。

我正在尝试找到 p3 的公式(如图所示)

这就是我到目前为止所做的:

import math

points = [[100, 200], [250, 350]]
midpoint = (int(((points[0][0] + points[1][0]) / 2)), int(((points[0][1] + points[1][1]) / 2)))
radius = int(math.sqrt(((points[1][0] - points[0][0])**2) + ((points[1][1] - points[0][1])**2))) // 2

# This below is wrong
print(int(midpoint[0] - math.sqrt((points[0][1] - midpoint[1]) ** 2)),
                 int(midpoint[1] - math.sqrt((points[0][0] - midpoint[1]) ** 2)))

【问题讨论】:

  • 你的具体问题是什么?
  • 已编辑的问题。我正在寻找位于圆圈上给出的 2 之间的第三点的公式。数学或 Python
  • 这似乎更像是一个数学问题而不是 Python 问题。

标签: python math geometry


【解决方案1】:

要在圆上生成点,请使用极坐标 (https://math.stackexchange.com/questions/154550/polar-equation-of-a-circle)

import math
import random

points = [[100, 200], [250, 350]]
midpoint = (int(((points[0][0] + points[1][0]) / 2)), int(((points[0][1] + points[1][1]) / 2)))
radius = int(math.sqrt(((points[1][0] - points[0][0])**2) + ((points[1][1] - points[0][1])**2))) // 2

angle = random.uniform(0, 2 * math.pi)
x_relative = radius * math.cos(angle)
y_relative = radius * math.sin(angle)

x = midpoint[0] + x_relative
y = midpoint[1] + y_relative

print(f"{x} {y}")

要在点之间的圆上找到中间点:

import math

points = [[100, 200], [250, 350]]
midpoint = (int(((points[0][0] + points[1][0]) / 2)), int(((points[0][1] + points[1][1]) / 2)))
radius = int(math.sqrt(((points[1][0] - points[0][0])**2) + ((points[1][1] - points[0][1])**2))) // 2

points_relative = [[points[0][0] - midpoint[0], points[0][1] - midpoint[1]], [points[1][0] - midpoint[0], points[1][1] - midpoint[1]]]

midpoint_points_relative = [[points_relative[0][1], - points_relative[0][0]], [points_relative[1][1], - points_relative[1][0]]]
midpoint_points = [[midpoint_points_relative[0][0] + midpoint[0], midpoint_points_relative[0][1] + midpoint[1]], [midpoint_points_relative[1][0] + midpoint[0], midpoint_points_relative[1][1] + midpoint[1]]]

print(points)
print(midpoint)
print(points_relative)

print(midpoint_points)

您可以使用此页面测试结果:https://www.desmos.com/calculator/mhq4hsncnh

【讨论】:

  • 非常感谢,真的帮助了我并推动了我的项目
【解决方案2】:

不需要三角函数,这太过分了。

圆的中心是Xs= (X0 + X1) / 2Ys= (Y0 + Y1) / 2

两个相反的“中间点”由X3 = Xs - (Y1 - Ys)Y3 = Ys + (X1 - Xs)X4 = Xs + (Y1 - Ys)Y4 = Ys - (X1 - Xs) 给出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多