## 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. […] our updated definition of `containsS` for polygons from Exercise 8.4. To prove the second fact is […]

2. […] Exercise 8.4 for definitions of isLeftOf and […]

3. Wieslaw Poszewiecki says:

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)

4. Wieslaw Poszewiecki says:

Can anyone advise me how to forbid this control

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

5. Wieslaw Poszewiecki says:

Sorry,
I noticed that my previous solution does not work
if some points are on sides.

6. Wieslaw Poszewiecki says:

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)

7. admin says:

To preserve code formatting, use <pre> tags around it.

8. fero says:

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)

9. Ricky Liu says:

@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.