22. //---------------------------------------- global
// declare variables
int x;
int y;
int diameter;
//---------------------------------------- setup
void setup()
{
// set window size
size( 640, 480 );
// assign values to variables
x = 320;
y = 240;
diameter = 200;
}
//---------------------------------------- draw
void draw()
{
// fill background
background( 255 );
// disable stroke
noStroke();
// set fill color
fill( 255, 0, 0 );
// draw ellipse with variables
ellipse( x, y, diameter, diameter );
}
ProcessingWorkshop
Variables - Declaration
23. //---------------------------------------- global
// declare variables
int x;
int y;
int diameter;
//---------------------------------------- setup
void setup()
{
// set window size
size( 640, 480 );
// assign values to variables
x = 320;
y = 240;
diameter = 200;
}
//---------------------------------------- draw
void draw()
{
// fill background
background( 255 );
// disable stroke
noStroke();
// set fill color
fill( 255, 0, 0 );
// draw ellipse with variables
ellipse( x, y, diameter, diameter );
// update x
x = x + 1;
}
ProcessingWorkshop
Variables - Animation
24. ProcessingWorkshop
Conditional
i f 文
特定の条件下だけ通常とは違う処理をさせ
たいときに使う。
if( 条件 )
{
// ここに処理を描く
}
関係演算子
// a が b と等しいとき
if( a == b )
{
// ここに処理
}
// a が b と等しくないとき
if( a != b )
{
// ここに処理
}
// a が b より小さいとき
if( a < b )
{
// ここに処理
}
// a が b 以下のとき
if( a <= b )
{
// ここに処理
}
// a が b より大きいとき
if( a > b )
{
// ここに処理
}
// a が b 以上のとき
if( a >= b )
{
// ここに処理
}
25. //---------------------------------------- global
// declare variables
int x;
int y;
int diameter;
//---------------------------------------- setup
void setup()
{
// set window size
size( 640, 480 );
// assign values to variables
x = 0;
y = 240;
diameter = 200;
}
//---------------------------------------- draw
void draw()
{
// fill background
background( 255 );
// disable stroke
noStroke();
// set fill color
fill( 255, 0, 0 );
// draw ellipse with variables
ellipse( x, y, diameter, diameter );
// update x
x = x + 1;
// reset x when x is over window width
if( x > width )
{
x = 0;
}
}
ProcessingWorkshop
Conditional - Animation
26. //---------------------------------------- global
// declare variables
int x;
int y;
int diameter;
//---------------------------------------- setup
void setup()
{
// set window size
size( 640, 480 );
// assign values to variables
diameter = 200;
x = -( diameter / 2 );
y = 240;
}
//---------------------------------------- draw
void draw()
{
// fill background
background( 255 );
// disable stroke
noStroke();
// set fill color
fill( 255, 0, 0 );
// draw ellipse with variables
ellipse( x, y, diameter, diameter );
// update x
x = x + 1;
// reset x when x is over window width
if( ( x - ( diameter / 2 ) ) > width )
{
x = -( diameter / 2 );
}
}
ProcessingWorkshop
Conditional - NaturalAnimation
28. ProcessingWorkshop
SerialCommunication //---------------------------------------- import
// import serial library
import processing.serial.*;
//---------------------------------------- global
// serial object
Serial serial;
int val = 0;
//---------------------------------------- setup
void setup()
{
// set window size
size( 480, 480 );
// listup serial devices to console
printArray( Serial.list() );
// setup serial
serial = new Serial( this, Serial.list()[ 5 ], 9600 );
// clear serial
serial.clear();
// buffer serial until 'n'
serial.bufferUntil( 'n' );
}
//---------------------------------------- draw
void draw()
{
// fill background
background( 0 );
}
//------------------------------------------------------------ serialEvent
void serialEvent( Serial _serial )
{
// read string until 'n' from serial buffer
String str = _serial.readStringUntil( 'n' );
// if str is not null
if( str != null )
{
// trim str
str = trim( str );
// convert string to int
val = int( str );
// print value to console
println( val );
}
}
29. ProcessingWorkshop
SerialCommunication
//---------------------------------------- import
// import serial library
import processing.serial.*;
//---------------------------------------- global
// serial object
Serial serial;
int val = 0;
float angle = 0.0;
float centerX, centerY;
//---------------------------------------- setup
void setup()
{
// set window size
size( 480, 480 );
// fill background
background( 0 );
centerX = width / 2;
centerY = height / 2;
// listup serial devices to console
printArray( Serial.list() );
// setup serial
serial = new Serial( this, Serial.list()[ 5 ], 9600 );
// clear serial
serial.clear();
// buffer serial until 'n'
serial.bufferUntil( 'n' );
}
//---------------------------------------- draw
void draw()
{
// disable stroke
noStroke();
// fill background slowly
fill( 0, 2 );
rect( 0, 0, width, height );
// set stroke color
stroke( 0, 255, 0 );
// update angle
++angle;
float radius = val / 4;
float x = centerX + ( cos( radians( angle ) ) * radius );
float y = centerY + ( sin( radians( angle ) ) * radius );
// draw line
line( centerX, centerY, x, y );
}
// serialEvent は変更しないので割愛