Game Development Reference
In-Depth Information
result
<<
r_x
result
<<
r_y
end
result
end
# ...
end
We can now calculate edges of our bounding box, but we need one more function which
tells if point is inside a polygon. This problem has been solved million times before, so just
poke the internet for it and drink from the information firehose until you understand how
to do this.
If you wasn't familiar with the term yet, by now you should discover what
vertex
is. In
geometry, a vertex (plural vertices) is a special kind of point that describes the corners or
intersections of geometric shapes.
Here's what I ended up writing:
module
Utils
# ...
# http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/
pnpoly.html
def
self
.
point_in_poly
(testx, testy,
*
poly)
nvert
=
poly
.
size
/ 2
# Number of vertices in poly
vertx
= []
verty
= []
poly
.
each_slice(
2
)
do
|
x, y
|
vertx
<<
x
verty
<<
y
end
inside
=
false
j
=
nvert
- 1
(
0.
.nvert
- 1
)
.
each
do
|
i
|
if
(((verty
[
i
] >
testy)
!=
(verty
[
j
] >
testy))
&&
(testx
<
(vertx
[
j
] -
vertx
[
i
]
)
*
(testy
-
verty
[
i
]
)
/
(verty
[
j
] -
verty
[
i
]
)
+
vertx
[
i
]
))
inside
= !
inside
end
j
=
i
end
inside
end
# ...