Geolocation - Check if a location belongs to an area - javascript

having a location coordinate(latitude,longitude), is it possible to check if it belongs to an area that have an array of coordinates (example when you draw a polygon on map that have array of coordinates) ? I am using JS, html5, and php and i would be flexible to solutions from other programming languages. Thank You a bunch in advance !

One way could be a ray casting algorithm. The theory is to draw a virtual horizontally ray from outside the polygon to your point of interest and count how often it crosses any side of the polygon. If the number is even, your point is outside if it's odd it's inside the polygon.
Look here http://alienryderflex.com/polygon/ for more information and C code sample.

Related

How to shrink polygon by a defined amount in centimetres using co-ordinates

I am trying to shrink a polygon by a specific amount where all edges in the new polygon are of equal distance to the old one. To explain better I have a picture here.
If the picture doesnt work I have a link here.
https://imgur.com/a/A27mVHs
This is a rough drawing.
I have a point array in ([x,y])
[[390,435], [388,430], [391,425], [425,428], [410,435]]
I am trying to shrink it so the new array
[[x1,y1], [x2,y2] ... [x5,y5]]
ensures that the distance between the new area and the original one is 2 at all sides of the area.
How can I do this by only manipulating the co-ordinates. I know I need some kind of scalar vector but I'm unsure how to do this. I am trying to implement this in javascript
In the general case this is indeed an arduous problem. But for a convex polygon, it is pretty easy.
At every angle, draw the bissector and find the point at distance of the vertex equal to d / sin α/2 where α is the measure of the angle.
Have you done some research because there is lots of topics in internet about that subject, the short answer is this is not easy because there is lots of edge cases (look this topic for exemple).
A good term if you want to look more and find nice libs to do this task for your is Polygon offsetting
Others good links :
An algorithm for inflating/deflating (offsetting, buffering) polygons
ClipperOffset
A Survey of Polygon Offseting Strategies
Javascript Clipper

Get compass orientation of wgs84 coordinates or polygon

I got a set of wgs84 coordinates to place a polygon on Google Maps. I am wondering if i can use the same coordinates to get a compass direction of the coordinates. I don't need a solution specific for Google Maps, javascript or php. I am just wondering what the right approche would be.
Something like this would be the end result. I understand it needs some sort of algorithm in the background
I found Geolib and the 'getCompassDirection' function but this function only allows 2 points to create a line and get the bearing(angle) of that line.
You can't get any direction information out of a single position coordinate.
Position coordinates don't represent any knowledge about the orientation of an object. Therefore you need something that has direction (i.e. a vector between two coordinates) to calculate compass direction. That's exactly why the function you mentioned asks for two points.
This means you can get a compass direction for any edge of a polygon (by using its two end points), but not for its vertices.
Hope this helps!

Is there a way to determine if a given point (lat, long) is inside a drawn polygon?

I have a KML file with about 5 polygon placemarks and I was wondering if there's a way to determine if a specified point is inside any of the polygons. Does Google Earth have a function to do that or can one be written?
Use the ray casting method for point in polygon test.
The test shoots off a ray towards infinity (arbitrarily chosen direction, usually parallel to one of the axis used for code simplicity and speed) from the test point and counts the intersections with the polygon in question. If the number of intersections are odd, then the point lies within the polygon, else it lies outside. Repeat this test for all the polygons.
check out this link for further explanation and degenerate and special cases.
pip wikipedia .
I've implemented this function in C so let me know if you need any pointers.
You could alternatively also use the Winding Number test. WN wikipedia
They may have similar performances depending on the implementation and the platform.
The implementations you'l find may be mostly for Cartesian co-ordinates. Keep in mind that they'l work perfectly fine as it is, with geographic co-ordinates as well.

Creating a dot density map with D3.js

Has anyone successfully created a dot density map using D3.js? I'd love to see an example, but if anyone knows of an efficient "points in polygon" algorithm in JS, that would be useful as well. Here's what I have so far, and here's the block to see it 'in action'.
Right now I have either Halton sequences or random points working. But there are problems: a) It is not very efficient (should probably use canvas), and b) I don't have the points clipped to the states, and c) Even if I did clip to states, the total count in each state would be wrong (based on bounds not geometry).
Any thoughts/ideas to improve any aspect of the above are greatly appreciated!
Andy Woodruff has created a canvas-based dot density map with d3 at
http://bl.ocks.org/awoodruff/94dc6fc7038eba690f43
While this may not be exactly what you are looking for (it's pixel-based and not vectorized / SVG, although implemented in d3), you may get some inspirations from it. You may even consider creating the proposed canvas-based solution off-screen for point-in-polygon checking, to determine the coordinates of points to set in your SVG representation.

how to "sort" polygons 3d?

I am still working on my "javascript 3d engine" (link inside stackoverflow).
at First, all my polygons were faces of cubes, so sorting them by average Z was working fine.
but now I've "evolved" and I want to draw my polygons (which may contain more than 4 vertices)
in the right order, namely, those who are close to the camera will be drawn last.
basically,
I know how to rotate them and "perspective"-ize them into 2D,
but don't know how to draw them in the right order.
just to clarify:
//my 3d shape = array of polygons
//polygon = array of vertices
//vertex = point with x,y,z
//rotation is around (0,0,0) and my view point is (0,0,something) I guess.
can anyone help?
p.s: some "catch phrases" I came up with, looking for the solution: z-buffering, ray casting (?!), plane equations, view vector, and so on - guess I need a simple to understand answer so that's why I asked this one. thanks.
p.s2: i don't mind too much about overlapping or intersecting polygons... so maybe the painter's algorthm indeed might be good. but: what is it exactly? how do I decide the distance of a polygon?? a polygon has many points.
The approach of sorting polygons and then drawing them bottom-to-top is called the "Painter's algorithm". Unfortunately the sorting step is in general an unsolvable problem, because it's possible for 3 polygons to overlap each other:
Thus there is not necessarily any polygon that is "on top". Alternate approaches such as using a Z buffer or BSP tree (which involves splitting polygons) don't suffer from this problem.
how do I decide the distance of a polygon?? a polygon has many points.
Painter's algorithm is the simplest to implement, but it works only in very simple cases because it assumes that there is only a single "distance" or z-value for each polygon (which you could approximate to be the average of z-values of all points in the polygon). Of course, this will produce wrong results if two polygons intersect each other.
In reality, there isn't a single distance value for a polygon -- each point on the surface of a polygon can be at a different distance from the viewer, so each point has its own "distance" or depth.
You already mentioned Z-buffering, and that is one way of doing this. I don't think you can implement this efficiently on a HTML canvas, but here's the general idea:
You need to maintain an additional canvas, the "z-buffer", where each pixel's colour represents the z-depth of the corresponding pixel on the main canvas.
To draw a polygon, you go through each point on its surface and draw only those points which are closer to the viewer than any previous objects, as indicated by the z-buffer.
I think you will have some ideas by investigating BSP tree ( binary spaces partition tree ), even if the algo will require to split some of your polygon in two.
Some example could be find here http://www.devmaster.net/articles/bsp-trees/ or by google for BSP tree. Posting some code as a reply is, in my opinion, not serious since is a complex topic.

Categories

Resources