How to resolve the algorithm Determine if two triangles overlap step by step in the Haskell programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Determine if two triangles overlap step by step in the Haskell programming language
Table of Contents
Problem Statement
Determining if two triangles in the same plane overlap is an important topic in collision detection.
Determine which of these pairs of triangles overlap in 2D:
Optionally, see what the result is when only a single corner is in contact (there is no definitive correct answer):
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Determine if two triangles overlap step by step in the Haskell programming language
The code you posted checks if two triangles overlap. It does this by checking if any of the vertices of one triangle are inside the other triangle, or if any of the midlines of one triangle intersect the other triangle.
Here's a breakdown of the code:
- The
isOverlapping
function takes two triangles as input and returns True if they overlap, and False otherwise. - The
vertexInside
function checks if any of the vertices of one triangle are inside the other triangle. It does this by calling theisInside
function for each vertex of one triangle and each triangle of the other triangle. - The
isInside
function checks if a point is inside a triangle. It does this by computing the area of the triangle formed by the point and the three vertices of the triangle. If the area is positive, then the point is inside the triangle. - The
midLineInside
function checks if any of the midlines of one triangle intersect the other triangle. It does this by calling theisInside
function for each midline of one triangle and each triangle of the other triangle. - The
midPoints
function computes the midpoints of the three sides of a triangle. - The
intersections
function computes the intersection point of two lines. - The
midLines
function computes the three midlines of a triangle. - The
line
function computes the equation of a line given three points.
The test
function is used to test the isOverlapping
function. It does this by applying the isOverlapping
function to a list of pairs of triangles.
Source code in the haskell programming language
isOverlapping :: Triangle Double -> Triangle Double -> Bool
isOverlapping t1 t2 = vertexInside || midLineInside
where
vertexInside =
any (isInside t1) (vertices t2) ||
any (isInside t2) (vertices t1)
isInside t = (Outside /=) . overlapping t
midLineInside =
any (\p -> isInside t1 p && isInside t2 p) midPoints
midPoints =
[ intersections l1 l2 | l1 <- midLines t1
, l2 <- midLines t2 ]
intersections (a1,b1,c1) (a2,b2,c2) =
( -(-b2*c1+b1*c2)/(a2*b1-a1*b2)
, -(a2*c1-a1*c2)/(a2*b1-a1*b2) )
midLines (Triangle a b c) =
[line a b c, line b c a, line c a b]
line (x,y) (ax, ay) (bx, by) =
(ay+by-2*y, -ax-bx+2*x, -ay*x-by*x+ax*y+bx*y)
test = map (uncurry isOverlapping)
[ (Triangle (0,0) (5,0) (0,5), Triangle (0,0) (5,0) (0,6))
, (Triangle (0,0) (0,5) (5,0), Triangle (0,0) (0,5) (5,0))
, (Triangle (0,0) (5,0) (0,5), Triangle (-10,0) (-5,0) (-1,6))
, (Triangle (0,0) (5,0) (2.5,5), Triangle (0,4) (2.5,-1) (5,4))
, (Triangle (0,0) (1,1) (0,2), Triangle (2,1) (3,0) (3,2))
, (Triangle (0,0) (1,1) (0,2), Triangle (2,1) (3,-2) (3,4))
, (Triangle (0,0) (1,0) (0,1), Triangle (1,0) (2,0) (1,1))]
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Oforth programming language
You may also check:How to resolve the algorithm Pythagorean triples step by step in the Forth programming language
You may also check:How to resolve the algorithm Ascending primes step by step in the Sidef programming language
You may also check:How to resolve the algorithm Sudan function step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Nim game step by step in the Prolog programming language