Spatial relationship
Between
Drag all three circles. C is between A and B when its position falls in the range they define.
Try it — drag, click, or tap inside the figure.
Open in p5 Web Editor Fork and experiment with this sketchConcept
BETWEEN means in the space separating two other things. Test whether a point's coordinate lies between two bounds on an axis.
Translation strategy
Ways to express between in a sketch:
- Horizontal: Compare X against left and right bounds
- Vertical: Compare Y against top and bottom bounds
- Both axes: Require the point to be between on X and Y when needed
Key p5.js methods
Key methods
Canvas coordinates
• createCanvas() — origin (0, 0) is top-left
• Y increases downward; X increases to the right
Range test
•
• && — combine boundary checks
Drawing
• ellipse(), line() — objects and span guides
Pointer input
• mousePressed(), mouseDragged(), mouseReleased() — drag objects
• dist() — test whether the pointer is inside a circle
Canvas coordinates
• createCanvas() — origin (0, 0) is top-left
• Y increases downward; X increases to the right
Range test
•
minX < x && x < maxX — horizontal between• && — combine boundary checks
Drawing
• ellipse(), line() — objects and span guides
Pointer input
• mousePressed(), mouseDragged(), mouseReleased() — drag objects
• dist() — test whether the pointer is inside a circle
Code pattern
Code pattern
1. Store three positions
2. Find the span
3. Test between
4. Hit-test for dragging
1. Store three positions
let a = { x: 100, y: 150 };let b = { x: 300, y: 150 };let c = { x: 200, y: 150 };2. Find the span
let left = min(a.x, b.x);let right = max(a.x, b.x);3. Test between
let isBetween = c.x > left && c.x < right;4. Hit-test for dragging
if (dist(mouseX, mouseY, c.x, c.y) < c.r) c.dragging = true;