javascript - How would I implement the shoelace theorem to find the areas of multiple convex polygons created from intersecting lines? -
i creating piece of javascript code it's necessary identify every polygon created number of randomly generated intersecting lines. following screenshot give better explanation of i'm talking about:
now, need calculate area of each polygon , return largest area. approach i'm taking identify every intersection (denoted red dots) , treat them vertex of whichever polygon(s) belong to. if can somehow identify polygon(s) each vertex/intersection belongs to, arrange vertices of each polygon in clockwise direction simple apply shoelace theorem find area of each polygon.
however, i'm afraid i'm lost , have tried various (failed) methods achieve this. best way compile list of clockwise-arranged vertices each polygon? i'm working on acquiring segments associated every given intersection, , think step in right direction don't know go there. require vector work?
i can think of 1 possibility. here i've labeled each of vertices.
i'm assuming if know lines involved , intersections, can find line segments intersect @ particular point. lets start particular point, k
, , directed segment, ik
. have 4 directed segments lead end of that, ki
, kj
, kl
, , km
. interested in 2 closest to, not on, line ki
. let's focus on km
, although can same thing kj
.
(note if there more 2 lines intersecting @ point, can still find 2 closest line, 1 forming positive angle initial segment, other negative one.)
we notice ikm
positive angle, , examine segments containing m
, choosing 1 smallest positive angle km
, in case mf
, again @ f
(although there 2 choices here) fg
, , gh
, , hi
, completes 1 polygon, hexagon ikmfgh
.
going our original segment of ik
, @ our other smallest angle, ikj
, , similar process find triangle ikj
. have found polygons containing ik
.
then of course again, each other segment. need remove duplicates, or smarter not continuing analyze path when can see duplicate. (each angle in @ 1 polygon, if see angle recorded, can skip it.)
this not work if polygons weren't convex, if made lines cut through rectangle, i'm pretty sure convex.
i haven't tried code this, i'm pretty sure work.
Comments
Post a Comment