Baćko Kaczor
Baćko Kaczor

Reputation: 5

Middlepoint on circle between 2 points

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

Upvotes: 0

Views: 414

Answers (2)

user1196549
user1196549

Reputation:

There is no need for trigonometry, which is total overkill.

The center of the circle is Xs= (X1 + X2) / 2, Ys= (Y1 + Y2) / 2.

The two opposite "middlepoints" are given by X3 = Xs - (Y1 - Ys), Y3 = Ys + (X1 - Xs) and X4 = Xs + (Y1 - Ys), Y4 = Ys - (X1 - Xs).

If you prefer direct formulas, X3 = (X1 + X2 - Y1 + Y2) / 2, Y3 = (X1 - X2 + Y1 + Y2) / 2...

Upvotes: 1

mlokos
mlokos

Reputation: 409

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

Upvotes: 0

Related Questions