# W1525 Containment

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder Nuclear containment

## Background

### Set Basics

By the 1930s, 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
$\left\{\right\}$ The empty set
$\emptyset$ Another way of symbolizing the empty set
$\left\{\alpha ,\beta ,\gamma \right\}$ The set containing the elements $\alpha ,\beta ,\gamma$ $\alpha \in P$ $\alpha$ is a member of the set $P$ $\alpha \notin Q$ $\alpha$ is NOT a member of the set $Q$ $\mathbb {N}$ The set of natural numbers $\left\{1,2,3,\cdots \right\}$ $\mathbb {N} _{0}$ or $\mathbb {W}$ The set of natural numbers with zero (whole numbers) $\left\{0,1,2,3,\cdots \right\}$ $\mathbb {Z}$ The set of integers (negative, zero, and positive)
$\mathbb {R}$ The set of real numbers
$X\subseteq Y$ The set X is a subset of the set Y
$X{\cancel {\subseteq }}Y$ The set X is NOT a subset of the set Y

A set $X$ is termed a subset of set $Y$ iff every element of $X$ is an element of $Y$ . Formally: $X\subseteq Y\iff \forall z\in X,z\in Y$ Two sets $X$ and $Y$ are equal iff every element of $X$ is an element of $Y$ and vice versa. Formally: $X=Y\iff X\subseteq Y\land Y\subseteq X$ ### Boolean Algebra of Sets

Assume the existence of the universal set $U$ such that $A,B\subseteq U$ . The following operations are defined:

Symbols Name Definition
$A\cup B$ Union $\left\{x\mid x\in A\lor x\in B\right\}$ $A\cap B$ Intersection $\left\{x\mid x\in A\land x\in B\right\}$ ${\bar {A}}$ Complement $\left\{x\in U\mid x\notin A\right\}$ #### Set Union The union of two sets $A$ and $B$ , both subsets of the set $U$ , i.e. $A\cup B$ #### Set Intersection The intersection of sets $A$ and $B$ , both subsets of the set $U$ , i.e. $A\cap B$ #### Set Complement Consider the set $A$ , a subset of the set $U$  Then ${\bar {A}}$ is the complement of $A$ ### Sets in Swift

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.

This article can be improved by:  In the the table below, first two rows in the Purpose column, should the "iff" be "if" instead?

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 the elements of another
union Returns a new set which contains ALL 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

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)

case contact                  // Indicates target's rect overlaps with object's rect                     (Rects only)
}


## Experiment

### Containment Example

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

Continue from the previous project; we'll be editing all of our files there. Enter into the Sources directory of the project.

john-williams@codermerlin: cd ~/Experiences/W1521/Sources/ScenesShell/

### Horizontal Bounce Off "Wall"

Carefully review the below code in calculate() of Ball.swift. Before proceeding, be certain you fully understand the code.

    let canvasBoundingRect = Rect(size:canvasSize)
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
}


For the requirements of this challenge, see the Exercises section below.

## Key Concepts

This article can be improved by:  Some of the symbols in the below list aren't appearing; it looks incomplete.

Key Concepts • Sets are an unordered collection of elements
• Important Symbols
• $\color {white}\emptyset$ represents the empty set
• $\color {white}\left\{\alpha ,\beta ,\gamma \right\}$ represents a set containing the elements $\color {white}\alpha ,\beta ,\gamma$ • $\color {white}\alpha \in P$ represents that $\color {white}\alpha$ is a member of the set $\color {white}P$ • $\color {white}\alpha \notin P$ represents that $\color {white}\alpha$ is NOT a member of the set $\color {white}P$ • $\color {white}X\subseteq Y$ represents that X is a subset of the set Y
• $\color {white}X{\cancel {\subseteq }}Y$ represents that X is NOT a subset of the set Y
• Important Formulae
• $\color {white}X\subseteq Y\iff \forall z\in X,z\in Y$ • $\color {white}X=Y\iff X\subseteq Y\land Y\subseteq X$ • Useful Swift methods for sets
• contains
• ==
• isSubset
• union
• intersection
• 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.

## Exercises

Exercises 1. Refactor all hit detection to rely only upon Containment
2. Animate at least five background objects, at least three of which must be different instances of the same class
•  M1525-28  Complete  Merlin Mission Manager  Mission M1525-28.