给定三个点 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)
当 ? < ? 时,上述表达式为负数,即逆时针
下面是上述想法的实现:
// javascript Code to find Orientation of 3
// ordered points
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
// 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
function orientation(p1, p2, p3) {
// See 10th slides from following link
// for derivation of the formula
let 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 */
let p1 = new Point(0, 0);
let p2 = new Point(4, 4);
let p3 = new Point(1, 2);
let o = orientation(p1, p2, p3);
if (o == 0)
document.write("Linear");
else if (o == 1)
document.write("Clockwise");
else
document.write("CounterClockwise");
// This code is contributed by Saurabh Jaiswal
输出:
逆时针
线性
顺时针
时间复杂度: O(1)
辅助空间: O(1)
以下文章使用了方向的概念:
找到给定点集的简单闭合路径
c++ https://blog.csdn.net/hefeng_aspnet/article/details/141715783
java https://blog.csdn.net/hefeng_aspnet/article/details/141715877
python https://blog.csdn.net/hefeng_aspnet/article/details/141715899
C# https://blog.csdn.net/hefeng_aspnet/article/details/141715914
Javascript https://blog.csdn.net/hefeng_aspnet/article/details/141715931
如何检查两个给定的线段是否相交?
c++ https://blog.csdn.net/hefeng_aspnet/article/details/141713655
java https://blog.csdn.net/hefeng_aspnet/article/details/141713762
python https://blog.csdn.net/hefeng_aspnet/article/details/141714389
C# https://blog.csdn.net/hefeng_aspnet/article/details/141714420
Javascript https://blog.csdn.net/hefeng_aspnet/article/details/141714442
凸包 | 集合 1(贾维斯算法或包装)
c++ https://blog.csdn.net/hefeng_aspnet/article/details/141716082
java https://blog.csdn.net/hefeng_aspnet/article/details/141716363
python https://blog.csdn.net/hefeng_aspnet/article/details/141716444
C# https://blog.csdn.net/hefeng_aspnet/article/details/141716403
Javascript https://blog.csdn.net/hefeng_aspnet/article/details/141716421
凸包 | 集合 2(格雷厄姆扫描)
c++ https://blog.csdn.net/hefeng_aspnet/article/details/141716566
java https://blog.csdn.net/hefeng_aspnet/article/details/141717095
python https://blog.csdn.net/hefeng_aspnet/article/details/141717139
C# https://blog.csdn.net/hefeng_aspnet/article/details/141717214
Javascript https://blog.csdn.net/hefeng_aspnet/article/details/141717264
方法#2:使用斜率
该方法通过计算由点形成的线段的斜率来检查平面中 3 个有序点的方向。如果斜率相等,则这些点共线。如果前两个点形成的线段的斜率小于后两个点形成的线段的斜率,则方向为逆时针,否则为顺时针。
算法
1. 计算 (p1,p2) 和 (p2,p3) 形成的线的斜率
2. 如果斜率相等,则点共线
3. 如果 (p1,p2) 的斜率 < (p2,p3) 的斜率,则点处于逆时针方向
4. 如果 (p1,p2) 的斜率 > (p2,p3) 的斜率,则点处于顺时针方向
function orientation(p1, p2, p3) {
// Calculate slopes
let slope1 = (p2[1] - p1[1]) * (p3[0] - p2[0]);
let slope2 = (p3[1] - p2[1]) * (p2[0] - p1[0]);
// Check orientation
if (slope1 == slope2) {
return "Collinear";
} else if (slope1 < slope2) {
return "CounterClockWise";
} else {
return "ClockWise";
}
}
// Example usage
let p1 = [0, 0];
let p2 = [4, 4];
let p3 = [1, 1];
console.log(orientation(p1, p2, p3));
p1 = [0, 0];
p2 = [4, 4];
p3 = [1, 2];
console.log(orientation(p1, p2, p3));
输出:
共线
逆时针
时间复杂度:O(1)
空间复杂度:O(1)