To check for ray-triangle collisions, we can first see if the ray collides with the triangle's plane. If it does, we then check if the intersection point is on the same side for all triangle sides. If true, this means that the point is inside the triangle. This procedure is analogous for rectangles and other convex figures.
This is a list of vertexes belonging to a rectangle (counter-clockwise ordered):
vertexes = [ll, lr, ur, ul]
and I want to generate a list with all its sides; that is, all adjacent pairs of vertexes:
vertexPairs = [(ll, lr), (lr, ur), (ur, ul), (ul, ll)]
(note that the last vertex, ul, also pairs with the first one, ll)
How can I lazily generate such a list for a generic convex geometric figure, assuming I have an ordered list of its vertexes?
The idea is to feed each of the pairs to a function, isInside, and check if all of its return values are the same. This is what I'm doing:
1. vertexes = [<list of vertexes>]
2. vertexPairs = ???
3. results = map (\(v1, v2) -> isInside point v1 v2) vertexPairs
4. allequal = all (== head results) (tail results)
Because Haskell is lazy, if a call to isInside returns a value that differs from the first call's return value, the call to all ends (line 4). Similarly, I wanted a way to generate the vertexPairs list in a lazy way.
As I was writing this question, I thought of a possible solution to generate the pairs:
vertexPairs = zip (vertexes) (tail vertexes ++ [head vertexes])
Though tikhon has answered most questions, if you want to write it in a slightly prettier way, you could do
vertexPairs v = zip v (tail $ cycle v)
This works since zip
stops generating a list when one of its arguments "run out"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With