slothfulwave612
slothfulwave612

Reputation: 1409

Find coordinates inside a circle to form a triangle

I have the following given problem: I had a point outside the circle(the orange one) which is at (2,3) and based on a mathematical formula I found out the closest coordinate to the orange point in the circle(which is the blue point).

How can I now draw an equilateral triangle, with the blue point being one of the triangle vertexes, and length of each side is 0.02 units? The coordinate for the blue point is (3.505025253169417, 1.4949747468305832), the radius of the circle is 0.7

To make the required triangle I need the other two vertex coordinates? How to find them.

Can somebody help me in writing down pseudocode for this problem?

Upvotes: 2

Views: 273

Answers (1)

JohanC
JohanC

Reputation: 80299

The height of an equilateral triangle is sqrt(3)/2 times its side length. Normalize the distance b-a to get a unit vector in the same direction. Adding it to b will be the center of the base. Exchanging the x and y of c and changing one sign, gets a perpendicular vector. Adding and subtracting this vector results in the base of the triangle.

from matplotlib import pyplot as plt
import numpy as np

a = np.array([2, 3])  # orange point
b = np.array([3.505025253169417, 1.4949747468305832])  # blue point
side = 0.2
height = side * np.sqrt(3) / 2
c = (b - a) / np.linalg.norm(b - a) * height
d = np.array([c[1], - c[0]]) / height * side / 2
e = b + c + d
f = b + c - d

plt.scatter(*b)
plt.scatter(*a)
plt.scatter(*(b + c), alpha=0.4)
plt.scatter(*e)
plt.scatter(*f)
triangle = np.array([b, e, f, b])
plt.plot(triangle[:, 0], triangle[:, 1], color='gold')
plt.fill(triangle[:, 0], triangle[:, 1], color='gold', alpha=0.3)
plt.gca().set_aspect('equal')
plt.show()

resulting plot

Upvotes: 3

Related Questions