![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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)
Advertisements