Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Consider three two-dimensional points a, b, and c. If we look at the angle
  2. --formed by the line segment from a to b and the line segment from b to c,
  3. --it either turns left, turns right, or forms a straight line.
  4. --Define a Direction data type that lets you represent these possibilities.
  5. data Direction a = ToRight
  6.           | ToLeft
  7.           | ToStraight
  8.           | None
  9.             deriving (Eq, Show)
  10.  
  11. -- Write a function that calculates the turn made by three 2D points and returns a Direction.
  12. dirCalc :: Ord a => Fractional a => Eq a => Num a => (a, a) -> (a, a) -> (a, a) -> Direction a
  13. dirCalc (0,0) (0,0) (0,0) = None
  14. dirCalc (x1, y1) (x2, y2) (x3, y3) = if k1 > k2 then ToRight
  15.                               else if k1 == k2 then ToStraight
  16.                               else ToLeft
  17.                           where k1 = (y2 - y1) / (x2 - x1)
  18.                                 k2 = (y3 - y2) / (x3 - x2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement