Check whether the point (x, y) lies on a given line in Python

To check whether a point (x, y) lies on a given line, we need to verify if the point satisfies the line equation. For a line in the form y = mx + b, where m is the slope and b is the y-intercept, we substitute the point's coordinates into the equation.

If the equation holds true (left side equals right side), then the point lies on the line. Otherwise, it doesn't.

Problem Example

Given a line with slope m = 3 and y-intercept b = 5, we need to check if point (6, 23) lies on this line ?

The line equation is: y = 3x + 5

Substituting x = 6: y = 3(6) + 5 = 18 + 5 = 23

Since the calculated y-value (23) matches the given y-coordinate, the point lies on the line.

Algorithm

The steps to solve this problem are ?

  • Extract x and y coordinates from the given point
  • Calculate the expected y-value using the formula: y = mx + b
  • Compare the calculated y-value with the actual y-coordinate
  • Return True if they match, False otherwise

Implementation

def check_point_on_line(m, b, point):
    x, y = point
    expected_y = (m * x) + b
    return y == expected_y

# Test with the given example
m = 3
b = 5
point = (6, 23)

result = check_point_on_line(m, b, point)
print(f"Point {point} lies on line y = {m}x + {b}: {result}")
Point (6, 23) lies on line y = 3x + 5: True

Testing with Multiple Points

Let's test the function with several points to verify its correctness ?

def check_point_on_line(m, b, point):
    x, y = point
    expected_y = (m * x) + b
    return y == expected_y

# Line: y = 2x + 1
m = 2
b = 1

test_points = [(1, 3), (2, 5), (0, 1), (3, 6), (1, 4)]

for point in test_points:
    result = check_point_on_line(m, b, point)
    x, y = point
    expected = (m * x) + b
    print(f"Point {point}: Expected y = {expected}, Actual y = {y}, On line: {result}")
Point (1, 3): Expected y = 3, Actual y = 3, On line: True
Point (2, 5): Expected y = 5, Actual y = 5, On line: True
Point (0, 1): Expected y = 1, Actual y = 1, On line: True
Point (3, 6): Expected y = 7, Actual y = 6, On line: False
Point (1, 4): Expected y = 3, Actual y = 4, On line: False

Handling Edge Cases

The function can handle various scenarios including vertical and horizontal lines ?

def check_point_on_line(m, b, point):
    x, y = point
    expected_y = (m * x) + b
    return y == expected_y

# Horizontal line: y = 5 (m = 0, b = 5)
print("Horizontal line y = 5:")
result1 = check_point_on_line(0, 5, (10, 5))
result2 = check_point_on_line(0, 5, (100, 5))
result3 = check_point_on_line(0, 5, (0, 3))

print(f"Point (10, 5): {result1}")
print(f"Point (100, 5): {result2}")
print(f"Point (0, 3): {result3}")
Horizontal line y = 5:
Point (10, 5): True
Point (100, 5): True
Point (0, 3): False

Conclusion

Checking if a point lies on a line is straightforward: substitute the point's coordinates into the line equation y = mx + b and verify equality. This method works for all types of lines except vertical lines, which require a different approach since they cannot be expressed in slope-intercept form.

Updated on: 2026-03-25T14:41:55+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements