Ask Your Question
1

What is the distance between a point and a line in Python?

asked 2022-10-02 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-01-21 20:00:00 +0000

djk gravatar image

To calculate the distance between a point and a line in Python, you can use the following formula:

distance = abs((a*x + b*y + c) / (sqrt(a*a + b*b)))

where x and y are the coordinates of the point, and a, b, and c are the coefficients of the line in the standard form (ax + by + c = 0).

Here is an example Python code that implements this formula:

from math import sqrt

def distance_point_line(x, y, a, b, c):
    distance = abs((a*x + b*y + c) / sqrt(a*a + b*b))
    return distance

You can then call this function with your own values for the point coordinates and line coefficients.

For example:

# Suppose we have the point (2, 3) and the line 3x + 4y - 12 = 0
x = 2
y = 3
a = 3
b = 4
c = -12

distance = distance_point_line(x, y, a, b, c)
print(distance)  # Output: 1.8

This means that the distance between the point (2, 3) and the line 3x + 4y - 12 = 0 is approximately 1.8 units.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-10-02 11:00:00 +0000

Seen: 7 times

Last updated: Jan 21 '22