8.hs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. {-# LANGUAGE OverloadedStrings #-}
  2. import Data.List
  3. import Text.LaTeX
  4. import Text.LaTeX.Base.Class
  5. import Text.LaTeX.Base.Syntax
  6. import Text.LaTeX.Packages.Graphicx
  7. import Text.LaTeX.Packages.Geometry
  8. import Util
  9. import Vec
  10. main :: IO ()
  11. main = printdoc doc
  12. doc :: Monad m => LaTeXT_ m
  13. doc = do
  14. mapM_ rendere $ polymap (vadd (0.0, 0.0, 1.8)) $ polymap vtransform $ polyrotate (cubes 0.9) (0.6, 0.9, 0.32)
  15. rendere :: LaTeXC l => (Vec3, Vec3) -> l
  16. rendere (a, b) =
  17. if d > 0.001 then textblock' (vw $ fst c) (vw $ snd c) $ rotatebox' ((-t ) * 180 / pi) $ fontsize (vw $ d * 0.222) (vw 0) "collapsing"
  18. else ""
  19. where
  20. c = vadd a' $ vdiv (vsub b' a') 2
  21. d = vdist a' b'
  22. t = atan2 (snd $ vsub b' a') (fst $ vsub b' a')
  23. a' = vadd (0.5, sqrt 2 / 2) $ vmul (vproject a) 1
  24. b' = vadd (0.5, sqrt 2 / 2) $ vmul (vproject b) 1
  25. vtransform :: Vec3 -> Vec3
  26. vtransform v@(x, y, z)
  27. | x < 0 = (vdiv v (vmag v))
  28. | x >= 0 = (vmul v (vmag v))
  29. polymap :: (Vec3 -> Vec3) -> [(Vec3, Vec3)] -> [(Vec3, Vec3)]
  30. polymap f = fmap (\(a, b) -> (f a, f b))
  31. polyfilter :: (Vec3 -> Bool) -> [(Vec3, Vec3)] -> [(Vec3, Vec3)]
  32. polyfilter f = filter (\(a, b) -> f a || f b)
  33. polyrotate :: [(Vec3, Vec3)] -> Vec3 -> [(Vec3, Vec3)]
  34. polyrotate vs t = polymap (flip v3rotate t) vs
  35. cubes :: Double -> [(Vec3, Vec3)]
  36. cubes r = connect dist $ vcubes r
  37. where
  38. connect dist xs = [(x, y) | (x:ys) <- tails xs, y <- ys, vdist x y < dist]
  39. dist = r * 0.257
  40. vcubes :: Double -> [Vec3]
  41. vcubes r = fmap (flip vmul r) vs
  42. where
  43. vs = [(x, y, z) | x <- a, y <- a, z <- b]
  44. a = [-1, -3/4 .. 1]
  45. b = [-1, -3/4 .. 1]
  46. phi :: Double
  47. phi = (1 + sqrt 5) / 2