1 | initial version |
To draw a tangent line from a specified point to a circle using OpenCV, you can follow these steps:
Here's an example code snippet that draws a red tangent line from a specified point to the circle:
import cv2
import numpy as np
# Define the center and radius of the circle and the point
center = (100, 100)
radius = 50
point = (150, 140)
# Calculate the distance between the center of the circle and the specified point
distance = np.sqrt((point[0] - center[0])**2 + (point[1] - center[1])**2)
# Check if the specified point lies inside the circle or on the circumference of the circle
if distance <= radius:
print("The point lies inside or on the circle, cannot draw tangent line.")
else:
# Calculate the angle between the line joining the center of the circle and the specified point and the x-axis
theta = np.arctan2(point[1] - center[1], point[0] - center[0])
# Calculate the slope of the tangent line
m = -1 / np.tan(theta)
# Draw a red tangent line from the specified point to the circle
img = np.zeros((200, 200, 3), dtype=np.uint8)
cv2.circle(img, center, radius, (255, 255, 255), 1)
cv2.line(img, point, (point[0] + 100, point[1] + int(100 * m)), (0, 0, 255), 2)
cv2.imshow("Tangent Line", img)
cv2.waitKey(0)
cv2.destroyAllWindows()