Java 如何检查给定的点是否在矩形内
矩形是一个封闭的二维图形,有4条边和4个角落。相对的两边长度相等且平行。所有4个内角相等,角度为90度。它是一个四边形。
本程序中将介绍两种方法。
- 如果给出了矩形的左下角和右上角点的值
-
如果给出了矩形的四个点
如果给出了矩形的左下角和右上角点的值
那么给定点的x坐标应该在矩形的x坐标范围内,给定点的y坐标应该在矩形的y坐标范围内。
如果给出了矩形的四个点
找到矩形的面积,并检查它是否与由该点形成的四个三角形的面积相等。
本文将介绍如何使用Java编程语言来检查点是否在矩形内。
为您展示一些实例
实例1
假设左下角和右上角点值分别为(1, 1)和(9, 8)。
给定点为(2, 3)。
我们可以说给定的点(2, 3)在矩形内。
步骤
步骤1
- 步骤1 - 获取左下角点(x1, y1)和右上角点(x2, y2),以及给定点值(x, y),可以通过初始化或用户输入来获得。
-
步骤2 - 如上所述,使用if条件判断,检查是否给出了左下角和右上角点的值,然后应满足x > x1和x < x2以及y > y1和y < y2。
-
步骤3 - 如果条件满足,打印点在矩形内,否则不在。
步骤2
-
步骤1 - 获取矩形A(x1,y1),B(x2,y2),C(x3,y3),D(x4,y4)的四个点值以及给定点P(x,y)的值,可以通过初始化或用户输入获得。
-
步骤2 - 如上所述,如果给定矩形的4个点,则找出矩形ABCD的面积以及4个三角形的面积,即三角形PAB的面积,三角形PBC的面积,三角形PCD的面积,三角形PAD的面积。
-
步骤3 - 然后使用if条件语句检查矩形的面积是否与4个三角形的面积相同。如果相同,则点在矩形内部,否则不在矩形内。
多种方法
我们以不同的方法提供了解决方案。
- 如果给出了左下角和右上角的点值
-
如果给出了矩形的四个点
让我们逐一查看程序及其输出。
方法1:如果给出了左下角和右上角的点值
在这种方法中,程序将初始化左下角、右上角和一个点的值。然后通过使用Algorithm−1检查点是否在矩形内。
示例
public class Main {
//main method
public static void main(String[] args) {
//declared variables and initialized bottom-left corner coordinates
int x1 = 1, y1 = 1;
//declared variables and initialized top-right corner coordinates
int x2 = 9, y2 = 8;
//declared variables and initialized point value which needs to be checked
int x = 2, y = 3;
//calling the user defined method by passing all the point values as parameter
if (FindPoint(x1, y1, x2, y2, x, y))
System.out.println("Yes given point lies inside rectangle");
else
System.out.println("No given point does not lie inside rectangle");
}
//method to check given point lies inside rectangle or not
static boolean FindPoint(int x1, int y1, int x2, int y2, int x, int y) {
//if it satisfies the condition
if (x > x1 && x < x2 && y > y1 && y < y2) {
//then return true
return true;
}
//else return false
return false;
}
}
输出
Yes given point lies inside rectangle
方法-2:如果矩形的四个点已知
在这种方法中,程序中将初始化矩形的四个点。然后,使用算法-2来检查点是否在矩形内部。
示例
public class Main {
//main method
public static void main (String[] args) {
//declared the variables and initialized the point values
int x1=0;
int y1=0;
int x2=10;
int y2=0;
int x3=10;
int y3=10;
int x4=0;
int y4=10;
//given point to be checked
int x=13;
int y=13;
//calling the user defined method to check if point lies inside rectangle
if (checkPoint(x1,y1,x2,y2,x3,y3,x4,y4,x,y))
System.out.print("Yes given point lies inside rectangle");
else
System.out.print("No given point does not lie inside rectangle");
}
//user defined method to find area of triangle
static float triangleArea(int x1, int y1, int x2, int y2, int x3, int y3) {
return (float)Math.abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0);
}
//user defined method to check whether point lies inside rectangle or not
static boolean checkPoint(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int x, int y) {
//area of rectangle ABCD
float ABCD = (x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))/2 + (x1*(y4-y3) + x4*(y3-y1) + x3*(y1-y4))/2;
//find area of triangle PAB
float PAB = triangleArea(x, y, x1, y1, x2, y2);
//find area of triangle PBC
float PBC = triangleArea(x, y, x2, y2, x3, y3);
//find area of triangle PCD
float PCD = triangleArea(x, y, x3, y3, x4, y4);
// find area of triangle PAD
float PAD = triangleArea(x, y, x1, y1, x4, y4);
//check if area of rectangle is
//equal to areas formed by four triangles
if(ABCD == PAB + PBC + PCD + PAD)
//then return true
return true;
else
//else return false
return false;
}
}
输出
No given point does not lie inside rectangle
在本文中,我们通过使用不同的方法来探讨如何在Java中检查一个点是否在矩形内。