简体   繁体   English

如何使用Java检查值是否在矩形范围内

[英]How to check if the values lie within the rectangular bound using java

I have a set of values say 我有一组价值观说

LatLong1=(lon=74.663085,lat=22.67578)
LatLong2=(lon=80.663085,lat=28.67578)

These are the latitude and longitude values of a rectangular bounded region. 这些是矩形有界区域的纬度和经度值。 LatLong1 is the left and bottom boundary values and LatLong2 is the right and top boundary values. LatLong1是左侧和底部边界值,LatLong2是右侧和顶部边界值。

Now I need to check if some object lies within this boundary at current time. 现在,我需要检查当前是否有某个对象位于此边界内。 If I have the object's position with 75.67 and 26.89 as latitude longitude respectively. 如果我有对象的位置,分别以75.67和26.89作为纬度经度。 How do I check whether these values lies within the above mentioned LatLong1 , LatLong2 values? 如何检查这些值是否在上述LatLong1和LatLong2值之内?

I've to guess your objects for which pertenency you need to check is a rentangle too, with values: 我不得不猜测您的对象,您还需要检查其持久性是否为rentangle,并带有值:

LatLong1=(lon=x0,lat=y0)
LatLong2=(lon=x1,lat=y1)

If this is the case, and both graphical structures are tied to the same origin of coordenates, the conditions to check is: 如果是这种情况,并且两个图形结构都绑定到同一个坐标系,则要检查的条件是:

if( (x0>=lon0 && x1<=lon1 ) && (y0>=lat0 && y1<=lat1 ) )
    return true;
else return false;

If you need to check if a point is inside a rectangle, being the coordinates of the point (x0,y0): 如果需要检查点是否在矩形内,则为点(x0,y0)的坐标:

if( (x0>=lon0 && x0<=lon1 ) && (y0>=lat0 && y0<=lat1 ) )
    return true;
else return false;

I don't know openlayer. 我不知道openlayer。 But isn't it like finding whether a point (x,y) is in a rectangle whose lower left is (x1,y1) and top right is (x2, y2)? 但是,这是否不像查找点(x,y)是否位于矩形的矩形中,该矩形的左下角为(x1,y1)而右上角为(x2,y2)?

In that case (x,y) is in the rectangle, if (x>x1 && x<x2 && y>y1 && y<y2) 在那种情况下(x,y)在矩形中,如果(x>x1 && x<x2 && y>y1 && y<y2)

You could create a rectangle. 您可以创建一个矩形。 With your two points you can compute the upper left corner and the dimension. 通过这两点,您可以计算左上角和尺寸。

Rectangle rect = new Rectangle(upperLeftCorner, dimension);

You want to check if a point (x,y) lies in the rectangle: 您要检查点(x,y)是否位于矩形中:

Point p = new Point(x,y);
rect.contains(p);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM