Processing 2. Креативное программирование
.pdf...
, .integer float. numbersfloat, , .abs(), ceil(), floor(), round(), sq(), sqrt(), min(), max() dist() println().
int x = 177; int y = -189; float a = 32.75;
float b = -70.38;
float[] numbers = {a, b, x, y};
println("The absolute value of " + a + " is " + abs(a) ); println("The absolute value of " + b + " is " + abs(b) ); println("The absolute value of " + y + " is " + abs(y) );
println("The closest int value greater than (or equal to) " + x + " is " + ceil(x) );
println("The closest int value greater than (or equal to) " + a + " is " + ceil(a) );
println("The closest int value greater than (or equal to) " + b + " is " + ceil(b) );
println("The closest int value less than (or equal to) " + y + " is " + floor(y) );
println("The closest int value less than (or equal to) " + a + " is " + floor(a) );
println("The closest int value less than (or equal to) " + b + " is " + floor(b) );
println("The closest int value to " + a + " is " + round(a) ); println("The closest int value to " + b + " is " + round(b) ); println("The square number of " + x + " is " + sq(x) ); println("The square number of " + b + " is " + sq(b) ); println("The square root of " + x + " is " + sqrt(x) ); println("The square root of " + a + " is " + sqrt(a) ); println("The square root of " + b + " is " + sqrt(b) );
println("The smallest number in the list {" + a + "," + b + "," + x + "," + y + "} is " + min( numbers ) );
println("The largest number in the list {" + a + "," + b + "," + x + "," + y + "} is " + max( numbers ) );
println("The distance between (" + x + ", " + y + ") and (" + a + ", " + b + ") is " + dist(x, y, a, b ) );
20
1
, , Processing 100 x 100 . , Processing,size(). :
. - , :
fabs() .; abs(-189) 189.
fceil() , - . , ceil(177) 177, ceil(-70.38) -70.
ffloor() , . floor(32.75) 32, floor(-70.38) -71.
fround() . round(32.75) 33, round(-70.38) -70.
fmin() .
fmax() .
fsq() . . - .
fsqrt() .. sqrt(-70.38) NaN (Not a Number).
fdist() . - x y , - x y .dist() .
21
...
println() - .. , println(a) -.println() ,. , .
println( x + y );
-12. +println(), .
println( x + " " + y );
177 -189. +println() String.
, . - (GUI). , Apple Macintosh 1980-., ,.
.
draw() ,. draw. setup().
void setup()
{
size( 640, 480 ); smooth(); background( 255 );
}
22
1
void draw()
{
// empty, but we need it to create an app that runs in the continuous mode.
}
void mousePressed()
{
if ( mouseButton == RIGHT ) { background( 255 );
}
}
void mouseMoved()
{
stroke( 0, 64 ); strokeWeight( 1 ); fill( 255, 32 );
float d = dist( mouseX, mouseY, pmouseX, pmouseY ); constrain( d, 8, 100 );
ellipse( mouseX, mouseY, d, d );
}
void mouseDragged()
{
stroke( 0 );
float d = dist( mouseX, mouseY, pmouseX, pmouseY ); constrain( d, 0, 100 );
float w = map( d, 0, 100, 1, 10 ); strokeWeight( w );
line( mouseX, mouseY, pmouseX, pmouseY );
}
void mouseReleased()
{
noStroke(); fill( 255, 16 );
rect( 0, 0, width, height );
}
void mouseClicked()
{
fill( 255, 0, 0, 128 ); float d = random( 20, 200 );
ellipse( mouseX, mouseY, d, d );
}
23
...
, run Cmd + R Mac Ctrl + R Windows Linux. .. ,. , ,. . - :
,:
fmouseClicked() , .. .
24
1
fmouseDragged() ,, ..
fmouseMoved() , ,..
fmousePressed() , .mouseButton.
fmouseReleased() , ..
fmouseX x . .
fmouseY y . .
fpmouseX x . .
fpmouseY y . .
fmousePressed - , - , . ,, .
fmouseButton - ,, .
LEFT, RIGHT CENTER.
- . ,, . Processing ,.. Processing. , D, S -.
25
...
setup() draw().Processing,.
int x; int y; int r; color c;
boolean drawStroke; void setup()
{
size( 480, 320 ); smooth(); strokeWeight( 2 );
x = width/2; y = height/2; r = 80;
c = color( 255, 0, 0 ); drawStroke = true;
}
void draw()
{
background( 255 );
if ( drawStroke == true ) { stroke( 0 );
}else { noStroke();
}
fill( c );
ellipse( x, y, r*2, r*2 );
}
.: keyPressed(), keyReleased() keyTyped().
void keyPressed()
{
if ( key == CODED ) {
26
1
if ( keyCode == RIGHT ) { x += 10;
}else if ( keyCode == LEFT ) {
x-= 10;
}else if ( keyCode == UP ) {
y-= 10;
}else if ( keyCode == DOWN ) {
y+= 10;
}
}
x = constrain( x, r, width-r ); y = constrain( y, r, height-r );
}
void keyReleased()
{
switch ( key ) { case 'r':
c = color( 255, 0, 0 ); break;
case 'g':
c = color( 0, 255, 0 ); break;
case 'b':
c = color( 0, 0, 255 ); break;
case 'c':
c = color( 0, 255, 255 ); break;
case 'm':
c = color( 255, 0, 255 ); break;
case 'y':
c = color( 255, 255, 0 ); break;
default:
break;
}
}
void keyTyped()
{
if ( key == 's' ) { drawStroke = !drawStroke;
}
}
27
...
, :
. S .R, G, B, C, M Y .
Processing , : keyPressed(), keyReleased() keyTyped(). - . keyPressed() , ., .keyReleased() , . ,, .keyTyped() , keyPressed(),, Enter, Ctrl Alt.
fkey .
fkeyCodeShift, Ctrl . ,if, , , ,keyPressed() . keyCode UP,
DOWN, LEFT, RIGHT, ALT, CONTROL, SHIFT, BACKSPACE, TAB, ENTER, RETURN, ESC DELETE.
fkeyPressed ., - ,.draw().
fkeyPressed() .
28
1
fkeyReleased() .
fkeyTyped() , . Alt, Ctrl Shift .
.- ( ,Ctrl + S ),. Processing Wiki ,: http:// wiki.processing.org/w/ Multiple_key_presses.
29