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
Run Code Online (Sandbox Code Playgroud)
我也做了两个Plane和Sphere实例Shape.
我试图将球体和平面存储在同一个列表中,但它不起作用.据我所知,它不应该工作,因为Sphere和Plane两种不同的类型,但他们的两个实例Shape,所以它不应该工作?如何在列表中存储形状和平面?
shapes :: (Shape t) => [t]
shapes = [ Sphere { center = Point [0, 0, 0], radius = 2.0 }, …Run Code Online (Sandbox Code Playgroud) haskell ×1