Skip to content

S1Q1 · Position of a Point Relative to a Line

⚡ Quick Reference

Function: point_position_relative_to_line(a, b, c, x, y) -> int

Core idea: evaluate ax + by + c. Return its sign: +1, -1, or 0.

def point_position_relative_to_line(a, b, c, x, y) -> int:
    val = a * x + b * y + c
    if val > 0:
        return 1
    elif val < 0:
        return -1
    return 0

Key rules: - Compute val = ax + by + c - val > 0+1 (above) - val < 0-1 (below) - val == 00 (on the line)


Problem Statement

Problem

Write a function point_position_relative_to_line(a, b, c, x, y) that returns +1 if point (x, y) is above the line ax + by + c = 0, -1 if below, and 0 if on the line.

Examples:

Input
a=1, b=-1, c=0, x=2, y=1
Output
1
Input
a=-1, b=-1, c=-1, x=0, y=0
Output
-1
Input
a=2, b=-1, c=-4, x=2, y=0
Output
0

Understanding the problem

Substituting the point (x, y) into the line equation ax + by + c gives a single value. The sign of this value determines which side of the line the point is on:

val = ax + by + c

val > 0  →  point is above  → return +1
val = 0  →  point is on     → return  0
val < 0  →  point is below  → return -1

The sign function

This is essentially the mathematical sign(val) function. Python doesn't have a built-in sign(), but (val > 0) - (val < 0) is an elegant one-liner that evaluates to +1, 0, or -1 using boolean arithmetic.


Tracing all examples

Example 1: a=1, b=-1, c=0, x=2, y=1

val = 1(2) + (-1)(1) + 0 = 2 - 1 = 1 > 0  →  +1 

Example 2: a=-1, b=-1, c=-1, x=0, y=0

val = -1(0) + (-1)(0) + (-1) = 0 + 0 - 1 = -1 < 0  →  -1 

Example 3: a=2, b=-1, c=-4, x=2, y=0

val = 2(2) + (-1)(0) + (-4) = 4 + 0 - 4 = 0  →  0 


Solution approaches

def point_position_relative_to_line(a, b, c, x, y) -> int:
    val = a * x + b * y + c
    if val > 0:
        return 1
    elif val < 0:
        return -1
    else:
        return 0

Compute the value, then check each case explicitly. Most readable.

def point_position_relative_to_line(a, b, c, x, y) -> int:
    val = a * x + b * y + c
    return (val > 0) - (val < 0)

(val > 0) is True (= 1) when positive, False (= 0) otherwise. (val < 0) is True (= 1) when negative. Subtracting gives 1 - 0 = 1, 0 - 1 = -1, or 0 - 0 = 0. Clean one-liner.

import math

def point_position_relative_to_line(a, b, c, x, y) -> int:
    val = a * x + b * y + c
    if val == 0:
        return 0
    return int(math.copysign(1, val))

math.copysign(1, val) returns 1.0 or -1.0 with the sign of val. Cast to int for the required return type. Handles the zero case separately since copysign doesn't return 0.

point_position_relative_to_line = lambda a, b, c, x, y: (
    (lambda val: (val > 0) - (val < 0))(a * x + b * y + c)
)

The entire function as a lambda. An inner lambda computes the sign of the evaluated expression. Compact but less readable than the def form.


Key takeaways

01

(val > 0) - (val < 0) -the sign trick

Python booleans are integers: True == 1, False == 0. Subtracting two boolean comparisons gives the sign of any number in one expression -no if-else needed.

02

Substitute first, then classify

Don't try to reason about the geometry directly. Just compute ax + by + c and check its sign. The math reduces to a single number and a comparison.

03

Zero check before sign comparison

Always check val == 0 before classifying as positive or negative. Floating-point inputs might need a tolerance check (abs(val) < 1e-9), but for integer inputs exact zero works fine.