📜  找到从 2D 平面中的点到直线的垂直脚

📅  最后修改于: 2021-10-23 08:20:30             🧑  作者: Mango

给定二维平面中的一个点 P 和一条直线的方程,任务是找到从 P 到直线的垂线的底脚。
:直线方程的形式为 ax+by+c=0。
例子:

Input :  P=(1, 0), a = -1, b = 1, c = 0
Output : Q = (0.5, 0.5)
The foot of perpendicular from point (1, 0) 
to line -x + y = 0 is (0.5, 0.5)

Input :  P=(3, 3), a = 0, b = 1, c = -2
Output : Q = (3, 2)
The foot of perpendicular from point (3, 3) 
to line y-2 = 0 is (3, 2)

由于直线方程的形式为ax + by + c = 0 。通过 P 且与直线垂直的直线方程。因此,通过 P 和 Q 的线方程变为ay – bx + d = 0 。 P 也经过 P 和 Q 的直线,所以我们把 P 的坐标放在上面的等式中:

ay1 - bx1 + d = 0 
or, d = bx1 - ay1

此外,Q 是给定线与通过 P 和 Q 的线的交点。因此我们可以找到以下解决方案:

ax + by + c = 0
and,
ay - bx + (bx1-ay1) = 0

由于 a、b、c、d 都是已知的,我们可以在这里找到 x 和 y:

下面是上述方法的实现:

C++
// C++ program for implementation of
// the above approach
#include 
using namespace std;
 
// Function to find foot of perpendicular from
// a point in 2 D plane to a Line
pair findFoot(double a, double b, double c,
                              double x1, double y1)
{
    double temp = -1 * (a * x1 + b * y1 + c) / (a * a + b * b);
    double x = temp * a + x1;
    double y = temp * b + y1;
    return make_pair(x, y);
}
 
// Driver Code
int main()
{
    // Equation of line is
    // ax + by + c = 0
    double a = 0.0;
    double b = 1.0;
    double c = -2;
 
    // Coordinates of point p(x1, y1).
    double x1 = 3.0;
    double y1 = 3.0;
 
    pair foot = findFoot(a, b, c, x1, y1);
    cout << foot.first << " " << foot.second;
 
    return 0;
}


Java
import javafx.util.Pair;
 
// Java program for implementation of
// the above approach
class GFG
{
 
// Function to find foot of perpendicular from
// a point in 2 D plane to a Line
static Pair findFoot(double a, double b, double c,
                            double x1, double y1)
{
    double temp = -1 * (a * x1 + b * y1 + c) / (a * a + b * b);
    double x = temp * a + x1;
    double y = temp * b + y1;
    return new Pair(x, y);
}
 
// Driver Code
public static void main(String[] args)
{
    // Equation of line is
    // ax + by + c = 0
    double a = 0.0;
    double b = 1.0;
    double c = -2;
 
    // Coordinates of point p(x1, y1).
    double x1 = 3.0;
    double y1 = 3.0;
 
    Pair foot = findFoot(a, b, c, x1, y1);
    System.out.println(foot.getKey() + " " + foot.getValue());
    }
}
 
// This code contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
 
# Function to find foot of perpendicular
# from a point in 2 D plane to a Line
def findFoot(a, b, c, x1, y1):
 
    temp = (-1 * (a * x1 + b * y1 + c) //
                  (a * a + b * b))
    x = temp * a + x1
    y = temp * b + y1
    return (x, y)
 
# Driver Code
if __name__ == "__main__":
 
    # Equation of line is
    # ax + by + c = 0
    a, b, c = 0.0, 1.0, -2
     
    # Coordinates of point p(x1, y1).
    x1, y1 = 3.0, 3.0
 
    foot = findFoot(a, b, c, x1, y1)
    print(int(foot[0]), int(foot[1]))
         
# This code is contributed
# by Rituraj Jain


C#
// C# program for implementation of
// the above approach
using System;
 
class GFG
{
    // Pair class
    public class Pair
    {
        public double first,second;
        public Pair(double a,double b)
        {
            first = a;
            second = b;
        }
    }
 
// Function to find foot of perpendicular from
// a point in 2 D plane to a Line
static Pair findFoot(double a, double b, double c,
                            double x1, double y1)
{
    double temp = -1 * (a * x1 + b * y1 + c) / (a * a + b * b);
    double x = temp * a + x1;
    double y = temp * b + y1;
    return new Pair(x, y);
}
 
// Driver Code
public static void Main(String []args)
{
    // Equation of line is
    // ax + by + c = 0
    double a = 0.0;
    double b = 1.0;
    double c = -2;
 
    // Coordinates of point p(x1, y1).
    double x1 = 3.0;
    double y1 = 3.0;
 
    Pair foot = findFoot(a, b, c, x1, y1);
    Console.WriteLine(foot.first + " " + foot.second);
    }
}
 
// This code contributed by Arnab Kundu


PHP


Javascript


输出:
3 2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程