diff --git a/src/AnimationHelpers.elm b/src/AnimationHelpers.elm index a8dd938..c5ac096 100644 --- a/src/AnimationHelpers.elm +++ b/src/AnimationHelpers.elm @@ -39,3 +39,8 @@ distanceFromSpeed pixPerSec millis = fmod : Float -> Float -> Float fmod lh rh = lh - (toFloat (floor (lh / rh)) * rh) + + +rectToRectCollision : ( Point, Point ) -> ( Point, Point ) -> Bool +rectToRectCollision ( ( ax1, ay1 ), ( ax2, ay2 ) ) ( ( bx1, by1 ), ( bx2, by2 ) ) = + ax1 < bx2 && ax2 > bx1 && ay1 < by2 && ay2 > by1 diff --git a/tests/Tests.elm b/tests/Tests.elm index 9472778..188fd2b 100644 --- a/tests/Tests.elm +++ b/tests/Tests.elm @@ -2,7 +2,14 @@ module Tests exposing (..) import Test exposing (..) import Expect -import AnimationHelpers exposing (calculateAnimation, movePosition, distanceFromSpeed, fmod) +import AnimationHelpers + exposing + ( calculateAnimation + , movePosition + , distanceFromSpeed + , fmod + , rectToRectCollision + ) trimFloatX : Int -> Float -> Float @@ -107,4 +114,18 @@ all = (fmod 51934 123) 28 ] + , describe "Rectangle collision testing" + [ test "No collision" <| + \() -> + Expect.false "Expect rectangles not to collide" <| + rectToRectCollision + ( ( 10, 10 ), ( 20, 30 ) ) + ( ( 30, 50 ), ( 40, 60 ) ) + , test "Collide left" <| + \() -> + Expect.true "Expect rectangles to collide" <| + rectToRectCollision + ( ( 30, 10 ), ( 50, 30 ) ) + ( ( 10, 10 ), ( 40, 30 ) ) + ] ]