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
我已经把 Plane
和 Sphere
都设为 Shape
的实例。
我尝试把球体和平面存储在同一个列表中,但是它不起作用。我知道这不应该起作用,因为 Sphere
和 Plane
是两种不同的类型,但它们都是 Shape
的实例,所以它不应该起作用吗?那我该如何将形状和平面存储在列表中呢?
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] }
]