简体   繁体   中英

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.

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. How do I check whether these values lies within the above mentioned LatLong1 , LatLong2 values?

I've to guess your objects for which pertenency you need to check is a rentangle too, with values:

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

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

I don't know 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)?

In that case (x,y) is in the rectangle, if (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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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