W1525 Containment

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

Prerequisites[edit]

Research[edit]

Background[edit]

Set Basics[edit]

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
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]

The union of two sets and , both subsets of the set , i.e.


Set Intersection[edit]

The intersection of sets and , both subsets of the set , i.e.


Set Complement[edit]

Consider the set , a subset of the set
Then is the complement of


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.

 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
Hint.pngHelpful 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)

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

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.

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

Horizontal Bounce Off "Wall"[edit]

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 ballBoundingRect = Rect(topLeft:Point(x:ellipse.center.x-ellipse.radiusX, y:ellipse.center.y-ellipse.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
    }


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

Key Concepts[edit]

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

Key ConceptsKeyConceptsIcon.png
  • Sets are an unordered collection of elements
  • Important Symbols
    • represents the empty set
    • represents a set containing the elements
    • represents that is a member of the set
    • represents that is NOT a member of the set
    • represents that X is a subset of the set Y
    • represents that X is NOT a subset of the set Y
  • Important Formulae
  • 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[edit]

ExercisesExercisesIcon.png
  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.

References[edit]