且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Java中查找点是否位于正方形内?

更新时间:2023-02-26 16:03:32

在Java中没有内置方法可以执行此操作,但这是简单的数学运算.

There's no built-in method to do it in Java, but it's simple math.

一条线的等式是:

 a * x + b * y = c1   (1)

与此平行的线的方程为:

The equation of a line parallel to this is:

 a * x + b * y = c2   (2)

垂直于这两条线的等式为:

The equation of two lines perpendicular to these are:

-b * x + a * y = c3   (3)
-b * x + a * y = c4   (4)

这些是正方形四个边的方程式.

These are the equations of the four edges of the square.

确定正方形的上述方程的系数(abc1..c4).

Determine the coefficients of the equations above (a, b, c1..c4) for your square.

如果满足以下两个条件,则该点位于正方形内:

The point is inside the square iff both of the following conditions are true:

min(c1, c2) <= a * x + b*y <= max(c1, c2)
min(c3, c4) <= -b * x + a * y <= max(c3, c4)