module Data.Mesh where

import Linear

type Vec2 = V2 Double

data Mesh2D
  = Rectangle Vec2 Vec2
  | Line Vec2 Vec2
  | Triangle Vec2 Vec2 Vec2

vertices :: Mesh2D -> [Vec2]
vertices (Rectangle (V2 x y) (V2 w h)) = [V2 x y, V2 x (y+h), V2 (x+w) (y+h), V2 (x+w) y]
vertices (Line o s) = [o, o + s]
vertices (Triangle o a b) = [o, o+a, o+b]

vertexLoop :: Mesh2D -> [Vec2]
vertexLoop m = verts ++ [head verts]
  where verts = vertices m

edges :: Mesh2D -> [(Vec2, Vec2)]
edges m = let verts = vertices m
           in zip verts (tail . cycle $ verts)

centeredRectangle :: Double -> Double -> Mesh2D
centeredRectangle w h = Rectangle (V2 x y) (V2 w h)
  where x = negate (w/2)
        y = negate (h/2)

rectangle :: Double -> Double -> Mesh2D
rectangle w h = Rectangle 0 (V2 w h)

horizontalLine, verticalLine :: Double -> Mesh2D
horizontalLine length = Line (V2 ((-length)/2) 0) (V2 (length/2) 0)
verticalLine   length = Line (V2 ((-length)/2) 0) (V2 (length/2) 0)

translate :: Vec2 -> Mesh2D -> Mesh2D
translate r (Rectangle o s)  = Rectangle (o+r) s
translate r (Line o s)       = Line (o+r) s
translate r (Triangle o a b) = Triangle (o+r) a b