CSES Solutions - Point Location Test
Last Updated :
22 Apr, 2024
There is a line that goes through the points p1 = (x1,y1) and p2 = (x2,y2). There is also a point p3 = (x3,y3).
Your task is to determine whether p3 is located on the left or right side of the line or if it touches the line when we are looking from p1 to p2.
Example:
Input: x1 = 1, y1 = 1, x2 = 5, y2 = 3, x3 = 2, y3 = 3
Output: LEFT
Input: x1 = 1, y1 = 1, x2 = 5, y2 = 3, x3 = 4, y3 = 1
Output: RIGHT
Approach:
The cross product of two vectors a and b is a vector that is perpendicular to both a and b and thus normal to the plane containing them. The direction of the cross product vector is given by the right-hand rule, and its magnitude is equal to the area of the parallelogram that the vectors span.
But for this problem, we are not interested in the resulting vector itself, but rather the sign of its z-component (since we are working in 2D space, this is the only component of the cross product). The sign of the z-component of the cross product tells us whether p3 is to the left or right of the line from p1 to p2:
- If the cross product is positive, p3 is on the left side of the line.
- If the cross product is negative, p3 is on the right side of the line.
- If the cross product is zero, p3 is on the line.
Steps to solve the problem:
- Calculate the cross product using the formula: (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1).
- If the cross product is less than 0, then point p3 is on the right side of the line passing through p1 and p2. Print "RIGHT".
- If the cross product is greater than 0, then point p3 is on the left side of the line passing through p1 and p2. Print "LEFT".
- If the cross product is equal to 0, then point p3 lies on the line passing through p1 and p2. Print "TOUCH".
Below are the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Coordinates of points p1, p2, and p3
long long x1 = 1, y1 = 1, x2 = 5, y2 = 3, x3 = 2, y3 = 3;
// Calculate the cross product to determine the position of p3 relative to the line through p1 and p2
long long crossProduct = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);
// If cross product is less than 0, p3 is on the right of the line
if (crossProduct < 0)
cout << "RIGHT" << endl;
// If cross product is more than 0, p3 is on the left of the line
else if (crossProduct > 0)
cout << "LEFT" << endl;
// If cross product is 0, p3 is on the line
else
cout << "TOUCH" << endl;
return 0;
}
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
// Coordinates of points p1, p2, and p3
long x1 = 1, y1 = 1, x2 = 5, y2 = 3, x3 = 2, y3 = 3;
// Calculate the cross product to determine the position of p3 relative to the line through p1 and p2
long crossProduct = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);
// If cross product is less than 0, p3 is on the right of the line
if (crossProduct < 0)
System.out.println("RIGHT");
// If cross product is more than 0, p3 is on the left of the line
else if (crossProduct > 0)
System.out.println("LEFT");
// If cross product is 0, p3 is on the line
else
System.out.println("TOUCH");
}
}
Python3
# Python code to determine the position of a point relative to a line
# Coordinates of points p1, p2, and p3
x1, y1 = 1, 1
x2, y2 = 5, 3
x3, y3 = 2, 3
# Calculate the cross product to determine the position of p3 relative to the line through p1 and p2
crossProduct = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)
# If cross product is less than 0, p3 is on the right of the line
if crossProduct < 0:
print("RIGHT")
# If cross product is more than 0, p3 is on the left of the line
elif crossProduct > 0:
print("LEFT")
# If cross product is 0, p3 is on the line
else:
print("TOUCH")
JavaScript
function main() {
// Coordinates of points p1, p2, and p3
let x1 = 1, y1 = 1, x2 = 5, y2 = 3, x3 = 2, y3 = 3;
// Calculate the cross product to determine the position of p3 relative to the line through p1 and p2
let crossProduct = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);
// If cross product is less than 0, p3 is on the right of the line
if (crossProduct < 0)
console.log("RIGHT");
// If cross product is more than 0, p3 is on the left of the line
else if (crossProduct > 0)
console.log("LEFT");
// If cross product is 0, p3 is on the line
else
console.log("TOUCH");
}
// Call the main function
main();
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
CSES Solutions - Point in Polygon You are given a polygon of N vertices and a list of M points. Your task is to determine for each point if it is inside, outside or on the boundary of the polygon. The polygon consists of n vertices (x1,y1),(x2,y2),...,(xn,yn). The vertices (xi,yi) and (xi+1,yi+1) are adjacent for i=1,2,...,n-1, and
10 min read
CSES Solution - Line Segment Intersection There are two line segments: the first goes through the points (x1,y1) and (x2,y2), and the second goes through the points (x3,y3) and (x4,y4). Your task is to determine if the line segments intersect, i.e., they have at least one common point. Example: Input: points = {1, 1}, {5, 3}, {1, 2}, {4, 3}
9 min read
Point in Polygon in C++ Point in Polygon problem is a classic computational geometry problem where the task is to determine whether a given point lies inside, outside, or on the boundary of a polygon, the point that lies on the border is considered as inside only. In this article, we will learn how to check if a given poin
10 min read
Point in Polygon in C In computational geometry, we come across a problem to determine whether a given point lies inside, outside, or on the boundary of a polygon. This problem, known as the "Point in Polygon" (PIP) problem. It has numerous applications in computer graphics, geographic information systems (GIS), and robo
12 min read
Line Intersection in C++ Line Intersection in C++ is a problem that involves finding the point of intersection of two lines. For given points X and Y that corresponds to line XY and points A and B that corresponds to line AB the task is to find whether two lines (XY and AB) intersect and find the intersection point if they
4 min read