Try It

Drag the circle through the blue barrier. Watch how the color changes to show movement progress and final position.

Concept

THROUGH means moving from one side of something to the other, passing inside or across its boundaries. This involves motion and the crossing of a barrier or boundary.

Translation Strategy

To create "through" relationships on screen:

Key P5.js Methods

Coordinate System:
createCanvas(width, height) - Creates coordinate system where (0,0) is top-left
rect(x, y, width, height) - Draw rectangular barrier

Boundary Detection:
• Compare object position with barrier edges
dist(x1, y1, x2, y2) - Calculate distance from barriers
• Track which side of barrier the object is on

State Management:
• Use boolean flags to track entry and exit states
• Check if movement crosses from one side to another

Visual Feedback:
fill(r, g, b) - Change colors based on position and state
stroke(r, g, b) - Change border colors
ellipse(x, y, width, height) - Draw moving circle

Mouse Interaction:
mousePressed() - Detect when mouse is clicked
mouseDragged() - Detect when mouse is dragged
mouseReleased() - Detect when mouse is released

Code Structure

Basic Pattern for "Through":

1. Define barrier: Create rectangular boundary
let barrier = {x: 180, y: 50, width: 40, height: 200};

2. Track state: Monitor entry and exit
let movingCircle = {hasEnteredBarrier: false, hasExitedBarrier: false, hasPassed: false};

3. Check crossing: Detect complete traversal
if (isInsideBarrier && !hasEnteredBarrier) {
  hasEnteredBarrier = true;
  entryFromLeft = (x < barrier.x + barrier.width/2);
}

4. Position-based colors: Visual feedback based on location
if (isInsideBarrier) {
  fill(255, 255, 100); // Yellow - passing through
} else if (x < barrier.center) {
  fill(255, 150, 100); // Orange - left side
} else {
  fill(100, 255, 100); // Green - right side
}