Created
June 16, 2014 16:06
-
-
Save patrickt/49bd8564404b56e794b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
type CGFloat = Double | |
data CGPoint = CGPoint { x :: CGFloat, y :: CGFloat } deriving (Eq, Show) | |
instance Num CGPoint where | |
(CGPoint x y) + (CGPoint x' y') = CGPoint { x = x+x', y = y+y' } | |
(CGPoint x y) * (CGPoint x' y') = CGPoint { x = x*x', y = y*y' } | |
(CGPoint x y) - (CGPoint x' y') = CGPoint { x = x-x', y = y-y' } | |
abs (CGPoint x y) = CGPoint { x = abs x, y = abs y} | |
signum = undefined | |
fromInteger = undefined | |
class (Show a, Num a) => IsPoint a where | |
zero :: a | |
distance :: a -> a -> CGFloat | |
instance IsPoint CGPoint where | |
zero = CGPoint 0 0 | |
distance = undefined -- blah blah blah | |
newtype NaturalPoint = NaturalPoint CGPoint | |
deriving (Show, Eq, Num, IsPoint) -- we inherit the definitions of equality, numeric operations, and those defined in the IsPoint class | |
newtype UnscaledPoint = UnscaledPoint CGPoint | |
deriving (Show, Eq, Num, IsPoint) | |
newtype ScaledPoint = ScaledPoint CGPoint | |
deriving (Show, Eq, Num, IsPoint) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment