如何在Java中检查三个有序点的方向?

2023年 9月 14日 15.4k 0

如何在Java中检查三个有序点的方向?

在本文中,我们将找到3个有序点的方向。这里的方向指的是给定的点在空间中形成顺时针、逆时针或共线的形状。

在上图中,a、b、c 是检查形状在空间中的方向的三个点。我们通过计算斜率找到三个给定点的方向。

计算坡度并计算 3 个有序点的方向。

线段的斜率

线段的斜率 $(a,b):theta=left ( y_{b}-y_{a} right )/left ( x_{b} -x_{a}right )$

线段$(b,c)$的斜率:$phi=left ( y_{c} -y_{b}right )/(x_{c}-x_{b})$

因此,方向取决于以下表达式:

$$(y_{b}-y_{a})(x_{c}-x_{b})-(y_{c}-y_{b})(x_{b}-x_{a}) :或者:(y2-y1)*(x3-x2)-(y3-y2)*(x2-x1)$$

即,无论是正数、负数还是

  • 如果表达式为零,则 θ = φ。因此方向是共线的。

  • 如果表达式为负,则 θ

  • 如果表达式为正,则 θ > φ。因此方向为顺时针。

开始吧!

展示一些实例给你看

Instance-1

的翻译为:

实例-1

假设 3 个有序点是 (0,3), (4,2), (3,1)

检查 3 个有序点的方向后,结果将是:

给定的3个点形成:顺时针

Instance-2

的中文翻译为:

实例-2

假设 3 个有序点是 (0,3), (1,2), (9,5)

检查 3 个有序点的方向后,结果将是:

给定的3个点形成:逆时针方向

实例3

假设 3 个有序点是 (2,2), (3,3), (4,4)

检查 3 个有序点的方向后,结果将是:

给定的3点形式:线性

算法

第 1 步 - 声明 3 个有序点。

步骤 2 - 将三个给定点传递给表达式,即 (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y)。

步骤 3 - 检查线性、顺时针和逆时针的条件。

第四步 - 打印结果。

多种方法

我们通过不同的方式提供了解决方案。

  • 通过静态输入

  • 通过使用用户定义的方法

让我们一一看看该程序及其输出。

方法 1:使用静态输入

在这个方法中,首先将3个点传递给表达式,以检查线性、顺时针和逆时针的条件。然后将结果打印到输出。

示例

public class Main{
//main method
public static void main(String[] args){
//Declaring variables
int x1=0, y1=1;
int x2=4, y2=3;
int x3=3, y3=2;

//expression to check for 3 ordered point
int val = (y2 - y1) * (x3 - x2) - (x2 - x1) * (y3 - y2);

// check for collinear
if (val == 0){
//printing collinear orientation
System.out.print("The given 3 points form : Linear");
}

//check for clockwise
else if(val > 0){

//printing clockwise orientation
System.out.print("The given 3 points form: Clockwise");
} else {

//printig counter clockwise orientation
System.out.print("The given 3 points form: CounterClockwise");
}
}
}

登录后复制

输出

The given 3 points form: Clockwise

登录后复制

方法二:使用用户定义的方法

在这种方法中,首先将3个点通过一个用户定义的方法传递给表达式,以检查线性、顺时针和逆时针的条件。然后将结果打印到输出。

示例

public class Main {
public static void main(String[] args){
Point a = new Point(2, 2);
Point b = new Point(3, 3);
Point c = new Point(4, 4);

//calling user defined method
int o = orientation(a, b, c);

//check for Linear orientation
if (o==0)

//printing Linear orientation
System.out.print("The given 3 points form : Linear");

//check for Clockwise orientation
else if (o == 1)

//printing clockwise orientation
System.out.print("The given 3 points form : Clockwise");
else

//printing counter clockwise orientation
System.out.print("The given 3 points form : CounterClockwise");
}

// user defined method
public static int orientation(Point a, Point b, Point c){

//expression to check for 3 ordered point
int val = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);

// check for collinear
if (val == 0) return 0;

// check for clock or counterclock wise
return (val > 0)? 1: 2;
}
}
class Point{
int x, y;
Point(int x,int y){
this.x=x;
this.y=y;
}
}

登录后复制

输出

The given 3 points form : Linear

登录后复制

在本文中,我们使用Java编程语言探讨了如何通过检查3个有序点的方向来判断方向。

以上就是如何在Java中检查三个有序点的方向?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论