Guest User

Untitled

a guest
Sep 7th, 2023
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2. import Data.Function
  3.  
  4. -- Define a data type to represent 2D points
  5. data Point = Point { x :: Double, y :: Double } deriving (Eq, Show)
  6.  
  7. -- Define a data type for representing a light source
  8. data Light = Light { position :: Point, intensity :: Double } deriving Show
  9.  
  10. -- Define a data type for representing obstacles (assumed to be square)
  11. data Obstacle = Obstacle { center :: Point, size :: Double } deriving Show
  12.  
  13. -- Function to simulate lighting and shadows
  14. simulateLight :: Point -> Double -> [Obstacle] -> String
  15. simulateLight lightPos lightIntensity obstacles =
  16.     let gridSize = 20  -- Size of the 2D grid
  17.         -- Create a grid of points
  18.         grid = [[Point (fromIntegral x) (fromIntegral y) | x <- [0..gridSize-1]] | y <- [0..gridSize-1]]
  19.         -- Compute the illumination status for each point on the grid
  20.         illuminatedGrid = map (map (illuminate lightPos lightIntensity obstacles)) grid
  21.     in unlines $ map (concatMap cellChar) illuminatedGrid
  22.  
  23. -- Function to convert illumination status to ASCII characters
  24. cellChar :: Bool -> String
  25. cellChar True = " "  -- Empty space (illuminated)
  26. cellChar False = "#" -- Hash symbol (in shadow)
  27.  
  28. -- Function to determine if a point is illuminated or in shadow
  29. illuminate :: Point -> Double -> [Obstacle] -> Point -> Bool
  30. illuminate lightPos lightIntensity obstacles point =
  31.     let ray = Ray lightPos point
  32.         -- Find intersections between the ray and obstacles
  33.         intersections = filter (isIntersection ray) obstacles
  34.         distanceToPoint = distance lightPos point
  35.         -- Check if the point is blocked by any obstacle
  36.         blocked = any (\obs -> distanceToPoint > distance (center obs) point) intersections
  37.     in not blocked
  38.  
  39. -- Define a data type for representing a ray
  40. data Ray = Ray { origin :: Point, destination :: Point } deriving Show
  41.  
  42. -- Function to check if a ray intersects with an obstacle
  43. isIntersection :: Ray -> Obstacle -> Bool
  44. isIntersection ray obs =
  45.     let d = destination ray `sub` origin ray
  46.         f = origin ray `sub` center obs
  47.         a = d `dot` d
  48.         b = 2 * (f `dot` d)
  49.         c = (f `dot` f) - (size obs) ** 2
  50.         discriminant = b * b - 4 * a * c
  51.     in discriminant >= 0
  52.  
  53. -- Function to subtract two points
  54. sub :: Point -> Point -> Point
  55. sub p1 p2 = Point (x p1 - x p2) (y p1 - y p2)
  56.  
  57. -- Function to compute the dot product of two points
  58. dot :: Point -> Point -> Double
  59. dot p1 p2 = x p1 * x p2 + y p1 * y p2
  60.  
  61. -- Function to compute the distance between two points
  62. distance :: Point -> Point -> Double
  63. distance p1 p2 = sqrt ((x p1 - x p2) ** 2 + (y p1 - y p2) ** 2)
  64.  
  65. -- Main function to run the simulation
  66. main :: IO ()
  67. main = do
  68.     let light = Light (Point 10.0 10.0) 1.0
  69.         obstacles = [Obstacle (Point 5.0 5.0) 2.0, Obstacle (Point 15.0 15.0) 2.0]
  70.         result = simulateLight (position light) (intensity light) obstacles
  71.     putStrLn result
  72.  
Advertisement
Add Comment
Please, Sign In to add comment