java - Find an object within an geographical area -
i want java
function if pass lat,long
of object, whether object lies inside area. again area defined lat,long
for example define area 3 lat/long positions.( rectangle ) , if pass lat/long position should return me whether within rectangle.
if area rectangle, easiest way compare coordinates.
let's assume rectangle defined upper left (r1x: lon , r1y: lat) , lower right (r2x , r2y) corners. object (a point) defined px: lon , py: lat.
so, object inside area if
px > r1x , px < r2x
and
py < r1y , py > r2y
programatically like:
boolean ispinr(double px, double py, double r1x, double r1y, double r2x, double r2y){ if(px > r1x && px < r2x && py < r1y && py > r2y){ //it inside return true; } return false; }
edit
in case polygon not rectangle, can use java.awt.polygon class. in class find method contains(x,y) return true if point x , y coordinates inside polygon. method uses ray-casting algorithm. simplify, algorithm draw segment in random direction point. if segment cross polygon's boarder odd number of times, inside polygon. if crosses number of times, outside.
to use polygon class, can like:
//this defines polygon int xcoord[] = {1,2,3,5,9,-5}; int ycoord[] = {18,-32,1,100,-100,0}; mypolygon = new polygon(xcoord, ycoord, xcoord.length); //this finds if points defined x , y coordinates inside polygon boolean isinside = mypolygon.contains(x,y);
and don't forget
import java.awt.polygon;
edit
right coordinates in double !
so need use path2d.double !
begin import java.awt.geom.path2d;
let's start similar arrays before:
//this defines polygon double xcoord[] = {1.00121,2,3.5464,5,9,-5}; double ycoord[] = {18.147,-32,1,100,-100.32,0}; path2d mypolygon = new path2d.double(); //here append of points polygon for(int = 0; < xcoord.length; i++) { mypolygon.moveto(xcoord[i], ycoord[i]); } mypolygon.closepath(); //now want know if point x, y inside polygon: double x; //the x coord double y; //the y coord boolean isinside = mypolygon.contains(x,y);
and here go double !
Comments
Post a Comment