VOOZH about

URL: https://www.geeksforgeeks.org/dsa/basic-geometry-for-competitive-programming/

⇱ Basic Geometry for Competitive Programming - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Basic Geometry for Competitive Programming

Last Updated : 23 Jul, 2025

Ever wondered how to tackle tricky problems in competitive programming? Well, basic geometry is your secret weapon! In this article, we're diving into the basics Geometric Algorithms. It's like a power-up for your problem-solving skills, giving you the tools to crack those coding challenges like a pro.

Geometric algorithms are a set of computational techniques used in Competitive Programming to solve problems related to geometry and spatial relationships. These problems often involve points, lines, polygons, and other geometric objects.

There are several questions which require basic geometric algorithms like:

  1. Vector Addition/Subtraction
  2. Dot Product and Cross Product
  3. Distance of a point from a line
  4. Intersection of Lines
  5. Intersection of Planes, etc.

Operations like addition and subtraction of two vectors can be simply done by performing the operation on the individual components of the vectors. Like if we have two vectors (x1, y1) and (x2, y2), then the sum of the two vectors is (x1+x2, y1+y2) and the difference between them is (x1-x2, y1-y2).

Implementation:


Output
Vector Addition Result: 6 6 6 6 6 
Vector Subtraction Result: -4 -2 0 2 4 

The dot product of two vectors is simply the sum of the products of the corresponding elements. If we have two vectors (x1, y1) and (x2, y2), then the dot product of the two vectors is (x1 * x2) + (y1 * y2). The dot product of two vectors is a scalar quantity.

If we have two vectors in x-y plane (x1, y1) and (x2, y2), then the magnitude of cross product of two vectors is (x1 * y2) - (x2 * y1) and direction is in ±z direction. The cross product of two vectors is a vector quantity. Similarly, the cross product of two 3D vectors (x1, y1, z1) and (x2, y2, z2) is (y1*z2 - y2*z1, z1*x2 - z2*x1, x1*y2 - x2*y1).

Implementation:


Output
Dot Product: 32
Cross Product: -3 6 -3 

Suppose we are given point A and a line L and we need to calculate the distance between the point and the line (say h), we can simply do it using Cross Product of vectors.

👁 linePointDistance

Let's say we take 2 points on the line L, say B and C so the distance between the point and the line would simply be:

where the numerator is the magnitude of Cross Product of vector B to A and B to C and the denominator is the magnitude of vector B to C.

From the above diagram, we can see that the area of the parallelogram ABCD =

Also, we know that area of a parallelogram = base * height =

So, using the above two equations, we can calculate the height h,

Note: We can use the same formula for 3D vectors also.

Implementation:


Output
2.16025

Time Complexity: O(1)
Auxiliary Space: O(1)

We can find the intersection point of two lines in 2D using parametric equations. Parametric equations are a way of representing a curve in terms of one or more parameters. For a line in 2D, we can use the following parametric equation:

=> r = a + td

where a is the starting point of the line, t is a real parameter, d is the direction vector for the line and r is a point on the line.

So, for the first line the parametric equation will be, r1 = a1 + t1d1 and for the second line the parametric equation will be, r2 = a2 + t2d2.We can further simplify the second equation:

=> r2 - a2 = t2d2

Taking Cross Product with d2 vector on both sides.
=> (r2 - a2) X d2 = 0

As the lines intersect at any point, the value of r1 and r2 will be same at that intersection point. So, by substituting the value of r1 in the above equation,
=> (a1 + t1d1 - a2) X d2 = 0
=> t1 = ((a2 - a1) X d2) / (d1 X d2)

Now, we can find the intersection point by putting the value of t1 into r1 = a1 + t1d1

Implementation:


Output
2.4 2.4

Click here to know about finding the intersection point using equation of lines.

Problem

Problem Link

Check if two line segments intersect

Practice Now

Line Passing through two points

Practice Now

Find the missing point of parallelogram

Practice Now

Comment