给定三个点 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)
当 ? < ? 时,上述表达式为负数,即逆时针
下面是上述想法的实现:
# 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 : Collinear 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:
# Collinear 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
输出:
逆时针
线性
顺时针
时间复杂度: 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) 的斜率,则点处于顺时针方向
def orientation(p1, p2, p3):
# Calculate slopes
slope1 = (p2[1] - p1[1]) * (p3[0] - p2[0])
slope2 = (p3[1] - p2[1]) * (p2[0] - p1[0])
# Check orientation
if slope1 == slope2:
return "Collinear"
elif slope1 < slope2:
return "CounterClockWise"
else:
return "ClockWise"
# Example usage
p1 = [0, 0]
p2 = [4, 4]
p3 = [1, 1]
print(orientation(p1, p2, p3))
p1 = [0, 0]
p2 = [4, 4]
p3 = [1, 2]
print(orientation(p1, p2, p3))
输出:
共线
逆时针
时间复杂度:O(1)
空间复杂度:O(1)