问题
I'm in no way a professional programmer so, pls don't expect a sophisticated approach or language here. I'll however appreciate your advice and recommendations to materialize an algorithm which, at a later stage, I could programmatically add to my project... Here is the problem:
Imagine an arbitrary point (Point X) in space with the following properties:
- has coordinates
- lies on a 2D-surface
- is stationary
- belongs to a single area (boundary coordinates of which are also known) at any given time. Namely, it's the only "child" of its "parent" element. Again, if it doesn't sit in one area, it definitely sits inside another one!
An area is NOT a simple square, quadrangle, or a circle but instead is an irregular shape.
Now, my question is: How can I determine: (i) if Point X lies inside that particular area and NOT the adjacent one; (ii) which specific area (among a set of areas A, B, or C) the point belongs to?!? See the linked image to better visualize the problem:
PS: I perused a possibility of dealing with Point in Polygon problem (especially, the "ray casting algorithm" sounds quite smart!) but it doesn't seem to be a solution since (i) areas may be adjacent to each other; (ii) I need to determine the area a point belongs to more than it lies inside/outside of it.
Thank you very much in advance!!!
回答1:
A programmer would do it so:
A function is taking as parametres an array of areas and a point.
Make a cycle - check ALL your areas and for every one
check by the ray casting algorythm if the point belongs there.
If no, continue the cycle,
If yes, finish the function returning the number of the current area.
If you are out of areas, return -1.
Of course, you can improve the algorythm, taking into account the common borders, not repeating the counting of angles for them, but such algorythms are obviously out of your possiblilities now. And even a good programmer will start by something easy, but working.
来源:https://stackoverflow.com/questions/12332489/point-inside-an-irregular-shape