如何在 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:如果給出左下角和右上角點的值
在這種方法中,左下角、右上角點和一個點值將在程式中初始化。然後透過使用演算法 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 中的矩形內。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP