1 -- Originally from Kevin Glynn
2 -- Tests existential data types
6 data Coordinate3D = Coord3D {cx, cy, cz::Double}
9 -- We Represent a line by two coordinates which it passes through.
10 data Line = MkLine Coordinate3D Coordinate3D
13 class PictureObject pot where
15 -- Returns ordered (rel to 0 0 0) of points where the object
16 -- intersects the given line.
17 intersectLineObject :: pot -> Line -> [Coordinate3D]
19 getPictureName :: pot -> String
22 Sphere Coordinate3D -- Centre
24 Double -- ambient coeff
25 Double -- diffuse coeff
26 Double -- specular coeff
27 Double -- phong specular exponent
29 intersectLineSphere :: Sphere -> Line -> [Coordinate3D]
30 intersectLineSphere sp line = []
32 instance PictureObject Sphere where
33 intersectLineObject = intersectLineSphere
34 getPictureName _ = "Sphere"
37 Cube Coordinate3D -- Origin corner
38 Coordinate3D -- Opposite corner
39 Double -- ambient coeff
40 Double -- diffuse coeff
41 Double -- specular coeff
42 Double -- phong specular exponent
45 intersectLineCube :: Cube -> Line -> [Coordinate3D]
46 intersectLineCube cube line = []
48 instance PictureObject Cube where
49 intersectLineObject = intersectLineCube
50 getPictureName _ = "Cube"
53 data GenPic = forall pot. (PictureObject pot) => MkGenPic pot
56 sphere = Sphere (Coord3D 1 1 1) 1 1 1 1 1
59 cube = Cube (Coord3D 1 1 1) (Coord3D 2 2 2) 1 1 1 1
62 obj_list = [MkGenPic sphere, MkGenPic cube]
64 putName :: PictureObject pot => pot -> IO ()
65 putName x = putStr $ getPictureName x
69 main = do { sequence $ map put_it obj_list }
71 put_it (MkGenPic s) = putStr (getPictureName s)