Point Location Test¶
- Title:
Point Location Test - Judge / source:
CSES - Original URL: https://cses.fi/problemset/task/2189
- Secondary topics:
Cross product sign,Directed line side test,Integer geometry - Difficulty:
easy - Subtype:
Classify a point as LEFT / RIGHT / TOUCH relative to a directed segment - Status:
solved - Solution file: pointlocationtest.cpp
Why Practice This¶
This is the smallest useful geometry predicate in the whole track:
- one cross product decides the answer
- the sign convention matters more than the arithmetic
- the same primitive reappears in segment intersection, convex hull, and polygon work
If this test feels automatic, a lot of later geometry gets much less scary.
Recognition Cue¶
Reach for orientation when:
- the task asks which side of a directed line or segment a point lies on
- only the sign matters, not the exact distance
- later geometry primitives will be built on top of the same predicate
The strongest smell is:
LEFT / RIGHT / TOUCH
That is almost a direct request for one cross-product sign.
Problem-Specific Transformation¶
The statement sounds like point classification, but the reusable rewrite is:
- build the vectors
b-aandp-a - compute one determinant
- interpret only its sign
So the whole problem is not about segments or distances. It is just:
- positive determinant -> left turn
- negative determinant -> right turn
- zero determinant -> collinear
Core Idea¶
For points a, b, and p, look at the directed segment a -> b.
Compute:
cross((b - a), (p - a))
Equivalently:
(b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x)
Interpret the sign:
- positive:
pis to theLEFT - negative:
pis to theRIGHT - zero:
pis collinear, so printTOUCH
This is exactly the orientation test for the ordered triple (a, b, p).
Complexity¶
- one query:
O(1) - total for
tqueries:O(t)
The whole problem is just a reliable primitive.
Pitfalls / Judge Notes¶
- The direction matters:
a -> bandb -> aflipLEFTandRIGHT. TOUCHhere means collinear with the infinite line throughaandb; the point does not need to lie between them.- With coordinates around
1e9, use__int128for the cross-product intermediate to stay comfortably safe. - No floating point or epsilon is needed.
Reusable Pattern¶
- Topic page: Vector And Orientation
- Practice ladder: Vector And Orientation ladder
- Starter template: geometry-primitives.cpp
- Notebook refresher: Geometry cheatsheet
- Carry forward: the sign of one cross product is the first geometry fact to trust automatically.
- This note adds: the exact classification or predicate layered on top of the orientation primitive.
Solutions¶
- Code: pointlocationtest.cpp