Created
September 20, 2016 20:52
-
-
Save slambert/e025b1237e797835eb50ce9a291fccbf to your computer and use it in GitHub Desktop.
Shows the difference between constrain and map functions in Processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Constrain vs map v1 | |
| // Steve Lambert 2016-09-20 | |
| // variables | |
| int ball1fill; // ball #1 fill color | |
| int ball2fill; // ball #2 fill color | |
| void setup(){ | |
| size(500,300); | |
| background(0); | |
| } | |
| void draw(){ | |
| background(255,0,0); | |
| stroke(125); | |
| line(255,0,255,height); | |
| noStroke(); | |
| // Ball #1 - Constrain | |
| // first, we're going to use constrain | |
| // to limit the fill color to 255. | |
| // Anything over 255 is 255 | |
| ball1fill = constrain(mouseX,0,255); | |
| fill(ball1fill); | |
| ellipse(mouseX,(height/2)-50, 50,50); | |
| // Ball #2 - Map | |
| // here we'll use map to convert the mouseX | |
| // position from 0 to width, to 0-255. | |
| ball2fill = int(map(mouseX,0,width,0,255)); | |
| // we need to add int(...) to change the number map | |
| // gives us from a float to a simle integer | |
| fill(ball2fill); | |
| ellipse(mouseX,(height/2)+50, 50,50); | |
| // Some println so you can see it as it happens... | |
| println("mouseX: " + mouseX); | |
| println("ball1 Fill: " + ball1fill); | |
| println("ball2 Fill: " + ball2fill); | |
| } //draw |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment