C++ 3 个有序点的方向(Orientation of 3 ordered points)

给定三个点 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 C++ program to find orientation of three points
#include <iostream>
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 collinear
// 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; // collinear
 
    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";
    cout << endl;
 
    p1 = { 0, 0 }, p2 = { 4, 4 }, p3 = { 1, 1 };
    o = orientation(p1, p2, p3);
    if (o == 0)
        cout << "Linear";
    else if (o == 1)
        cout << "Clockwise";
    else
        cout << "CounterClockwise";
    cout << endl;
 
    p1 = { 1, 2 }, p2 = { 4, 4 }, p3 = { 0, 0 };
    o = orientation(p1, p2, p3);
    if (o == 0)
        cout << "Linear";
    else if (o == 1)
        cout << "Clockwise";
    else
        cout << "CounterClockwise";
    return 0;
}

输出:

逆时针

线性

顺时针

时间复杂度: 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) 的斜率,则点处于顺时针方向

#include <iostream>
using namespace std;
 
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";
    }
    else if (slope1 < slope2) {
        return "CounterClockWise";
    }
    else {
        return "ClockWise";
    }
}
 
int main()
{
    // Example usage
    int p1[] = { 0, 0 };
    int p2[] = { 4, 4 };
    int p3[] = { 1, 1 };
    cout << orientation(p1, p2, p3) << endl;
 
    int p4[] = { 0, 0 };
    int p5[] = { 4, 4 };
    int p6[] = { 1, 2 };
    cout << orientation(p4, p5, p6) << endl;
 
    return 0;
}
// This code is contributed by user_dtewbxkn77n

输出:

共线

逆时针

时间复杂度:O(1)

空间复杂度:O(1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn_aspnet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值