티스토리 뷰
nature of code : shiffman
코드 사이트 : https://github.com/shiffman/The-Nature-of-Code-Examples
프로세싱 언어 입문서 : https://processing.org/learning/books
프로세싱 언어 다운로드 : https://processing.org/download
프로세싱 언어 Get Start : https://processing.org/tutorials/gettingstarted
Getting start with processing sample : https://processing.org/books/media.zip
!. Hello, World!
println(“Hello,World”);
!. Hello, World!
void setup() {
size(320, 100);
fill(0, 102, 153);
textSize(32);
text(“Hello,World!”, 10, 50);
}
!. 원을 그리기
// 0.0 좌표에서 50,50 좌표로 이동하여 높이와 넓이가 80인 원을 그리기
ellipse(50, 50, 80,80);
!. 마우스를 따라 원을 그리기
// 작업판의 크기 설정(넓이 480, 높이 120)
void setup() {
size(480, 120);
}
// 마우스를 움직이며 하얀색 원이 생성되고 마우스를 클릭하면 검정색 원이 생성됨
void draw() {
if (mousePressed) {
fill(0); // block
} else {
fill(255); // white
}
ellipse(mouseX, mouseY, 80, 80);
}
!. 2개의 동전을 던져서 4가지 경우에 수에 따라 여행자(Walker) 좌표를 이동하게 함
앞면(1) 앞면(1) 앞으로(3) : y++
앞면(1) 뒷면(0) 오른쪽으로(2) : x++
뒷면(0) 앞면(1) 왼쪽으로(1) : x–
뒷면(0) 뒷면(0) 뒤로(0) : y–
1. Walker 클래스 정의
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// A random walker object!
class Walker {
int x,y;
Walker() {
x = width/2; //width 640
y = height/2; //height 360
}
void display() {
stroke(0);
point(x,y);
}
// Randomly move up, down, left, right, or stay in one place
void step() {
int choice = int(random(4));
if (choice == 0) {
y–;
} else if (choice == 1) {
x–;
} else if (choice == 2) {
x++;
} else {
y++;
}
// Constrains a value to not exceed a maximum and minimum value
// constrain(amt, low, high)
x = constrain(x,0,width-1); // width minimum 0, maximum 639
y = constrain(y,0,height-1);//height minimum 0, maximum 359
}
}
2. Walker 객체 활용
Walker w;
void setup() {
size(640,360);
// Create a walker object
w = new Walker();
background(255);
}
void draw() {
// Run the walker object
w.step();
w.display();
}
!. 2개의 동전을 던져서 4가지 경우에 수에 따라 여행자(Walker) 좌표를 이동하게 함
위에는 4개의 방향(전후좌우)에서 확장한 전좌, 전우, 후좌, 후우의 추가함
x축의 값을 -1,0,1
y축의 값을 -1,0,1로 이동시키게 한다.
1. Walker 클래스 정의
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// A random walker object!
class Walker {
int x,y;
Walker() {
x = width/2; //width 640
y = height/2; //height 360
}
void display() {
stroke(0);
point(x,y);
}
// Randomly move up, down, left, right, or stay in one place
void step() {
int stepx = int(random(3))-1;
int stepy = int(random(3))-1;
x += stepx;
y += stepy;
// Constrains a value to not exceed a maximum and minimum value
// constrain(amt, low, high)
x = constrain(x,0,width-1); // width minimum 0, maximum 639
y = constrain(y,0,height-1);//height minimum 0, maximum 359
}
}
2. Walker 객체 활용
Walker w;
void setup() {
size(640,360);
// Create a walker object
w = new Walker();
background(255);
}
void draw() {
// Run the walker object
w.step();
w.display();
}
uniform 분포를 가질 수 있도록 random 함수 발생하여 사각형 그림
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// An array to keep track of how often random numbers are picked
float[] randomCounts;
void setup() {
size(640,360);
randomCounts = new float[20];
}
void draw() {
background(255);
// Pick a random number and increase the count
int index = int(random(randomCounts.length)); //0~19
randomCounts[index]++;
// Draw a rectangle to graph results
stroke(0); // black
strokeWeight(2);
fill(127); // grey
int w = width/randomCounts.length; //width 640/20= 32
for (int x = 0; x < randomCounts.length; x++) { rect(x*w,height-randomCounts[x],w-1,randomCounts[x]); // rect(x,y,width, height) } }
'processing' 카테고리의 다른 글
NATURE OF CODE-1.VECTOR (0) | 2016.05.07 |
---|---|
NATURE OF CODE-0.INTRODUCTION (0) | 2016.05.07 |
NATURE OF CODE-5 : Boxes 예제(원을 만들어 경사면을 구르게 하기) (0) | 2016.05.06 |
NATURE OF CODE-5 : Boxes 예제(경계면을 고정으로 만들기) (0) | 2016.05.06 |
NATURE OF CODE-5 : Boxes 예제(Box2D 라이브러리 사용) (0) | 2016.05.06 |