Java程序检查点在直线的左侧还是右侧
一条直线由无限多个点组成。在二维坐标系中,我们可以用两个值(即X和Y)来定义每个点。一个点基本上位于左侧或右侧,或者它可以位于直线上本身,只有当我们拥有它的坐标时才能定义这一点。
在这个程序中,我们将使用叉积方法来找到点的方向。叉积方法用于通过对两个向量进行叉乘来找到第三个向量。在我们的例子中,如果我们对直线的原点和用于查找方向的给定点进行叉乘。那么我们将得到一个正值、一个负值或零。
如果我们得到一个正值,那么我们可以确定该点位于直线的右侧。如果我们得到一个负值,那么我们可以确定该点位于直线的左侧。如果为零,则该点位于直线上的某个位置。
计算两个点A(x, y)和B(x, y)的叉积的公式−
Cross-Product = [A(x) X B(y)] – [B(x) X A(y)]
在这篇文章中,我们将了解如何使用Java来查找点是在直线的左侧还是右侧。
问题陈述
用Java编写一个程序来检查一个点是否在直线的左侧或右侧−
输入1
The input point of line:
A(x, y) = (20, -20)
B(x, y) = (-30, -23)
The coordinate of the point:
P(x, y) = (4, 5)
The coordinates of origin point and the observed points are:
O1(x, y) = [(B(x)-A(x)), (B(y)-A(y))] = (-50,-3)
O2(x, y) = [(P(x)-A(x)), (P(y)-A(y))] = (-16,-25)
The cross-product between the O1 and O2 = [O1(x) X O2(y)] – [O2(x) X O1(y)]
=[-50 X -25] – [-16 X -3]
= 1250 – 48 = 1202
输出1
As the output is positive, hence the point is situated on the right side of the line
输入2
The input point of line:
A(x, y) = (-2, 4)
B(x, y) = (7, 8)
The coordinate of the point:
P(x, y) = (10,-20)
The coordinates of origin point and the observed points are:
O1(x, y) = [(B(x)-A(x)), (B(y)-A(y))] = (9, 4)
O2(x, y) = [(P(x)-A(x)), (P(y)-A(y))] = (12, -24)
The cross-product between the O1 and O2 = [O1(x) X O2(y)] – [O2(x) X O1(y)]
= [9 X -24] – [12 X 4]
= -216 – 48 = -264
输出2
As the output is negative, hence the point is situated on the left side of the line.
多种方法
我们提供了不同方法的解决方案−
使用静态输入值
在这种方法中,我们通过静态输入方法声明直线的点和点的坐标,并将这些值作为参数传递给我们的用户定义方法。然后,通过使用方法内的算法,我们可以找到点相对于直线的方向。
- 首先创建Main类并定义main方法。
- 创建三个点对象:first、second和point。
- 为直线的端点first和second分配静态值。
- 为要检查的点point分配静态值。
- 使用三个点对象调用dirOfPoint方法来计算方向。
- 如果返回值为1,则打印该点位于直线的右侧;如果返回值为-1,则打印该点位于直线的左侧;如果返回值为0,则打印该点位于直线上。
- 定义一个静态内部类pnt,其中包含int x和int y。
- 实现dirOfPoint方法来计算叉积并返回方向。
示例
以下是检查点是否在直线的左侧或右侧的示例−
public class Main{
public static void main(String[] args) {
pnt first = new pnt();
pnt second = new pnt();
pnt point = new pnt();
first.x = -20;
first.y = 15;
// first(-20, 15)
second.x = 31;
second.y = -18;
// second(31, -18)
point.x = 32;
point.y = 45;
// point(32, 45)
int dir = dirOfPoint(first, second, point);
if (dir == 1)
System.out.println("The point is on Right Direction of the line.");
else if (dir == -1)
System.out.println("The point is on Left Direction of the line.");
else
System.out.println("Point is somewhere on the Line.");
}
static class pnt{
int x, y;
};
static int R = 1, L = -1, Z = 0;
static int dirOfPoint(pnt first,pnt second, pnt point) {
second.x -= first.x;
second.y -= first.y;
point.x -= first.x;
point.y -= first.y;
int crs_prod = second.x * point.y - second.y * point.x;
if (crs_prod > 0)
return R;
if (crs_prod < 0)
return L;
return Z;
}
}
输出
The point is on Right Direction of the line.
使用用户输入值
在这种方法中,我们通过用户输入方法声明直线的点和点的坐标,并将这些值作为参数传递给我们的用户定义方法。然后,通过使用方法内的算法,我们可以找到点相对于直线的方向。
- 首先,从java.util包导入所有必要的类,并将创建Main类并定义main方法。
- 之后,我们将创建一个Scanner对象用于用户输入,并创建三个点对象:first、second和point。
- 提示用户输入直线的第一个点的坐标,并在first.x和first.y中存储它们,然后输入直线的第二个点的坐标,并在second.x和second.y中存储它们。
- 提示用户输入要检查的点的坐标,并在point.x和point.y中存储它们。
- 使用三个点对象调用dirOfPoint方法来计算方向。
- 我们将使用if-else条件语句。如果返回值为1,则打印该点位于直线的右侧;如果返回值为-1,则打印该点位于直线的左侧;如果返回值为0,则打印该点位于直线上。
- 定义一个静态内部类pnt,其中包含int x和int y。
- 实现dirOfPoint方法来计算叉积并返回方向。
示例
以下是检查点是否在直线的左侧或右侧的示例−
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
pnt first = new pnt();
pnt second = new pnt();
pnt point = new pnt();
System.out.println("Enter the coordinates of first point of the line: ");
System.out.println("--------------------------------------------------");
System.out.print("X- value: ");
first.x = sc.nextInt();
System.out.print("Y- value: ");
first.y = sc.nextInt();
System.out.println("Enter the coordinates of second point of the line:");
System.out.println("--------------------------------------------------");
System.out.print("X value: ");
second.x = sc.nextInt();
System.out.print("Y value: ");
second.y = -sc.nextInt();
System.out.println("Enter the coordinates of the point which you want to navigate: ");
System.out.println("--------------------------------------------------");
System.out.print("X value: ");
point.x = sc.nextInt();
System.out.print("Y value: ");
point.y = sc.nextInt();
int dir = dirOfPoint(first, second, point);
if (dir == 1)
System.out.println("The point is on Right Direction of the line.");
else if (dir == -1)
System.out.println("The point is on Left Direction of the line.");
else
System.out.println("Point is somewhere on the Line.");
}
static class pnt{
int x, y;
};
static int R = 1, L = -1, Z = 0;
static int dirOfPoint(pnt first,pnt second, pnt point){
second.x -= first.x;
second.y -= first.y;
point.x -= first.x;
point.y -= first.y;
int crs_prod = second.x * point.y - second.y * point.x;
if (crs_prod > 0)
return R;
if (crs_prod < 0)
return L;
return Z;
}
}
输出
Enter the coordinates of first point of the line: --------------------------------------------------- X- value: 10 Y- value: 20 Enter the coordinates of second point of the line: ---------------------------------------------------- X value: -11 Y value: 12 Enter the coordinates of the point which you want to navigate: ---------------------------------------------------------------- X value: 4 Y value: 6 The point is on Right Direction of the line.
在这篇文章中,我们探讨了如何通过使用不同的方法在Java中检查点是否在直线的左侧或右侧。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP