Exercise 8.4


The simplest way to allow vertices in clockwise or anticlockwise order is to define isRightOf analogously to isLeftOf, and combine them with a boolean expression.

isLeftOf :: Coordinate -> Ray -> Bool
(px,py) `isLeftOf` ((ax,ay), (bx,by))
    = let (s,t) = (px-ax, py-ay)
          (u,v) = (px-bx, py-by)
      in s * v >= t * u

isRightOf :: Coordinate -> Ray -> Bool
(px,py) `isRightOf` ((ax,ay), (bx,by))
    = let (s,t) = (px-ax, py-ay)
          (u,v) = (px-bx, py-by)
      in s * v <= t * u

(Polygon pts) `containsS` p
    = let leftOfList = map isLeftOfp (zip pts (tail pts ++ [head pts]))
          isLeftOfp p' = isLeftOf p p'
          rightOfList = map isRightOfp (zip pts (tail pts ++ [head pts]))
          isRightOfp p' = isRightOf p p'
      in and leftOfList || and rightOfList

9 responses to “Exercise 8.4”

  1. Seems this also works:

    (Polygon pts) `containsS` p
    = let leftOfList = map (isLeftOf p)
    (zip pts (tail pts ++ [head pts]))
    in and leftOfList || not (or leftOfList)

  2. Can anyone advise me how to forbid this control
    to eat leading left spaces?

    >(Polygon pts) `containsS` p
    > = let leftOfList = map (isLeftOf p) (zip pts (tail pts ++ [head pts]))
    > in and leftOfList || not (or leftOfList)

  3. This one seems to work also for sides

    >(px,py) `isLeftOf` ((ax,ay),(bx,by))
    > = let (s,t) = (px-ax, py-ay)
    > (u,v) = (px-bx, py-by)
    > in s*v – t*u

    >(Polygon pts) `containsS` p
    > = let leftOfList = map (isLeftOf p) (zip pts (tail pts ++ [head pts]))
    > in and (map (>=0) leftOfList) || and (map (<=0) leftOfList)

  4. This is my solution and works even with [] because of and function:

    (Polygon pts) `containsS` p = let leftOfList = map (isLeftOf p)
    (zip pts (tail pts ++ [head pts]))
    in and (map (==head leftOfList) leftOfList)

  5. @fero, your solution is similar to the first one given by Wieslaw. As Wieslaw already said it did not work when p lies on one of the edges of the polygon.

Leave a Reply

Your email address will not be published. Required fields are marked *