Advertisement
Guest User

Untitled

a guest
Jun 13th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Geometry where
  2.  
  3.  
  4. data Point = Pt {
  5.   pointX :: Double,  
  6.   pointY :: Double
  7. } deriving Show
  8.  
  9. data Vector = Vec {
  10.   vectorX :: Double,
  11.   vectorY :: Double
  12. } deriving Show
  13.  
  14. data SourcedVector = SVec {
  15.   vectorSource :: Point,
  16.   vectorDirection :: Vector
  17. } deriving Show
  18.  
  19. data PlaneVector = PVec {
  20.   planeFrom :: Point,
  21.   planeTo :: Point
  22. } deriving Show
  23.  
  24.  
  25. sourcedToPlane :: SourcedVector -> PlaneVector
  26. sourcedToPlane (SVec (Pt fx fy) (Vec dx dy)) =
  27.       (PVec (Pt fx fy) (Pt (fx + dx) (fy + dy)))
  28.      
  29. planeToSourced :: PlaneVector -> SourcedVector
  30. planeToSourced (PVec (Pt fx fy) (Pt tx ty)) =
  31.       (SVec (Pt fx fy) (Vec (tx - fx) (ty - fy)))
  32.  
  33. makeSourced :: Point -> Point -> SourcedVector
  34. makeSourced f t = planeToSourced (PVec f t)
  35.  
  36.  
  37. dest :: SourcedVector -> Point
  38. dest (SVec (Pt fx fy) (Vec dx dy)) = Pt (fx + dx) (fy + dy)
  39.  
  40. dist :: Point -> Point -> Double
  41. dist (Pt a b) (Pt c d) = sqrt ((a - c) * (a - c) + (b - d) * (b - d))
  42.  
  43. mult :: SourcedVector -> Double -> SourcedVector
  44. mult (SVec s (Vec dx dy)) a = (SVec s newDirection)
  45.     where newDirection = Vec (a*dx) (a*dy)
  46.  
  47. alpha :: SourcedVector -> Point -> Double
  48. alpha (SVec (Pt x _) (Vec dx _)) (Pt a _) = (a - x) / dx
  49.  
  50. scp :: Vector -> Vector -> Double
  51. scp (Vec fx fy) (Vec sx sy) = fx * sx + fy * sy
  52.  
  53. vcp :: Vector -> Vector -> Double
  54. vcp (Vec x y) (Vec a b) = x * b - y * a
  55.  
  56.  
  57.  
  58. a = (Pt 2.0 2.0)
  59. b = (Pt 6.0 10.0)
  60.  
  61. c = (Pt 7.0 4.0)
  62. d = (Pt 4.0 6.0)
  63.  
  64.  
  65. u = makeSourced a b
  66. v = makeSourced c d
  67.  
  68. e = (Pt 3 (-1))
  69. f = (Pt 7 2)
  70.  
  71.  
  72. g = Pt 14 4
  73. h = Pt 16 10
  74. i = Pt 20 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement