Home > Enterprise >  How to calculate coordinates of tangent points?
How to calculate coordinates of tangent points?

Time:01-30

I need to make a svg file for a project and I need some parameters that I haven't figured out how to calculate yet. I have a point of coordinates x1,y1 and a circumference with a center of coordinates x2,y2 with radius r. The point x1,y1 is outside the circumference. How do I calculate the coordinates of the points belonging to the circumference (x3,y3 and x4,y4) from which the two tangent lines would pass? The outer point (x1,y1) will never touch the circumference and will never belong to the circumference. This is the drawing to make the concept better understood, in red the values to be calculated. Tangents scheme

CodePudding user response:

Shift coordinate system to make origin in circle center (to get simpler equations). Now point is

 x1' = x1 - x2
 y1' = y1 - y2

Solve the next equation system (point belongs to circumference and radius is perpendicular to tangent)

x^2   y^2 = r^2
(x - x1') * x   (y - y1') * y = 0

for unknown x, y.

To get final result, add x2, y2 to solution results (should be two solutions)

import math
def tangpts(px, py, cx, cy, r):
    px -= cx
    py -= cy
    r2 = r*r
    r4 = r2*r2
    a = px*px py*py
    b = -2*r2*px
    c = r4 - r2*py*py
    d = b*b-4*a*c
    if d < 0:
        return None
    d = math.sqrt(d)
    xx1 = (-b - d) / (2*a)
    xx2 = (-b   d) / (2*a)
    if (abs(py) > 1.0e-8):
        yy1 = (r2 - px * xx1) / py
        yy2 = (r2 - px * xx2) / py
    else:
        yy1 = math.sqrt(r2 - xx1*xx1)
        yy2 = -yy1

    return((xx1 cx,yy1 cy),(xx2 cx,yy2 cy))

print(tangpts(0.5, 0.5, 0, 0, 1))
print(tangpts(1, 1, 0, 0, 1))
print(tangpts(0, 0, -3, -3, 3))
print(tangpts(2, 0, 0, 0, 1))
print(tangpts(0, 1, 0, 0, 1))

>>>
None             #point inside 
((0.0, 1.0), (1.0, 0.0))    #common case
((-3.0, 0.0), (0.0, -3.0))   #common case
((0.5, 0.8660254037844386), (0.5, -0.8660254037844386))   #py is zero case
((0.0, 1.0), (0.0, 1.0))  # single tangent case - point at circumference

CodePudding user response:

In order to post the python code for the solution, I'm copying the explanation originally in comments:

  1. The center of the circle is P2(x2, y2), the radius is r. The unknown point P3(x3, y3) satisfies the equation of the circle:

    (x3-x2)^2 (y3-y2)^2 = r^2 (1).

  2. The tangent P1P3 is perpendicular to the radius of the circle P2P3. So apply the Pythagorean theorem to the triangle P1P2P3:

    a) the distance between P1 and P2 is (x1-x2)^2 (y1-y2)^2,

    b) the distance between P1 and P3 is (x1-x3)^2 (y1-y3)^2

    c) the distance P2P3 is r, the radius

    (x1-x3)^2 (y1-y3)^2 r^2 = (x1-x2)^2 (y1-y2)^2 (2)

We have thus to solve the equations (1) and (2) for x3 and y3.

We now separate the unknowns (a linear relation between x3 and y3 can be obtained by (1)-(2) => (x3-x2)(x1-x2) (y3-y2)(y1-y2) = r^2), and we get the two equations of second degree.

The python implementation:

import math
def tangentPoints(x1, y1, x2, y2, r):
  a = (y1-y2)**2 (x1-x2)**2
  bx = -r**2 * (x1-x2)
  cx = r**2 * (r**2-(y1-y2)**2)
  sqDeltax = math.sqrt(bx**2 - a*cx)
  x3 = x2   (-bx   sqDeltax)/a
  x4 = x2   (-bx - sqDeltax)/a
  
  by = -r**2 * (y1-y2)
  cy = r**2 * (r**2 - (x1-x2)**2)
  sqDeltay = math.sqrt(by**2 - a*cy)
  y3 = y2   (-by - sqDeltay)/a
  y4 = y2   (-by   sqDeltay)/a

  return (x3, y3), (x4, y4)
  • Related