What are the algorithms for determining if a point is inside an arbitrary closed shape or not?
-
If you ask a human, they will instantaneously be able to tell if the black point lies inside or outside the larger red shape. A computer program for the same would be to actually ENCODE the inside/outside information (i.e., for any given point shaded red, check if its X,Y co-ordinates match with that of the black point). For encoding one can select a pivot point on the boundary of red shape and draw a line and rotate it 360 degree and note down all the pixels that are present on the line joining the pivot point and another boundary point at which the line intersects the boundary. (Actually, for each rotated line, one can check if the black point lies on it.) Are there any better algorithms, (maybe more human-like)? You may be interested in the followup question
-
Answer:
Testing if a point is inside a polygon is pretty hard for a human if the polygon is a bit more complex. (picture from http://www.ics.uci.edu/~eppstein/161/960307.html) O(n) algorithms: Ray casting: shoot a ray from your point and see how many sides of the polygon it intersects. If the number is even, your point is outside the polygon. If it's odd your point is inside. Winding number: go around the sides of the polygon and sum up the sum of signed angles the points on the sides make with your current point. The result should be 2Ï if the point is inside and 0 if the point is outside Convex polygon/Starred polygon O(log n) algorithm: Choose a vertex of the convex polygon. Shoot n-1 rays from this vertex to every other vertex. Use binary search to find where your query point lies between two consecutive rays using their angle. Then you just need to test if the point is within a triangle. Grid solution: For a fixed precision split the plane into squares and compute if each square is inside or outside the polygon. Quadtree solution O(log U)?: Build a quadtree to represent the plane for some fixed precision. Each square in the quadtree is either totally inside, totally outside the polygon or intersects the polygon. If it intersects the polygon you either split it in 4 sub squares, or stop because the level of precision is small enough. Vertical slabs (O(n^2) preprocessing O(log n) query): Draw a vertical line through each vertex of the polygon. This splits the plane in n vertical slabs. For each slab compute the segments that cross the slab and sort them by the y of their middle point. For a given query point first use binary search to find the slab your point falls in. Within the slab you can binary search for the lowest segment that crosses the slab and is above our query point. Now you can count how many segments are intersected by a vertical ray that starts from the query point and know if you are inside the polygon or not. This approach can be improved to O(n) pre processing and O(log n) query time. There's another clever approach that splits at each level one triangle into 3 smaller ones, but I don't remember the details exactly.
Cosmin Negruseri at Quora Visit the source
Other answers
Well..the human visual system is really complicated (a fully trained neuroscientist will be able to answer this better than I can) but I will take a crack at it. But you are correct in saying a human can immediately identify where the black dot is in that image. Our eyes and brain have some unique features that make it easy to identify. There are (in general) two streams of visual processing: the ventral ("what") and dorsal ("where") streams. The dorsal stream of visual processing would help us identify where the black dot is above. Below is a list of some features of natural images (1), our visual inputs (2 & 3), and spatial attention (4) that help our brain identify images. Again, this is a gross simplification, but hopefully this will help. 1) Natural images (a painting vs. random static on your television) tend to exhibit redundancy. What I mean by this is that if you take a given pixel in the above image, chances are a red pixel will be surrounded by other red pixels, black pixels surrounded by other black pixels, and white pixels surrounded by other white pixels. 2) The individual cells that make up our visual inputs are center-surround cells. Center surround cells come in two flavors, "off center" and "on center". On center cells respond strongly to light spots against dark backgrounds. Off center cells respond strongly to dark spots against light backgrounds. These cells also essentially act to "compress" visual information so that it can be transmitted via the optic nerve. That's a whole different thing though. 3) Emphasize edges: The input from center-surround cells is processed in the visual cortex to emphasize edges and de-emphasize things with constant illumination. However, there is also attention, specifically spatial attention, e.g. our eyes / brain tell us where to look. One thing that makes stimuli stand out is their salience. 4) Salience: "Salient" stimuli are those which stand out the most. Things that are salient tend to stand out from their surroundings. The bright red shape, for example, is highly salient because it is oddly shaped and, well, bright red. When looking at that image, the first thing we see is the giant red blob. I know this answer is a little scattered-however I'm off of neuro right now and studying for other things, and unfortunately the neuroscience behind vision and spatial attention is really really hard . Hopefully, the bits of knowledge in my answer can help you craft some kind of algorithm. Also, as a final note, there is a researcher at Georgetown who is a true genius in the field of vision / visual processing / and neuroscience in general. His name is Dr. Reisenhuber. Here is his lab's website: http://riesenhuberlab.neuro.georgetown.edu/publications/
Daniel R. Layon
I was going to answer this with my limited knowledge, but I can't top this: http://stackoverflow.com/questions/217578/point-in-polygon-aka-hit-test
Amit Ruparel
Separating Axis Theorem is a very popular collision detection method in gaming. http://www.geometrictools.com/Documentation/MethodOfSeparatingAxes.pdf
Binh Nguyen
Fastest way, and quite simple and efficient: - Create a binary lookup table with the sufficient resolution (it will depend on the input space of data), so you put to 1 the ''pixels'' that lay inside the polygon. When a new point must be checked, their X,Y data will be the index value of the matrix (maybe you will have to quantizate it first), and you just have to check the stored value. It is only 1 reading operation. I think this is the most intuitive way and the nearer one to our brain's functioning. There is not math in it. It only encodes the red polygon as a group of pixels that are red, in a matrix that the other pixels are white. Every digital image is a lookup table that encodes the colour values on every position. And our brain also encodes the color values of every position.
Aitor Olano
Calculate sum of signed angles: pt1-pt2 and pt2-pt3 and etc. Sum = 0 if given point is outside the polygon; Sum=360 degree if inside. (assuming polygon doesn't self intersect)
Anonymous
Use the winding number or ray casting. I'm not sure about something more human-like.
Kevin Matzen
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=geometry3 Pretty neat way of doing it..
Pratyush Rathore
Related Q & A:
- What Is The Geometric Meaning Of Third Derivative Of A Function At A Point?Best solution by Mathematics
- How to center a window on a point?Best solution by Geographic Information Systems
- How to determine point is Inside the Triangle?Best solution by Mathematics
- What do you think of Jared Diamonds five-point collapse theory In his book collapse?Best solution by Yahoo! Answers
- What does it mean if there is a yellow/orange/brown spot on the inside of an eggshell?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.