Recently
vroom dropped into CB looking for a some code that would
answer the question that given a polygon and a point does point live
inside the polygon. I am sure
Tim has found or built his
solution by now but it got to thinking since I was in a an ojective
kind of mood I might take a crack for my own amusement.
First I dug up an algorithm1 and came up with some classes.
|
|
|
|
Polygon
|
SortedPoints[1..n]
|
|
Next we need, as per algorithm three methods/functions:
- inside
- intersect -- used by inside
- same_line -- used by intersect
Now where to stick methods?? Well inside could certainly live inside
Polygon but both same_line and intersect deal with
Lines(plural). So create a Lines class ?? Maybe just christian
them all Helpers and stick 'em in a GeometryHelper class.
I have a sneaking suspicion there is a twisted Perly path to this
design and that is why I am seeking a sober second opinion from the
Monk collective.
Note
- This is not a solicitation for code.
- If you suspect this is homework you are correct. In my spare time
(thats the time I have after; 'tending to the needs of my wife and 7
kids, sheperding a small pod of programmers and trying to keep my
customers happy) I am doing a double PHD in Philosphy (Thesis: What is
the Point) and Physics (Thesis: Where is the Point)
1 ... given a polygon represented as an array of points and
another point, determine whether the point is inside or outside. Draw
a line segment form the point long enough thats its endpoint is
guaranteed to be outside the polygon and count the number of lines
from the polygon it crosses. If LX mod 2 != 0 then point is inside
polygon.
pp 312-317
Algorithms, Sedgewick, Addison-Wesley, 1983
mitd-Made in the Dark
'My favourite colour appears to be grey.'
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.