- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find foot of perpendicular from a point in 2D plane to a Line in C++

Consider we have a point P in 2D plane and equation of a line, the task is to find the foot of the perpendicular from P to the line.

The equation of the straight line is ax + by + c = 0. Equation of line passing through P and perpendicular to line. Equation of line passing through P and Q will be ay – bx + d = 0. Also P(x1, y1), and Q(x2, y2), so we put coordinate of P on the equation.

*ay 1−bx 1+d=0, so d=bx1−ay 1*

Also Q is the intersection of the given line and the line passing through P and Q, so we will find solution for these two equations.

*ax+by+c=0,∧ay−bx+(bx1−ay 1)=0*

As a, b, c, d all are known, we can find using this formula −

$$\frac{x-x_{1}}{a}=\frac{y-y_{1}}{b}=\frac{f-(ax_{1}+by_{1}+c)}{a^{2}+b^{2}}$$

## Example

#include<iostream> using namespace std; void getFootCoordinate(double a, double b, double c, double x1, double y1) { double p = -1 * (a * x1 + b * y1 + c) / (a * a + b * b); double x = p * a + x1; double y = p * b + y1; cout << "(" << x << ", " << y <<")"; } int main() { double a = 0.0; double b = 1.0; double c = -2; double x1 = 3.0; double y1 = 3.0; cout << "The coordinate is: "; getFootCoordinate(a, b, c, x1, y1); }

## Output

The coordinate is: (3, 2)

- Related Articles
- C++ Program to find out the moves to read a point from another point in a 2D plane
- Find mirror image of a point in 2-D plane in C++
- Program to find the mid-point of a line in C++
- Draw a line $l$. Draw a perpendicular to $l$ at any point on $l$. On this perpendicular choose a point $X$, $4 cm$ away from $l$. Through $X$, draw a line $m$ parallel to $l$.
- Draw line $l$. Take any point $P$ on the line. Using a set square, draw a line perpendicular to line $l$ at the point $P$.
- Show that of all line segments drawn from a given point not on it, the perpendicular line segment is the shortest.
- What is a perpendicular line?
- Swift Program to Find the Mid-point of a Line
- Find a peak element in a 2D array in C++
- Best meeting point in 2D binary array in C++
- Construct a linked list from 2D matrix in C++
- Program to find slope of a line in C++
- C# program to find K’th smallest element in a 2D array
- Draw a line $l$ and a point ( mathrm{X} ) on it. Through ( mathrm{X} ), draw a line segment ( overline{mathrm{XY}} ) perpendicular to $1$. Now draw a perpendicular to ( overline{X Y} ) at Y. (use ruler and compasses)
- Program to find equation of a plane passing through 3 points in C++

Advertisements