data Plane = Plane { point :: Point, normal :: Vector Double }
data Sphere = Sphere { center :: Point, radius :: Double }
class Shape s where
intersect :: s -> Ray -> Maybe Point
surfaceNormal :: s -> Point -> Vector Double
I have also made both Plane
and Sphere
instances of Shape
.
I'm trying to store spheres and planes in the same list, but it doesn't work. I understand that it shouldn't work because Sphere
and Plane
are two different types, but they are both instances of Shape
, so shouldn't it work? How would I store shapes and planes in a list?
shapes :: (Shape t) => [t]
shapes = [ Sphere { center = Point [0, 0, 0], radius = 2.0 },
Plane { point = Point [1, 2, 1], normal = 3 |> [0.5, 0.6, 0.2] }
]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…