您的位置:首页 > 汽车 > 新车 > 如何查询公司名称能不能注册_佛山专业网站建设公司哪家好_网络营销方式包括哪些_什么搜索引擎搜索最全

如何查询公司名称能不能注册_佛山专业网站建设公司哪家好_网络营销方式包括哪些_什么搜索引擎搜索最全

2024/9/21 10:39:32 来源:https://blog.csdn.net/hefeng_aspnet/article/details/141718097  浏览:    关键词:如何查询公司名称能不能注册_佛山专业网站建设公司哪家好_网络营销方式包括哪些_什么搜索引擎搜索最全
如何查询公司名称能不能注册_佛山专业网站建设公司哪家好_网络营销方式包括哪些_什么搜索引擎搜索最全

给定三个点 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)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com