📜  3个有序点的方向

📅  最后修改于: 2021-04-26 06:03:33             🧑  作者: Mango

平面中点的有序三元组的方向可以是

  • 逆时针方向
  • 顺时针
  • 共线

下图显示了(a,b,c)的不同可能方向


如果(p1,p2,p3)的方向是共线的,那么(p3,p2,p1)的方向也是共线的。
如果(p1,p2,p3)的方向是顺时针方向,则(p3,p2,p1)的方向是逆时针方向,反之亦然。

示例:给定三个点p1,p2和p3,找到(p1,p2,p3)的方向。

Input:   p1 = {0, 0}, p2 = {4, 4}, p3 = {1, 2}
Output:  CounterClockWise

Input:   p1 = {0, 0}, p2 = {4, 4}, p3 = {1, 1}
Output:  Colinear

如何计算方向?

这个想法是使用斜率。 线段的斜率(p1,p2):σ=(y2-y1)/(x2-x1)线段的斜率(p2,p3):τ=(y3-y2)/(x3-x2)如果σ>τ ,方向为顺时针(右转)。使用σ和τ的上述值,我们可以得出结论,该方向取决于以下表达式的符号:(y2- y1)*(x3-x2)-(y3-y2)*( x2-x1)当σ<τ时,即逆时针,上述表达式为负

以下是上述想法的实现。

C++
// A C++ program to find orientation of three points
#include 
using namespace std;
  
struct Point
{
    int x, y;
};
  
// To find orientation of ordered triplet (p1, p2, p3).
// The function returns following values
// 0 --> p, q and r are colinear
// 1 --> Clockwise
// 2 --> Counterclockwise
int orientation(Point p1, Point p2, Point p3)
{
    // See 10th slides from following link for derivation
    // of the formula
    int val = (p2.y - p1.y) * (p3.x - p2.x) -
              (p2.x - p1.x) * (p3.y - p2.y);
  
    if (val == 0) return 0;  // colinear
  
    return (val > 0)? 1: 2; // clock or counterclock wise
}
  
// Driver program to test above functions
int main()
{
    Point p1 = {0, 0}, p2 = {4, 4}, p3 = {1, 2};
    int o = orientation(p1, p2, p3);
    if (o==0)         cout << "Linear";
    else if (o == 1)  cout << "Clockwise";
    else              cout << "CounterClockwise";
    return 0;
}


Java
// JAVA Code to find Orientation of 3
// ordered points
class Point
{
    int x, y;
    Point(int x,int y){
        this.x=x;
        this.y=y;
    }
}
  
class GFG {
      
    // To find orientation of ordered triplet 
    // (p1, p2, p3). The function returns 
    // following values 
    // 0 --> p, q and r are colinear
    // 1 --> Clockwise
    // 2 --> Counterclockwise
    public static int orientation(Point p1, Point p2,
                                         Point p3)
    {
        // See 10th slides from following link 
        // for derivation of the formula
        int val = (p2.y - p1.y) * (p3.x - p2.x) -
                  (p2.x - p1.x) * (p3.y - p2.y);
       
        if (val == 0) return 0;  // colinear
       
        // clock or counterclock wise
        return (val > 0)? 1: 2; 
    }
      
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
            Point p1 = new Point(0, 0);
            Point p2 = new Point(4, 4);
            Point p3 = new Point(1, 2);
              
            int o = orientation(p1, p2, p3);
              
            if (o==0)     
            System.out.print("Linear");
            else if (o == 1)  
            System.out.print("Clockwise");
            else              
            System.out.print("CounterClockwise");
          
    }
}
  
//This code is contributed by Arnav Kr. Mandal.


Python3
# A Python3 program to find orientation of 3 points
class Point:
      
    # to store the x and y coordinates of a point
    def __init__(self, x, y):
        self.x = x
        self.y = y
  
def orientation(p1, p2, p3):
      
    # to find the orientation of 
    # an ordered triplet (p1,p2,p3)
    # function returns the following values:
    # 0 : Colinear points
    # 1 : Clockwise points
    # 2 : Counterclockwise
    val = (float(p2.y - p1.y) * (p3.x - p2.x)) - \
           (float(p2.x - p1.x) * (p3.y - p2.y))
    if (val > 0):
          
        # Clockwise orientation
        return 1
    elif (val < 0):
          
        # Counterclockwise orientation
        return 2
    else:
          
        # Colinear orientation
        return 0
  
# Driver code
p1 = Point(0, 0)
p2 = Point(4, 4)
p3 = Point(1, 2)
  
o = orientation(p1, p2, p3)
  
if (o == 0):
    print("Linear")
elif (o == 1):
    print("Clockwise")
else:
    print("CounterClockwise")
      
# This code is contributed by Ansh Riyal


C#
// C# Code to find Orientation of 3
// ordered points
using System;
public class Point
{
    public int x, y;
    public Point(int x,int y)
    {
        this.x = x;
        this.y = y;
    }
}
  
class GFG 
{
      
    // To find orientation of ordered triplet 
    // (p1, p2, p3). The function returns 
    // following values 
    // 0 --> p, q and r are colinear
    // 1 --> Clockwise
    // 2 --> Counterclockwise
    public static int orientation(Point p1, Point p2,
                                        Point p3)
    {
        // See 10th slides from following link 
        // for derivation of the formula
        int val = (p2.y - p1.y) * (p3.x - p2.x) -
                (p2.x - p1.x) * (p3.y - p2.y);
      
        if (val == 0) return 0; // colinear
      
        // clock or counterclock wise
        return (val > 0)? 1: 2; 
    }
      
    /* Driver program to test above function */
    public static void Main(String[] args) 
    {
            Point p1 = new Point(0, 0);
            Point p2 = new Point(4, 4);
            Point p3 = new Point(1, 2);
              
            int o = orientation(p1, p2, p3);
              
            if (o == 0)     
                Console.WriteLine("Linear");
            else if (o == 1) 
                Console.WriteLine("Clockwise");
            else            
                Console.WriteLine("CounterClockwise");
          
    }
}
  
/* This code contributed by PrinciRaj1992 */


输出:

CounterClockwise

定向的概念在以下文章中使用:

  • 查找给定点集的简单闭合路径
  • 如何检查两个给定的线段是否相交?
  • 凸包|设置1(贾维斯算法或包装)
  • 凸包|套装2(Graham Scan)

资料来源: http : //www.dcs.gla.ac.uk/~pat/52233/slides/Geometry1x1.pdf