Although it is a naive method, I tried the following concept:
If the point Point (px, py) is inside the given rectangle, the sum of the areas of the triangles formed by the union of two rectangular points and the given point (say, counterclockwise or clockwise) will be equal to the sum of the rectangle.
I have a picture for the same, but because of the low reputation (since I'm new), I can not publish it.
When I formulated this in real Java code, I had to handle a situation where the value of the area with the decimal part, which has 15 9 s, is rounded to the nearest integer.
Refer to this code:
public class pointInsideRect {
public static double areaOfTriangle(int xa,int ya, int xb, int yb, int px, int py) { double side1 = Math.sqrt(Math.pow(Math.abs(ya-yb),2) + Math.pow(Math.abs(xa-xb),2)); double side2 = Math.sqrt(Math.pow(Math.abs(ya-py),2) + Math.pow(Math.abs(xa-px),2)); double side3 = Math.sqrt(Math.pow(Math.abs(yb-py),2) + Math.pow(Math.abs(xb-px),2)); double semi_perimeter = (side1+side2+side3)/2; double area = Math.sqrt(semi_perimeter*(semi_perimeter-side1)*(semi_perimeter-side2)*(semi_perimeter-side3)); return area; } public static double areaOfRect(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { double side1 = Math.sqrt(Math.pow(Math.abs(y1-y2),2) + Math.pow(Math.abs(x1-x2),2)); double side2 = Math.sqrt(Math.pow(Math.abs(y2-y3),2) + Math.pow(Math.abs(x2-x3),2)); double area = side1*side2; return area; } public boolean check(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int pointX, int pointY) { double trinagle1Area = areaOfTriangle(x1, y1, x2, y2, pointX, pointY); double trinagle2Area = areaOfTriangle(x2, y2, x3, y3, pointX, pointY); double trinagle3Area = areaOfTriangle(x3, y3, x4, y4, pointX, pointY); double trinagle4Area = areaOfTriangle(x4, y4, x1, y1, pointX, pointY); double rectArea = areaOfRect(x1, y1, x2, y2, x3, y3, x4, y4); double triangleAreaSum = (trinagle1Area+trinagle2Area+trinagle3Area+trinagle4Area); if(triangleAreaSum%(Math.pow(10, 14))>=0.999999999999999) { triangleAreaSum = Math.ceil(triangleAreaSum); System.out.println(triangleAreaSum); } if(triangleAreaSum==rectArea) return true; else return false; } public static void main(String[] args) { pointInsideRect obj = new pointInsideRect(); System.out.println(obj.check(1, 1, 1, 3, 3, 3, 3, 1, 2, 2)); } }