Finding a coordinate in a circle - java

I am doing a mashup using Google Maps under Grails where users can create geofences by selecting a point on the map and a radius. This get stored on my database and the application receives constantly a set of coordinates from a GPS device.
I would like to compare the received coordinates with the area stored in the circles. If the point is inside (or outside) the circle the program will fire an action. However, I would like to know how I can find out if the coordinates are located inside/outside the circle. There is a Javascript library which allows doing this but I need to do this on the server.
Is there a Java (or even Groovy) library for this?
How would you implement it?

if distance from point to center of circle is <= radius of circle then it is inside the circle.
if the area is made of more than one circle than compare to all the circles... it won't take that long.
java.awt.geom.Point2D.Double is perfect for this.

Well, if it doesn't need to be "perfect", you don't need to worry about plotting circles or anything like that. You can just take the two locations (the location you want to test, and the center of the circle) and use Pythagorus to find the distance. If that distance is less than the radius of the circle, it's inside.
There is a caveat to take into consideration, however: the reason this wouldn't be perfect is that that for your points, you're probably going to get a latitude and longitude...and the Earth is a sphere. So near the poles of the Earth this will kind of fall apart. But it may well be good enough for what you're doing.

Sadly, most of the responses here won't work for you conveniently, because GPS coordinates are in units of degrees. You will need something to convert from two points in Degrees of latitude and longitude to a great circle distance, which simple Pythagorean theorem falls short of.
If you're using Google maps API, you can probably do everything you need using GLatLng. As other posters have noted, You can determine the distance between two points is less than the radius of the specified circle. Specifically GLatLng.distance(other:GLatLng) returns the meters distance between too GPS locations.
To actually display the circles requires a bit more finesse. You will need to create a GPolygon to draw the circumference of the circle. You can find a number of free JavaScript functions that can do this for you.

Victor and Beska have the correct answer. That is, if the distance between the point and the center is less than the radius, then it's in the circle.
For the great circle distance between two points, you can use GeoTools' GeodeticCalculator. In particular you set the point and radius using setStartingGeographicPoint and setDestinationGeographicPoint followed by calling getOrthodromicDistance which will return the distance.

You want to find the vector that is the distance between the selected coordinate and the center of the circle, then compute the square distance between the selected coordinate and the center of the circle by squaring the components of the vector and adding them together; if that scalar (the squared distance) is less than the square of the radius, the point is within the circle.
This method avoids having to take a square root, and is just as accurate as normal distance comparison.

One possibility is to calculate the distance from the centerpoint and compare it to the radius.
Depending on you application you may be have to take into account that the world is a sphere and not 2Dimensional. To calcualte a distance on earth you can use this formula.

Since you are using Google Maps and for geographical distances spherical geometry holds rather than euclidean geometry. However if it is relativley smaller distance like a parking lot etc. then you can use euclidean distance formula (http://en.wikipedia.org/wiki/Distance) to find out whether the point is inside or outside the circle.
I presume you know the coordinates of the circle's center C(xc, yc) and its radius, R. Then for a given point P(x1, y1) find the euclidean distance, D as
square-root((x1-xc)^2 + (y1-yc)^2)). If D > R, the point lies outside the circle. If D < R, the point lies inside the circle. If D = R, the point lies on the circumference of the circle.
In case you are doing your measurements over larger distances then you should rather look for Geodesics (please check this http://en.wikipedia.org/wiki/Great-circle_distance).
I hope it helps.
cheers

Related

Geo-fencing in java web application

I am writing a server side java application related to Geo-fencing.
I have a store with their latitude,longitude and radius
I also have Person's current position(in terms of latitude & longitude).
If he arrives within the geo fence i.e., within the store's radius i have to give an alert. I googled for the solution & there are some methods to identify person is inside the fence(circle) or outside the fence.
Calculate distance between store latitude,longitude and person's current latitude & longitude and if Distance < radius then person is inside the fence else outside the fence.
Using Polygon Geofencing
Can anyone suggest me which method is best from the above.
Thanks in advance
It depends on your needs. If you want to know whether a Person is inside 1km radius of store, then you need a circle search. Here, location of the store is the pivot. You might change the radius based on your need.
Polygon search is used when you need to search people in a particular area regardless of how much distance it is away from anywhere. People in Paris, New York or Istanbul area, not people inside 1km radius of a store.
In your case, where only radius (i.e. distance to the shop) is needed, I would definitely choose the radius search (your first proposition).
The Polygon Geofencing, I think, is using the Ray-Casting Algrithm which iterates over each segment of the polygon. This will take more time and could lead to some approximation in the case of a circle (depending on how the algorithm is applied).
I would have thought that the best method to use here would be a radius distance calculation, presuming you need to know the distance 'as the crow flies' so to speak. So geographical distance rather than travelling distance. This will be the most performant and simplest to implement. Examples available here and here.
If the latter is the case (which I hope for your sake it isn't), then I would imagine you would need to use polygon geofencing or perhaps Google's distance Matrix API.
Thanks for all your answers. In my project i have integrated both methods(Polygon & Distance). If a user enters into the polygon he ll get a notification and same like radius. I have stored the polygon co-ordinates and radius into my database.

How to draw markers inside a specific radius for google maps in android

I have a Google Map fragment on my android application.
I have markers drawn on the map which represent places of interest and I have an extra functionality that draws a circle on the center with a radius defined by the user.
What I want to do is that only the markers which are contained inside this circle shall be displayed on map. I am using the Circle object, from Google maps Shapes, to draw the circle.
The problem is that, although the center of the circle is defined in Latitude/Longitude coordinates, it's radius is defined in meters. So how can I calculate if a position of a place in LatLng is contained within a radius defined in meters?
Thank you
All you need is to calculate if two LatLngs are closer to each other than radius.
For that you can use Location.distanceBetween.
See the Circle.contains function here for an example.
Basically what you are going to have to do is test all points and use ray casting to test the points against sides the circle.
If you find only 1 collision with a side then the point is inside the circle, if you find 2 collisions spots then it is outside the circle.
It sounds intimidating but its not too bad once you understand it
Here is excellent documentation on what is involved. I used a combination of the first and second answer to something similar

Making a rectangle go around in a circle - Java/Swing

My maths isn't that good so I'm having a bit of trouble in one of my applications that I'm trying to do where I want a rectangle to represent a vehicle and I want that vehicle/rectangle to "drive" around in a circle. Imagine a roundabout with only 1 vehicle in it, just circling around forever.
If I can get some help how to do that then I'll be able to build on the example and most importantly learn.
If someone could write up a simple example for me I'd be grateful. No background no images, just a rectangle "driving" around in a circle. I'm using java and Swing.
Sorry, I am not sure if could understand clear you exactly need. If you need to draw rectangle which is moving around inside of circle, you can use sin/cos functions.
Something like that:
double r = 50.0; // radius (it might radius of your circle, but consider dimensions of rectangle to make sure you are drawing inside of circle, e.g. circleRadius - rectangeDimesion / 2.0)
for (int f = 0; f < 360; f++) {
double x = Math.sin(Math.toRadians((double)f)) * r;
double y = Math.cos(Math.toRadians((double)f)) * r;
// draw rectangle on [x, y] coordinates
}
If you know the radius of the round about, all the you would need would be a trigonometric function and the angle which the vehicle makes to the round about. You could take a look at this simple introduction which should get you started in the right direction.
On another hand, another approach would be to use a Transformation Matrix where you start with a matrix containing two points (your X and Y co-ordinates) and you transform them to become the new co-ordinates.
You can then rotate the rectangle to mimic a vehicle turning.
If you have a limited background in Mathematics, the first option might be easier for you to grasp.
This is more an extended comment than an answer.
I would divide the problem up into several easier problems, and work on each of them separately:
Draw your rectangle with a specified center location and long axis orientation.
Determine the center point and long axis orientation for an object orbiting around the origin. Note that to get make the long axis a tangent it needs to be perpendicular to the radius through the center.
Translate the whole system so that it orbits the desired point, rather than the origin.

Calculate shape orientation in Java (Image analysis)

I have an image such as this:
and I need to calculate the orientation of it. In this case the shape is pointing towards the top left of the screen. Accuracy isn't hugely important as long as 3 or 4 calculations average out to within 5 degrees or so of the actual orientation (it will be moving slightly).
Can anyone point me towards an algorithm to do this? I don't mind if the orientation is returned as a double or as a vector.
If the image is always T-shaped, you can simply get the furthest pair of pixels, then find the furthest pair from either both of those (the edges of the T), find which is further from the other two, draw a line from that one to the middle point of those two.
You can further refine it by then finding the base of the T by comparing the middle line with the edges of the base, and adjusting the angle and offset until it is actually in the middle.
The definitive solution is impossible I guess, since requires image recognition. I would project the 2D image onto axis, i.e. obtain the width and height of the image and get direction vector from these values taking them as components.
First, a couple of assumptions:
The center and centroid are "close"
The descending bar of the T is longer than the cross-bar
First, determine the bounding rectangle of the image and find the points of the image that lie along this rectangle. For points that lie along the line and are a certain distance from one another (say 5 pixels to pick a value) you'll need to only take 1 point from that cluster. At the end of this you should have 3 points, i.e. a triangle. The shortest side of the triangle should be the cross-bar (from assumption 2), i.e. find the two points closest to each other. The line that is perpendicular to the line crossing those two points is then your orientation line, i.e. find the angle between it and the horizontal axis.
I would try morphological skeletonization to simplify the image, followed by some straightforward algorithm to determine the orientation of the longer leg of the skeleton.
The solution in the end was to use a Convex Hull Algorithm, which finds the minimum number of points needed to enclose a shape with a bound.

api for within distance polygon

Is there any API to obtain a list of vehicle within a certain distance (Eg:10 KM)? I am using the map from geoserver. And need to draw the circle within that distance.
Take a look here to see how you can obtain the distance between 2 GPS points (1 point being the vehicle you want to track and the other being the centre of the circle). Once that you have obtained the distance, check to see if the distance is greater than the radius. If the distance is less than the radius, the vehicle is in the circle. If it is equal to the radius, the vehicle is on the edge of the circle, if it is greater than the radius, then, the vehicle is outside the circle.
If you need more complex calcualtions, you might want to take a look at Spheres:
A Java package for doing spherical trigonometry without all the messy
(and cpu intensive) trigonometry. The spheres package includes classes
for points, spherical polygons, latitude/longitude bounding boxes,
orbits, and scenes.

Categories

Resources