Chapter 8 A Module of Regions The Region

  • Slides: 17
Download presentation
Chapter 8 A Module of Regions

Chapter 8 A Module of Regions

The Region Data Type w A region represents an area on the two-dimensional Cartesian

The Region Data Type w A region represents an area on the two-dimensional Cartesian plane. w It is represented by a tree-like data structure. data Region = Shape | Translate Vector Region | Scale Vector Region | Complement Region | Region `Union` Region | Region `Intersect` Region | Empty deriving Show type Vector = (Float, Float) ------- primitive shape translated region scaled region inverse of region union of regions intersection of regions

Questions about Regions w Why is Region tree-like? w What is the strategy for

Questions about Regions w Why is Region tree-like? w What is the strategy for writing functions over regions? w Is there a fold-function for regions? n How many parameters does it have? n What is its type? w Can one define infinite regions? w What does a region mean?

Sets and Characteristic Functions w How can we represent an infinite set in Haskell?

Sets and Characteristic Functions w How can we represent an infinite set in Haskell? E. g. : n the set of all even numbers n the set of all prime numbers w We could use an infinite list, but then searching it might take a very long time! (Membership becomes semi-decidable. ) w The characteristic function for a set containing elements of type z is a function of type z -> Bool that indicates whether or not a given element is in the set. Since that information completely characterizes a set, we can use it to represent a set: type Set a = a -> Bool w For example: even : : Set Integer even x = (x `mod` 2) == 0 -- Integer -> Bool

Combining Sets w If sets are represented by characteristic functions, then how do we

Combining Sets w If sets are represented by characteristic functions, then how do we represent the: n union of two sets? n intersection of two sets? n complement of a set? w In-class exercise – define the following Haskell functions: union s 1 s 2 = intersect s 1 s 2 = complement s = w We will use these later to define similar operations on regions.

Why Regions? Regions (as defined in the text) are interesting because: n They allow

Why Regions? Regions (as defined in the text) are interesting because: n They allow us to build complex “shapes” from simpler ones. n They illustrate the use of tree-like data structures. n n They “solve” the problem of having rectangles and ellipses centered about the origin. Their meaning can be given as characteristic functions, since a region denotes the set of points contained within it.

Characteristic Functions for Regions w We define the meaning of regions by a function:

Characteristic Functions for Regions w We define the meaning of regions by a function: contains. R : : Region -> Coordinate -> Bool type Coordinate = (Float, Float) w Note that contains. R r : : Coordinate -> Bool, which is a characteristic function. So contains. R “gives meaning to” regions. w Another way to see this: contains. R : : Region -> Set Coordinate w We can define contains. R recursively, using pattern matching over the structure of a Region. w Since the base cases of the recursion are primitive shapes, we also need a function that gives meaning to primitive shapes; we will call this function contains. S.

Rectangle s 1 s 2 `contains. S` (x, y) = let t 1 =

Rectangle s 1 s 2 `contains. S` (x, y) = let t 1 = s 1/2 t 2 = s 2/2 in -t 1<=x && x<=t 1 && -t 2<=y && y<=t 2 s 1 t 2 s 2

Ellipse r 1 r 2 `contains. S` (x, y) = (x/r 1)^2 + (y/r

Ellipse r 1 r 2 `contains. S` (x, y) = (x/r 1)^2 + (y/r 2)^2 <= 1 r 2 r 1

The Left Side of a Line For a ray directed from point a to

The Left Side of a Line For a ray directed from point a to point b, a point p is to the left of the ray (facing from a to b) when: b = (bx, by) p = (px, py) is. Left. Of : : Coordinate -> Ray -> Bool (px, py) `is. Left. Of` ((ax, ay), (bx, by)) = let (s, t) = (px-ax, py-ay) (u, v) = (px-bx, py-by) a = (ax, ay) in s*v >= t*u type Ray = (Coordinate, Coordinate)

Polygon A point p is contained within a (convex) polygon if it is to

Polygon A point p is contained within a (convex) polygon if it is to the left of every side, when they are followed in counterclockwise order. p Polygon pts `contains. S` p = let shiftpts = tail pts ++ [head pts] left. Of. List = map is. Left. Ofp (zip pts shiftpts) is. Left. Ofp p' = is. Left. Of p p' in and left. Of. List

Right Triangle Rt. Triangle s 1 s 2 `contains. S` p = Polygon [(0,

Right Triangle Rt. Triangle s 1 s 2 `contains. S` p = Polygon [(0, 0), (s 1, 0), (0, s 2)] `contains. S` p (0, s 2) s 2 (0, 0) s 1 (s 1, 0)

Putting it all Together contains. S : : Shape -> Vertex -> Bool Rectangle

Putting it all Together contains. S : : Shape -> Vertex -> Bool Rectangle s 1 s 2 `contains. S` (x, y) = let t 1 = s 1/2; t 2 = s 2/2 in -t 1<=x && x<=t 1 && -t 2<=y && y<=t 2 Ellipse r 1 r 2 `contains. S` (x, y) = (x/r 1)^2 + (y/r 2)^2 <= 1 Polygon pts `contains. S` p = let shiftpts = tail pts ++ [head pts] left. Of. List = map is. Left. Ofp (zip pts shiftpts) is. Left. Ofp p' = is. Left. Of p p' in and left. Of. List Rt. Triangle s 1 s 2 `contains. S` p = Polygon [(0, 0), (s 1, 0), (0, s 2)] `contains. S` p

Defining contains. R Using Recursion contains. R : : Region -> Vertex -> Bool

Defining contains. R Using Recursion contains. R : : Region -> Vertex -> Bool Shape s `contains. R` p = s `contains. S` p Translate (u, v) r `contains. R` (x, y) = r `contains. R` (x-u, y-v) Scale (u, v) r `contains. R` (x, y) = r `contains. R` (x/u, y/v) Complement r `contains. R` p = not (r `contains. R` p) r 1 `Union` r 2 `contains. R` p = r 1 `contains. R` p || r 2 `contains. R` p r 1 `Intersect` r 2 `contains. R` p = r 1 `contains. R` p && r 2 `contains. R` p Empty `contains. R` p = False

An Algebra of Regions w Note that, for any r 1, r 2, and

An Algebra of Regions w Note that, for any r 1, r 2, and r 3: (r 1 `Union` (r 2 `Union` r 3)) `contains. R` p if and only if: (r 1 `Union` r 2) `Union` r 3)) `contains. R` p which we can abbreviate as: (r 1 `Union` (r 2 `Union` r 3)) ((r 1 `Union` r 2) `Union` r 3) w In other words, Union is associative. w We can prove this fact via calculation.

Proof of Associativity (r 1 `Union` (r 2 `Union` r 3)) `contains. R` p

Proof of Associativity (r 1 `Union` (r 2 `Union` r 3)) `contains. R` p (r 1 `contains. R` p) || ((r 2 `Union` r 3) `contains. R` p) (r 1 `contains. R` p) || ((r 2 `contains. R` p) || (r 3 `contains. R` p)) ((r 1 `contains. R` p) || (r 2 `contains. R` p)) || (r 3 `contains. R` p) ((r 1 `Union` r 2) `contains. R` p) || (r 3 `contains. R` p) ((r 1 `Union` r 2) `Union` r 3) `contains. R` p (Note that the proof depends on the associativity of (||), which can also be proved by calculation, but we take it as given. )

More Axioms w w There are many useful axioms for regions: 1) Union and

More Axioms w w There are many useful axioms for regions: 1) Union and Intersect are associative. 2) Union and Intersect are commutative. 3) Union and Intersect are distributive. 4) Empty and univ = Complement Empty are zeros for Union and Intersect, respectively. 5) r `Union` Complement r univ and r `Intersect` Complement r Empty This set of axioms captures what is called a boolean algebra.