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

A set ${\displaystyle X}$ is termed a subset of set ${\displaystyle Y}$ iff every element of ${\displaystyle X}$ is an element of ${\displaystyle Y}$. Formally: ${\displaystyle X\subseteq Y\iff \forall z\in X,z\in Y}$

Two sets ${\displaystyle X}$ and ${\displaystyle Y}$ are equal iff every element of ${\displaystyle X}$ is an element of ${\displaystyle Y}$ and vice versa. Formally: ${\displaystyle X=Y\iff X\subseteq Y\land Y\subseteq X}$

### Boolean Algebra of Sets

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

Symbols Name Definition
${\displaystyle A\cup B}$ Union ${\displaystyle \left\{x\mid x\in A\lor x\in B\right\}}$
${\displaystyle A\cap B}$ Intersection ${\displaystyle \left\{x\mid x\in A\land x\in B\right\}}$
${\displaystyle {\bar {A}}}$ Complement ${\displaystyle \left\{x\in U\mid x\notin A\right\}}$

#### Set Union

The union of two sets ${\displaystyle A}$ and ${\displaystyle B}$, both subsets of the set ${\displaystyle U}$, i.e. ${\displaystyle A\cup B}$

#### Set Intersection

The intersection of sets ${\displaystyle A}$ and ${\displaystyle B}$, both subsets of the set ${\displaystyle U}$, i.e. ${\displaystyle A\cap B}$

#### Set Complement

 Consider the set ${\displaystyle A}$, a subset of the set ${\displaystyle U}$ Then ${\displaystyle {\bar {A}}}$ is the complement of ${\displaystyle 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
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
• ${\displaystyle \color {black}\emptyset }$ represents the empty set
• ${\displaystyle \color {black}\left\{\alpha ,\beta ,\gamma \right\}}$ represents a set containing the elements ${\displaystyle \color {black}\alpha ,\beta ,\gamma }$
• ${\displaystyle \color {black}\alpha \in P}$ represents that ${\displaystyle \color {black}\alpha }$ is a member of the set ${\displaystyle \color {black}P}$
• ${\displaystyle \color {black}\alpha \notin P}$ represents that ${\displaystyle \color {black}\alpha }$ is NOT a member of the set ${\displaystyle \color {black}P}$
• ${\displaystyle \color {black}X\subseteq Y}$ represents that X is a subset of the set Y
• ${\displaystyle \color {black}X{\cancel {\subseteq }}Y}$ represents that X is NOT a subset of the set Y
• Important Formulae
• ${\displaystyle \color {black}X\subseteq Y\iff \forall z\in X,z\in Y}$
• ${\displaystyle \color {black}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.