给定三个点 p1、p2 和 p3,任务是确定这三个点的方向。
平面中有序三重点的方向可以是
逆时针
顺时针
共线
下图显示了 (a,b,c) 的不同可能方向
如果 (p1, p2, p3) 的方向共线,则 (p3, p2, p1) 的方向也共线。
如果 (p1, p2, p3) 的方向是顺时针,则 (p3, p2, p1) 的方向是逆时针,反之亦然。
例子:
输入: p1 = {0, 0}, p2 = {4, 4}, p3 = {1, 2}
输出: 逆时针
输入: p1 = {0, 0}, p2 = {4, 4}, p3 = {1, 1}
输出: 共线
如何计算方向?
这个想法就是利用斜率。
线段 (p1, p2) 的斜率:? = (y2 - y1)/(x2 - x1)
线段 (p2, p3) 的斜率:? = (y3 - y2)/(x3 - x2)
如果 ? > ?,则方向为顺时针(右转)
使用上述 ? 和 ? 的值,我们可以得出结论,
方向取决于以下表达式的符号:
(y2 - y1)*(x3 - x2) - (y3 - y2)*(x2 - x1)
当 ? < ? 时,上述表达式为负数,即逆时针
下面是上述想法的实现:
// 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 collinear
// 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; // collinear
// clock or counterclock wise
return (val > 0)? 1: 2;
}
/* Driver program to test above function */<strong>
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 */
输出:
逆时针
线性
顺时针
时间复杂度: O(1)
辅助空间: O(1)
以下文章使用了方向的概念:
找到给定点集的简单闭合路径
c++ https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141715783
java https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141715877
python https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141715899
C# https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141715914
Javascript https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141715931
如何检查两个给定的线段是否相交?
c++ https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141713655
java https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141713762
python https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141714389
C# https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141714420
Javascript https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141714442
凸包 | 集合 1(贾维斯算法或包装)
c++ https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141716082
java https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141716363
python https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141716444
C# https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141716403
Javascript https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141716421
凸包 | 集合 2(格雷厄姆扫描)
c++ https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141716566
java https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141717095
python https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141717139
C# https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141717214
Javascript https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141717264
方法#2:使用斜率
该方法通过计算由点形成的线段的斜率来检查平面中 3 个有序点的方向。如果斜率相等,则这些点共线。如果前两个点形成的线段的斜率小于后两个点形成的线段的斜率,则方向为逆时针,否则为顺时针。
算法
1. 计算 (p1,p2) 和 (p2,p3) 形成的线的斜率
2. 如果斜率相等,则点共线
3. 如果 (p1,p2) 的斜率 < (p2,p3) 的斜率,则点处于逆时针方向
4. 如果 (p1,p2) 的斜率 > (p2,p3) 的斜率,则点处于顺时针方向
using System;
class Program
{
// Function to determine the orientation of three points
static string Orientation(int[] p1, int[] p2, int[] p3)
{
// Calculate slopes
int slope1 = (p2[1] - p1[1]) * (p3[0] - p2[0]);
int slope2 = (p3[1] - p2[1]) * (p2[0] - p1[0]);
// Check orientation
if (slope1 == slope2)
{
return "Collinear"; // Points are collinear
}
else if (slope1 < slope2)
{
return "CounterClockWise"; // Points are in counter-clockwise order
}
else
{
return "ClockWise"; // Points are in clockwise order
}
}
static void Main()
{
// Example usage
int[] p1 = { 0, 0 };
int[] p2 = { 4, 4 };
int[] p3 = { 1, 1 };
Console.WriteLine(Orientation(p1, p2, p3)); // Output: Collinear
int[] p4 = { 0, 0 };
int[] p5 = { 4, 4 };
int[] p6 = { 1, 2 };
Console.WriteLine(Orientation(p4, p5, p6)); // Output: CounterClockWise
}
}
输出:
共线
逆时针
时间复杂度:O(1)
空间复杂度:O(1)