Home > OS >  Middlepoint on circle between 2 points
Middlepoint on circle between 2 points

Time:10-23

I'm trying to find middlepoint on the circle between 2 points, pictorial drawing

There are given radius, p1, p2 and middle of the circle.

Distance betweeen p1 and p2 is an diameter, and I'm trying to make up python formula that returns point on the circle between those 2 points. I know this is rather silly question but I'm trying to make this for 3 hours now and all I can find on web is distance between those 2 points.

I'm trying to find formula for p3 (like in the picture)

That's what I ended up making so far:

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

CodePudding user response:

To generate points on the circle use polar equation (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}")

To find middle point on the circle between points:

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)

You can use this page to test results: https://www.desmos.com/calculator/mhq4hsncnh

  • Related