如何在Java中查找点是否位于正方形内? [英] How to find whether a point is lying inside a square or not in Java?

查看:470
本文介绍了如何在Java中查找点是否位于正方形内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确定一个点是否位于正方形内.我得到了一个正方形的对角线的结束坐标,并且找到了另一个对角线的结束坐标.

I want to find whether a point lies inside a square or not. I have been given end coordinates of a diagonal of a square and I have found out the other diagonal's end coordinates.

我能想到的一种方法是通过使用外接圆的性质来解决它.我已经将所有点都包括在正方形的外接圆内,但是我想不出一种方法来排除多余的点.

One method I could think of it was to solve it by using property of circumcircle. I have included all the points inside the circumcircle of the square but I cannot think of a method to exclude the extra points.

边缘不平行于任何坐标轴.

在Java中有什么方法可以做到吗?

Is there any method to do it in java?

这里是我的一部分代码:

Heres my part of code:

if(distance(x1,y1,x2,y2,j,i)<=dd)
        A[i][j]=1;        
    else
        A[i][j]=0; 

x1,y1,x2,y2是正方形对角线的坐标,dd是对角线的长度,i,j是大于0且小于100的坐标. 我正在使用数组存储满足条件的点.

x1,y1,x2,y2 are the coordinates of the diagonal of the square and dd is the length of diagonal and i,j are the coordinates greater than 0 and less than 100. I am using array to store the points satisfying the condition.

推荐答案

在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)

这篇关于如何在Java中查找点是否位于正方形内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆