📜  3D 中一对线之间的角度

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

给定 3D 平面中三个点A(x1, y1, z1)、B(x2, y2, z2)和 C(x3, y3, z3)的坐标,其中B是线ABBC的交点,则任务是找到线AB 和 BC之间的角度。

例子:

方法:

1. 用给定坐标根据方向比求直线ABBC的方程为:

2. 对AB线和BC线的两个方向比使用cosθ的公式,求AB线和BC线夹角的余弦为:

3. 假设有两个方向比率:

*** QuickLaTeX cannot compile formula:
 

*** Error message:
Error: Nothing to show, formula is empty

然后

4. 计算出的角度的余弦给出了以弧度为单位的余弦值。要找到角度,请将余弦值乘以(180/ Π )

下面是上述方法的实现:

C++
A = ai + bj + ck
B = xi + yj + zk


Java
// C++ program for the above approach
 
#include "bits/stdc++.h"
#define PI 3.14
using namespace std;
 
// Function to find the angle between
// the two lines
void calculateAngle(
    int x1, int y1, int z1,
    int x2, int y2, int z2,
    int x3, int y3, int z3)
{
    // Find direction ratio of line AB
    int ABx = x1 - x2;
    int ABy = y1 - y2;
    int ABz = z1 - z2;
 
    // Find direction ratio of line BC
    int BCx = x3 - x2;
    int BCy = y3 - y2;
    int BCz = z3 - z2;
 
    // Find the dotProduct
    // of lines AB & BC
    double dotProduct
        = ABx * BCx
          + ABy * BCy
          + ABz * BCz;
 
    // Find magnitude of
    // line AB and BC
    double magnitudeAB
        = ABx * ABx
          + ABy * ABy
          + ABz * ABz;
    double magnitudeBC
        = BCx * BCx
          + BCy * BCy
          + BCz * BCz;
 
    // Find the cosine of
    // the angle formed
    // by line AB and BC
    double angle = dotProduct;
    angle /= sqrt(
        magnitudeAB * magnitudeBC);
 
    // Find angle in radian
    angle = (angle * 180) / PI;
 
    // Print the angle
    cout << abs(angle) << endl;
}
 
// Driver Code
int main()
{
 
    // Given coordinates
    // Points A
    int x1 = 1, y1 = 3, z1 = 3;
 
    // Points B
    int x2 = 3, y2 = 4, z2 = 5;
 
    // Points C
    int x3 = 5, y3 = 6, z3 = 9;
 
    // Function Call
    calculateAngle(x1, y1, z1,
                   x2, y2, z2,
                   x3, y3, z3);
 
    return 0;
}


Python3
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the angle
// between the two lines
static void calculateAngle(int x1, int y1, int z1,
                           int x2, int y2, int z2,
                           int x3, int y3, int z3)
{
     
    // Find direction ratio of line AB
    int ABx = x1 - x2;
    int ABy = y1 - y2;
    int ABz = z1 - z2;
 
    // Find direction ratio of line BC
    int BCx = x3 - x2;
    int BCy = y3 - y2;
    int BCz = z3 - z2;
 
    // Find the dotProduct
    // of lines AB & BC
    double dotProduct = ABx * BCx +
                        ABy * BCy +
                        ABz * BCz;
 
    // Find magnitude of
    // line AB and BC
    double magnitudeAB = ABx * ABx +
                         ABy * ABy +
                         ABz * ABz;
    double magnitudeBC = BCx * BCx +
                         BCy * BCy +
                         BCz * BCz;
 
    // Find the cosine of the
    // angle formed by line
    // AB and BC
    double angle = dotProduct;
    angle /= Math.sqrt(magnitudeAB * magnitudeBC);
 
    // Find angle in radian
    angle = (angle * 180) / 3.14;
 
    // Print the angle
    System.out.printf("%.4f", Math.abs(angle));
}
 
// Driver code
public static void main(String[] args)
{
 
    // Given coordinates
    // Points A
    int x1 = 1, y1 = 3, z1 = 3;
 
    // Points B
    int x2 = 3, y2 = 4, z2 = 5;
 
    // Points C
    int x3 = 5, y3 = 6, z3 = 9;
 
    // Function Call
    calculateAngle(x1, y1, z1,
                   x2, y2, z2,
                   x3, y3, z3);
}
}
 
// This code is contributed by offbeat


C#
# Python3 program for the above approach
import math
 
# Function to find the angle
# between the two lines
def calculateAngle(x1, y1, z1,
                   x2, y2, z2,
                   x3, y3, z3):
                        
    # Find direction ratio of line AB
    ABx = x1 - x2;
    ABy = y1 - y2;
    ABz = z1 - z2;
 
    # Find direction ratio of line BC
    BCx = x3 - x2;
    BCy = y3 - y2;
    BCz = z3 - z2;
 
    # Find the dotProduct
    # of lines AB & BC
    dotProduct = (ABx * BCx +
                  ABy * BCy +
                  ABz * BCz);
 
    # Find magnitude of
    # line AB and BC
    magnitudeAB = (ABx * ABx +
                   ABy * ABy +
                   ABz * ABz);
    magnitudeBC = (BCx * BCx +
                   BCy * BCy +
                   BCz * BCz);
 
    # Find the cosine of
    # the angle formed
    # by line AB and BC
    angle = dotProduct;
    angle /= math.sqrt(magnitudeAB *
                       magnitudeBC);
 
    # Find angle in radian
    angle = (angle * 180) / 3.14;
 
    # Print angle
    print(round(abs(angle), 4))
 
# Driver Code
if __name__=='__main__':
 
    # Given coordinates
    # Points A
    x1, y1, z1 = 1, 3, 3;
 
    # Points B
    x2, y2, z2 = 3, 4, 5;
 
    # Points C
    x3, y3, z3 = 5, 6, 9;
  
    # Function Call
    calculateAngle(x1, y1, z1,
                   x2, y2, z2,
                   x3, y3, z3);
 
# This code is contributed by AbhiThakur


Javascript
// C# program for the above approach
using System;
class GFG{
     
// Function to find the angle
// between the two lines
static void calculateAngle(int x1, int y1,
                           int z1, int x2,
                           int y2, int z2,
                           int x3, int y3,
                           int z3)
{
     
    // Find direction ratio of line AB
    int ABx = x1 - x2;
    int ABy = y1 - y2;
    int ABz = z1 - z2;
 
    // Find direction ratio of line BC
    int BCx = x3 - x2;
    int BCy = y3 - y2;
    int BCz = z3 - z2;
 
    // Find the dotProduct
    // of lines AB & BC
    double dotProduct = ABx * BCx +
                        ABy * BCy +
                        ABz * BCz;
 
    // Find magnitude of
    // line AB and BC
    double magnitudeAB = ABx * ABx +
                         ABy * ABy +
                         ABz * ABz;
    double magnitudeBC = BCx * BCx +
                         BCy * BCy +
                         BCz * BCz;
 
    // Find the cosine of the
    // angle formed by line
    // AB and BC
    double angle = dotProduct;
    angle /= Math.Sqrt(magnitudeAB *
                       magnitudeBC);
 
    // Find angle in radian
    angle = (angle * 180) / 3.14;
 
    // Print the angle
    Console.Write(String.Format("{0:F4}", Math.Abs(angle)));
}
 
// Driver code
public static void Main()
{
 
    // Given coordinates
    // Points A
    int x1 = 1, y1 = 3, z1 = 3;
 
    // Points B
    int x2 = 3, y2 = 4, z2 = 5;
 
    // Points C
    int x3 = 5, y3 = 6, z3 = 9;
 
    // Function Call
    calculateAngle(x1, y1, z1,
                   x2, y2, z2,
                   x3, y3, z3);
}
}
 
// This code is contributed by Code_Mech


输出: