Reputation: 1426
I have line-equation, point-A and distance, and need to find point-B at the edge of the distance, on the line. I have 2 equations:
distance = math.sqrt((pt1[0] - pt2[0])**2 + (pt1[1] - pt2[1])**2)
pt2[1] = m*pt2[0] + n
distance, pt1, m and n are known.
How can I implement this calculation in Python? Maybe there is a Python library that can do this for me?
Upvotes: 1
Views: 3769
Reputation: 3787
Given the line y=mx+b
, the slope m
tells us the ratio between the change in x (dx
) and the change in y (dy
).
Math:
// Given
point_b = (point_a[0]+dx,point_a[1]+dy)
other_possible_point_b = (point_a[0]-dx,point_a[1]-dy)
dy = m*dx
x**2 + y**2 = distance
// Calculations
dx**2 + (m*dx)**2 = distance
(m**2+1)(dx**2) = distance
dx = sqrt(distance/(m**2+1))
dy = m*sqrt(distance/(m**2+1))
Python solution:
from math import sqrt
point_b = (point_a[0]+dx(distance,m), point_a[1]+dy(distance,m))
other_possible_point_b = (point_a[0]-dx(distance,m), point_a[1]-dy(distance,m)) # going the other way
def dy(distance, m):
return m*dx(distance, m)
def dx(distance, m):
return sqrt(distance/(m**2+1))
Upvotes: 4
Reputation: 12715
I am assuming that point A is not on the line. ( Even if it is, the solution will not be different) You need to find the intersection between a circle and a line. It may have 1, 2 or 0 solutions.
(x1-x2)^2 + (y1-y2)^2 = d^2
y2 = m*x2 + c
The simplest solution would be: Replace y2 by (m*x2 + c) in the first equation and solve the quadratic for x2.
Upvotes: 1