Difference between revisions of "W1525 Containment"

From Coder Merlin
m (→‎Key Concepts: pending editorial review: looks good)
 
(21 intermediate revisions by 7 users not shown)
Line 9: Line 9:
== Background ==
== Background ==
=== Set Basics ===
=== Set Basics ===
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.
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.  
A '''set''' is an ''unordered'' collection of objects.  The objects are referred to as '''elements''' or '''members''' of the set.  
Line 26: Line 26:
| The set containing the elements <math>\alpha, \beta, \gamma</math>
| The set containing the elements <math>\alpha, \beta, \gamma</math>
|-
|-
| style="text-align: center;"|<math>\alpha \in \aleph</math>
| style="text-align: center;"|<math>\alpha \in P</math>
| <math>\alpha</math> is a '''member''' of the set <math>\aleph</math>
| <math>\alpha</math> is a '''member''' of the set <math>P</math>
|-
|-
| style="text-align: center;"|<math>\alpha \notin \aleph</math>
| style="text-align: center;"|<math>\alpha \notin Q</math>
| <math>\alpha</math> is NOT a '''member''' of the set <math>\aleph</math>
| <math>\alpha</math> is NOT a '''member''' of the set <math>Q</math>
|-
|-
| style="text-align: center;"|
| style="text-align: center;"|<math>\mathbb{N}</math>
| The set of natural numbers <math>\left \{1, 2, 3, \cdots \right \}</math>
| The set of natural numbers <math>\left \{1, 2, 3, \cdots \right \}</math>
|-
|-
| style="text-align: center;"|<sub>0</sub>
| style="text-align: center;"|<math>\mathbb{N}_0</math> or <math>\mathbb{W}</math>
| The set of natural numbers with zero <math>\left \{0, 1, 2, 3, \cdots \right \}</math>
| The set of natural numbers with zero (whole numbers) <math>\left \{0, 1, 2, 3, \cdots \right \}</math>
|-
|-
| style="text-align: center;"|
| style="text-align: center;"|<math>\mathbb{Z}</math>
| The set of integers (negative, zero, and positive)
| The set of integers (negative, zero, and positive)
|-
|-
| style="text-align: center;"|
| style="text-align: center;"|<math>\mathbb{R}</math>
| The set of real numbers
| The set of real numbers
|-
|-
| style="text-align: center;"|<math>X \subset Y</math>
| style="text-align: center;"|<math>X \subseteq Y</math>
| The set X is a subset of the set Y
| The set X is a subset of the set Y
|-
|-
| style="text-align: center;"|<math>X \cancel{\subset} Y</math>
| style="text-align: center;"|<math>X \cancel{\subseteq} Y</math>
| The set X is NOT a subset of the set Y
| The set X is NOT a subset of the set Y
|}
|}


A set <math>X</math> is termed a '''subset''' of set <math>Y</math> iff every element of <math>X</math> is an element of <math>Y</math>.  Formally: <math>X \subset Y \iff \forall z \in X, z \in Y</math>
A set <math>X</math> is termed a '''subset''' of set <math>Y</math> iff every element of <math>X</math> is an element of <math>Y</math>.  Formally: <math>X \subseteq Y \iff \forall z \in X, z \in Y</math>


Two sets <math>X</math> and <math>Y</math> are equal iff every element of <math>X</math> is an element of <math>Y</math> and ''vice versa''.  Formally: <math>X = Y \iff X \subset Y \land Y \subset X</math>
Two sets <math>X</math> and <math>Y</math> are equal iff every element of <math>X</math> is an element of <math>Y</math> and ''vice versa''.  Formally: <math>X = Y \iff X \subseteq Y \land Y \subseteq X</math>


=== Boolean Algebra of Sets ===
=== Boolean Algebra of Sets ===
Assume set <math>U</math>.  The following operations are defined:
Assume the existence of the universal set <math>U</math> such that <math>A, B \subseteq U</math>.  The following operations are defined:
{| class=wikitable
{| class=wikitable
! Symbols
! Symbols
Line 77: Line 77:
<br clear='all' />
<br clear='all' />
==== Set Union ====
==== Set Union ====
[[File:Venn0111.svg|left|400px|thumb|link=|The union of two sets <math>A</math> and <math>B</math>, both subsets of the set <math>U</math>, i.e. <math>A \cup B</math>]]
[[File:Venn0111.svg|left|400px|thumb|link=|The union of two sets <math>A</math> and <math>B</math>, both subsets of the set <math>U</math>, i.e., <math>A \cup B</math>]]
<br clear='all' />
<br clear='all' />
==== Set Intersection ====
==== Set Intersection ====
[[File:Venn0001.svg|left|400px|thumb|link=|The intersection of sets <math>A</math> and <math>B</math>, both subsets of the set <math>U</math>, i.e. <math>A \cap B</math>]]
[[File:Venn0001.svg|left|400px|thumb|link=|The intersection of sets <math>A</math> and <math>B</math>, both subsets of the set <math>U</math>, i.e., <math>A \cap B</math>]]
<br clear='all' />
<br clear='all' />
==== Set Complement ====
==== Set Complement ====
{|
{|
| [[File:Venn01.svg|thumb|link=|Consider the set <math>A</math>, a subset of the set <math>U</math>]]
| [[File:Venn01.svg|thumb|link=|Consider the set <math>A</math>, a subset of the set <math>U</math>]]
| [[File:Venn10.svg|thumb|link=|Then <math>\bar{A}</math> is the compliment of <math>A</math>]]
| [[File:Venn10.svg|thumb|link=|Then <math>\bar{A}</math> is the complement of <math>A</math>]]
|}
|}
<br clear='all' />
<br clear='all' />
=== Sets in Swift ===
=== 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.
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.


{{MerlinNoteFromEditor|In the the table below, first two rows in the Purpose column, should the "iff" be "if" instead?}}
The most useful methods are:
The most useful methods are:
{| class=wikitable
{| class=wikitable
Line 104: Line 106:
|-
|-
| {{SwiftIdentifier|isSubset}}
| {{SwiftIdentifier|isSubset}}
| Returns true if the set contains ALL of the elements of another
| Returns true if the set contains ALL the elements of another
|-
|-
| {{SwiftIdentifier|union}}
| {{SwiftIdentifier|union}}
| Returns a new set which contains ALL of the elements of the original set and another
| Returns a new set which contains ALL the elements of the original set and another
|-
|-
| {{SwiftIdentifier|intersection}}
| {{SwiftIdentifier|intersection}}
Line 114: Line 116:


{{Hint|
{{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.}}
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 ===
=== 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:
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:
<syntaxhighlight lang="swift" style="font-size: 80%;">     
<syntaxhighlight lang="swift" style="font-size: 80%;">     
public enum Containment {
public enum Containment {
Line 146: Line 148:
                                   //    within or beyond object (toward top)
                                   //    within or beyond object (toward top)
     case beyondBottom            // Indicates target's top side is beyond (below) object                    (Points,Rects)
     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)
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 151: Line 155:
== Experiment ==
== Experiment ==
=== Containment Example ===
=== 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.
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 ===
=== Getting Started ===
Continue from the previous project; we'll be editing all of our files there. Enter into the Sources directory of the project.  
Continue from the previous project; we'll be editing all of our files there. Enter into the Sources directory of the project.  
{{ConsoleLine|john-williams@codermerlin:|cd ~/Experiences/W1521/Sources/ScenesShell/}}
{{ConsoleLine|john-williams@codermerlin:|cd ~/Experiences/W1521/Sources/ScenesShell/}}


=== Horizontal Bounce Off "Wall" ===
=== Horizontal Bounce Off "Wall" ===
Carefully review the below code located in {{SwiftIdentifier|calculate()}} of {{Pathname|Ball.swift}}. Be certain the code is fully understood before proceeding.
Carefully review the below code in {{SwiftIdentifier|calculate()}} of {{Pathname|Ball.swift}}. Before proceeding, be certain you fully understand the code.


<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
Line 174: Line 178:




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


== Key Concepts ==
== Key Concepts ==
Line 180: Line 184:
* Sets are an '''unordered''' collection of elements
* Sets are an '''unordered''' collection of elements
* Important Symbols
* Important Symbols
** <math>\color{white}\empty</math> represents the empty set
** <math>\color{black}\empty</math> represents the empty set
** <math>\color{white}\left \{\alpha, \beta, \gamma \right \}</math> represents a set containing the elements  <math>\color{white}\alpha, \beta, \gamma</math>
** <math>\color{black}\left \{\alpha, \beta, \gamma \right \}</math> represents a set containing the elements  <math>\color{black}\alpha, \beta, \gamma</math>
** <math>\color{white}\alpha \in \aleph</math> represents that <math>\color{white}\alpha</math> is a '''member''' of the set <math>\color{white}\aleph</math>
** <math>\color{black}\alpha \in P</math> represents that <math>\color{black}\alpha</math> is a '''member''' of the set <math>\color{black}P</math>
** <math>\color{white}\alpha \notin \aleph</math> represents that <math>\color{white}\alpha</math> is NOT a '''member''' of the set <math>\color{white}\aleph</math>
** <math>\color{black}\alpha \notin P</math> represents that <math>\color{black}\alpha</math> is NOT a '''member''' of the set <math>\color{black}P</math>
** <math>\color{white}X \subset Y</math> represents that X is a subset of the set Y
** <math>\color{black}X \subseteq Y</math> represents that X is a subset of the set Y
** <math>\color{white}X \cancel{\subset} Y</math> represents that X is NOT a subset of the set Y
** <math>\color{black}X \cancel{\subseteq} Y</math> represents that X is NOT a subset of the set Y
* Important Formulae
* Important Formulae
** <math>\color{white}X \subset Y \iff \forall z \in X, z \in Y</math>
** <math>\color{black}X \subseteq Y \iff \forall z \in X, z \in Y</math>
** <math>\color{white}X = Y \iff X \subset Y \land Y \subset X</math>
** <math>\color{black}X = Y \iff X \subseteq Y \land Y \subseteq X</math>
* Useful Swift methods for sets
* Useful Swift methods for sets
** {{SwiftIdentifier|{{White|contains}}}}
** {{SwiftIdentifier|contains}}
** {{SwiftIdentifier|{{White|{{Equal}}{{Equal}}}}}}
** {{SwiftIdentifier|{{Equal}}{{Equal}}}}
** {{SwiftIdentifier|{{White|isSubset}}}}
** {{SwiftIdentifier|isSubset}}
** {{SwiftIdentifier|{{White|union}}}}
** {{SwiftIdentifier|union}}
** {{SwiftIdentifier|{{White|intersection}}}}
** {{SwiftIdentifier|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.
* 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 ==
{{W1525-Exercises}}
{{W1525-Exercises}}
* {{MMMAssignment|M1525-28}}


== References ==
== References ==
* [https://en.wikipedia.org/wiki/Set_theory Set Theory] (Wikipedia)
* [https://en.wikipedia.org/wiki/Set_theory Set Theory] (Wikipedia)
* [https://www.cl.cam.ac.uk/~gw104/STfCS2010.pdf Set Theory for Computer Science] (Glynn Winskel, University of Cambridge)
* [https://www.cl.cam.ac.uk/~gw104/STfCS2010.pdf Set Theory for Computer Science] (Glynn Winskel, University of Cambridge)
* [https://developer.apple.com/documentation/swift/set Sets In Swift (Apple Documentation)]
[[Category:IGIS]]

Latest revision as of 12:05, 7 February 2023

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]

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]