W1525 Containment
Prerequisites[edit]
Research[edit]
 Set Theory (Wikipedia)
 Set Theory for Computer Science (Glynn Winskel, University of Cambridge)
Background[edit]
Set Basics[edit]
By the 1930's, through the work of Godel, Church, Turing and others, it was realized that Set Theory relied on an even more basic concept, that of computability. Computer Science continues to be inspired by Set Theory and understanding Set Theory should facilitate your ability to think abstractly. It is, by its nature, independent of yet critical for programming.
A set is an unordered collection of objects. The objects are referred to as elements or members of the set.
Symbols  Definition 

The empty set  
Another way of symbolizing the empty set  
The set containing the elements  
is a member of the set  
is NOT a member of the set  
The set of natural numbers  
or  The set of natural numbers with zero (whole numbers) 
The set of integers (negative, zero, and positive)  
The set of real numbers  
The set X is a subset of the set Y  
The set X is NOT a subset of the set Y 
A set is termed a subset of set iff every element of is an element of . Formally:
Two sets and are equal iff every element of is an element of and vice versa. Formally:
Boolean Algebra of Sets[edit]
Assume the existence of the universal set such that . The following operations are defined:
Symbols  Name  Definition 

Union  
Intersection  
Complement 
Set Union[edit]
Set Intersection[edit]
Set Complement[edit]
Sets in Swift[edit]
Sets are useful when we want to check efficiently to determine whether an element is a member of a set AND the order of the elements in the collection doesn't matter.
The most useful methods are:
Method  Purpose 

contains  Returns true iff the element is contained in the set 
==  Returns true iff both sets contain the same elements 
isSubset  Returns true if the set contains ALL of the elements of another 
union  Returns a new set which contains ALL of the elements of the original set and another 
intersection  Returns a new set with only the elements that are in common to the original set and another 
Helpful Hint  
Set operations can be used with another set, an array, or any other sequence type. Likewise, sequence and collection operations can be used on sets. 
Sets in Igis[edit]
Igis uses the concept of "Containment" to describe the relationship of a rectangle relative to a target Point or Rect. The relationship is fully described as a set of enum as follows:
public enum Containment {
// Special cases combining horizontal and vertical containment
case containedFully // Included in set when both containedHorizontally AND containedVertically (Points,Rects)
case overlapsFully // Included in set when both overlapsHorizontally AND overlapsVertically (Rects only)
case beyondFully // Included in set when both beyondHorizontally AND beyondVertically (Points,Rects)
// Horizontal cases
case beyondLeft // Indicates target's right side is beyond (to left of) object (Points,Rects)
case overlapsLeft // Indicates target's left is beyond left but target's right is (Rects only)
// within or beyond object (toward right)
case beyondHorizontally // Included in set when either beyondLeft OR beyondRight (Points,Rects)
case containedHorizontally // Target is contained within left and right of object (Points,Rects)
case overlapsHorizontally // Included in set when both overlapsLeft AND overlapsRight (Rects only)
case overlapsRight // Indicates target's right is beyond right but target's left is (Rects only)
// within or beyond object (toward left)
case beyondRight // Indicates target's left side is beyond (to right of) object (Points,Rects)
// Vertical cases
case beyondTop // Indicates target's bottom side is beyond (on top of) object (Points,Rects)
case overlapsTop // Indicates target's top is beyond top but target's bottom is (Rects only)
// within or beyond object (toward bottom)
case beyondVertically // Included in set when either beyondTop OR beyondBottom (Points,Rects)
case containedVertically // Target is contained within top and bottom of object (Points,Rects)
case overlapsVertically // Included in set when both overlapsTop AND overlapsBottom (Points,Rects)
case overlapsBottom // Indicates target's bottom is beyond bottom but target's top is (Rects only)
// within or beyond object (toward top)
case beyondBottom // Indicates target's top side is beyond (below) object (Points,Rects)
}
Experiment[edit]
Containment Example[edit]
Begin by cloning https://github.com/TheCoderMerlin/ScenesContainmentExample into your Experiences directory. Run the project and play with the containment until you develop a deep understanding of how it works. Then, return to your previous project and complete the required exercises.
Getting Started[edit]
Continue from the previous project; we'll be editing all of our files there. Enter into the Sources directory of the project.
Horizontal Bounce Off "Wall"[edit]
Carefully review the below code located in calculate() of Ball.swift. Be certain the code is fully understood before proceeding.
let canvasBoundingRect = Rect(size:canvasSize)
let ballBoundingRect = Rect(topLeft:Point(x:ellipse.center.xellipse.radiusX, y:ellipse.center.yellipse.radiusY),
size:Size(width:ellipse.radiusX*2, height:ellipse.radiusY*2))
let containment = canvasBoundingRect.containment(target: ballBoundingRect)
// Bounce horizontally
if !containment.intersection([.overlapsRight, .beyondRight]).isEmpty && velocityX > 0 
!containment.intersection([.overlapsLeft, .beyondLeft]).isEmpty && velocityX < 0 {
velocityX *= 1
}
Please see the Exercises section below for the requirements of this challenge.
Key Concepts[edit]
Key Concepts  

Exercises[edit]
Exercises  

References[edit]
 Set Theory (Wikipedia)
 Set Theory for Computer Science (Glynn Winskel, University of Cambridge)
 Sets In Swift (Apple Documentation)