티스토리 뷰
// Line
void setup() {
size(640,480);
background(255); //white
}
void draw(){
stroke(0);
line(0,479, 639, 0);
}
//————————————–
// Bounce Ball
float x = 100;
float y = 100;
float xspeed = 1;
float yspeed = 3.3;
void setup(){
size(640, 480);
smooth();
background(255);
}
void draw(){
background(255);
x = x + xspeed;
y = y + yspeed;
if((x > width)||(x < 0)) { xspeed = xspeed * -1; } if((y > height)||(x < 0)) { yspeed = yspeed * -1; } stroke(0); //black fill(175); //grey ellipse(x,y, 16,16); } //-------------------------------------- // Vector Class class PVector{ float x; float y; PVector(float _x, float _y){ x = _x; y = _y; } void add(PVector v) { x += v.x; y += v.y; } void sub(PVector v) { x -= v.x; y -= v.y; } void mul(float n) { x *= n; y *= n; } void div(float n) { x /= n; y /= n; } } PVector location; PVector velocity; void setup(){ size(640, 480); smooth(); background(255); location = new PVector(100, 100); velocity = new PVector(1, 3.3); } void draw(){ background(255); location.add(velocity); if((location.x > width)||(location.x < 0)) { velocity.x *= -1; } if((location.y > height)||(location.y < 0)) { velocity.y *= -1; } stroke(0); //black fill(175); //grey ellipse(location.x, location.y, 16,16); } //-------------------------------------- // Vector Using the mouse // Vector Class class PVector{ float x; float y; PVector(float _x, float _y){ x = _x; y = _y; } void add(PVector v) { x += v.x; y += v.y; } void sub(PVector v) { x -= v.x; y -= v.y; } void mul(PVector v) { x *= v.x; y *= v.y; } void div(PVector v) { x /= v.x; y /= v.y; } } PVector mouse; PVector center; void setup(){ size(640, 480); smooth(); background(255); } void draw(){ background(255); PVector mouse = new PVector(mouseX, mouseY); PVector center = new PVector(width/2, height/2); mouse.sub(center); translate(width/2, height/2); // Move 0,0 coordation to left/right line(0, 0, mouse.x, mouse.y); } //------------------------------------- // Draw line void setup(){ size(640, 480); smooth(); background(255); } void draw(){ // stroke(0); //black fill(120, 0, 0); // point(mouseX, mouseY); ellipse(mouseX, mouseY, 16, 16); } //----------------------------------- // Vector 곱하기 예제 class PVector{ float x; float y; PVector(float _x, float _y){ x = _x; y = _y; } void add(PVector v) { x += v.x; y += v.y; } void sub(PVector v) { x -= v.x; y -= v.y; } void mul(float n) { x *= n; y *= n; } void div(float n) { x /= n; y /= n; } } PVector mouse; PVector center; void setup(){ size(640, 480); smooth(); background(255); } void draw(){ background(255); PVector mouse = new PVector(mouseX, mouseY); PVector center = new PVector(width/2, height/2); mouse.sub(center); mouse.mul(0.5); translate(width/2, height/2); // Move 0,0 coordation to left/right line(0, 0, mouse.x, mouse.y); } //------------------------------------- // Vector의 크기(magnitude) class PVector{ float x; float y; PVector(float _x, float _y){ x = _x; y = _y; } void add(PVector v) { x += v.x; y += v.y; } void sub(PVector v) { x -= v.x; y -= v.y; } void mul(float n) { x *= n; y *= n; } void div(float n) { x /= n; y /= n; } float mag() { return sqrt(x*x + y*y); } } PVector mouse; PVector center; void setup(){ size(640, 480); smooth(); background(255); } void draw(){ background(255); PVector mouse = new PVector(mouseX, mouseY); PVector center = new PVector(width/2, height/2); mouse.sub(center); float m = mouse.mag(); fill(0); rect(0,0, m, 10); translate(width/2, height/2); // Move 0,0 coordation to left/right line(0, 0, mouse.x, mouse.y); } //------------------------------------------ // Vector 정규화 class PVector{ float x; float y; PVector(float _x, float _y){ x = _x; y = _y; } void add(PVector v) { x += v.x; y += v.y; } void sub(PVector v) { x -= v.x; y -= v.y; } void mul(float n) { x *= n; y *= n; } void div(float n) { x /= n; y /= n; } float mag() { return sqrt(x*x + y*y); } void normalize() { float m = mag(); if (m != 0){ div(m); } } } PVector mouse; PVector center; void setup(){ size(640, 480); smooth(); background(255); } void draw(){ background(255); PVector mouse = new PVector(mouseX, mouseY); PVector center = new PVector(width/2, height/2); mouse.sub(center); mouse.normalize(); //normalization vector length = 1 mouse.mul(50); translate(width/2, height/2); // Move 0,0 coordation to left/right line(0, 0, mouse.x, mouse.y); } //-------------------------------- // PVector 클래스의 생성 class PVector{ float x; float y; PVector(float _x, float _y){ x = _x; y = _y; } void add(PVector v) { x += v.x; y += v.y; } void sub(PVector v) { x -= v.x; y -= v.y; } void mul(float n) { x *= n; y *= n; } void div(float n) { x /= n; y /= n; } float mag() { return sqrt(x*x + y*y); } void normalize() { float m = mag(); if (m != 0){ div(m); } } void limit(float max){ if (mag() > 10){
normalize();
mul(10);
}
}
}
//——————————–
// Mover 클래스의 생성(Vecter를 통한 이동과 가시화)
class Mover{
PVector location;
PVector velocity;
Mover(){
location = new PVector(random(width), random(height));
velocity = new PVector(random(-5, 5), random(-5, 5));
}
void checkEdges1(){
if(location.x > width) {
location.x = 0;
} else if (location.x < 0) { location.x = width; } if(location.y > height){
location.y = 0;
} else if (location.y < 0) { location.y = height; } } void checkEdges2(){ if((location.x > width)||(location.x < 0)){ velocity.x *=-1; } if((location.y > height)|| (location.y < 0)) { velocity.y *= -1; } } void update(){ location.add(velocity); } void display(){ stroke(0); //black fill(175); //grey ellipse(location.x, location.y, 16, 16); } } //-------------------------------------------- // PVector와 Mover Class의 활용 Mover mover; void setup(){ size(640, 480); smooth(); background(255); mover = new Mover(); } void draw(){ //background(255); mover.update(); mover.checkEdges2(); mover.display(); } //-------------------------------------------- // Mover 클래스에서 CheckEdges()에 대한 재정의 통해 Bounce 하게 만듬 void checkEdges2(){ if((location.x > width)||(location.x < 0)){ velocity.x *=-1; } if((location.y > height)|| (location.y < 0)) { velocity.y *= -1; } } //------------------------------------- // 가속도에 대하여 고려해 보자(Mover 클래스의 수정) class Mover{ PVector location; PVector velocity; PVector acceleration; // New Item(Acceleration) float topspeed; Mover(){ location = new PVector(width/2, height/2); velocity = new PVector(0,0); acceleration = new PVector(-0.001, 0.01); topspeed = 10; } void checkEdges1(){ if(location.x > width) {
location.x = 0;
} else if (location.x < 0) { location.x = width; } if(location.y > height){
location.y = 0;
} else if (location.y < 0) { location.y = height; } } void checkEdges2(){ if((location.x > width)||(location.x < 0)){ velocity.x *=-1; acceleration.x *= -1; } if((location.y > height)|| (location.y < 0)) { velocity.y *= -1; acceleration.y *= -1; } } void update(){ velocity.add(acceleration); // add acceleration to velocity velocity.limit(topspeed); location.add(velocity); } void display(){ stroke(0); //black fill(175); //grey ellipse(location.x, location.y, 16, 16); } } Mover mover; void setup(){ size(640, 480); smooth(); background(255); mover = new Mover(); } void draw(){ //background(255); mover.update(); mover.checkEdges2(); mover.display(); } //----------------------------------------------- // 가속도와 Bounce되는 공을 고려한 전체 소스 class PVector{ float x; float y; PVector(float _x, float _y){ x = _x; y = _y; } void add(PVector v) { x += v.x; y += v.y; } void sub(PVector v) { x -= v.x; y -= v.y; } void mul(float n) { x *= n; y *= n; } void div(float n) { x /= n; y /= n; } float mag() { return sqrt(x*x + y*y); } void normalize() { float m = mag(); if (m != 0){ div(m); } } void limit(float max){ if (mag() > max){
normalize();
mul(max);
}
}
}
class Mover{
PVector location;
PVector velocity;
PVector acceleration; // New Item(Acceleration)
float topspeed;
Mover(){
location = new PVector(width/2, height/2);
velocity = new PVector(0, 0);
acceleration = new PVector(-0.001, 0.1);
topspeed = 10;
}
void checkEdges1(){
if(location.x > width) {
location.x = 0;
} else if (location.x < 0) { location.x = width; } if(location.y > height){
location.y = 0;
} else if (location.y < 0) { location.y = height; } } void checkEdges2(){ if((location.x > width)||(location.x < 0)){ velocity.x *=-1; acceleration.x *= -1; } if((location.y > height)|| (location.y < 0)) { velocity.y *= -1; acceleration.y *= -1; } } void update(){ velocity.add(acceleration); // add acceleration to velocity velocity.limit(topspeed); location.add(velocity); } void display(){ stroke(0); //black fill(175); //grey ellipse(location.x, location.y, 16, 16); } } Mover mover; void setup(){ size(640, 480); smooth(); background(255); mover = new Mover(); } void draw(){ background(255); mover.update(); mover.checkEdges2(); mover.display(); } //----------------------------------------------- // 이제부터 Prcessing에서 제공하는 PVector 사용 // 가속도와 Bounce되는 공을 고려한 전체 소스 // PVector.random2D() 을 사용하여 mag가 1인 임의 가속도를 부가함 class Mover{ PVector location; PVector velocity; PVector acceleration; // New Item(Acceleration) float topspeed; Mover(){ location = new PVector(width/2, height/2); velocity = new PVector(0, 0); acceleration = new PVector(-0.001, 0.1); topspeed = 10; } void checkEdges1(){ if(location.x > width) {
location.x = 0;
} else if (location.x < 0) { location.x = width; } if(location.y > height){
location.y = 0;
} else if (location.y < 0) { location.y = height; } } void checkEdges2(){ if((location.x > width)||(location.x < 0)){ velocity.x *=-1; acceleration.x *= -1; } if((location.y > height)|| (location.y < 0)) { velocity.y *= -1; acceleration.y *= -1; } } void update(){ acceleration = PVector.random2D(); velocity.add(acceleration); // add acceleration to velocity velocity.limit(topspeed); location.add(velocity); } void display(){ stroke(0); //black fill(175); //grey ellipse(location.x, location.y, 16, 16); } } Mover mover; void setup(){ size(640, 480); smooth(); background(255); mover = new Mover(); } void draw(){ background(255); mover.update(); mover.checkEdges2(); mover.display(); } //----------------------------------------------- // Static 함수 사용(PVector) // 동작하지 않고 오류 발생함 class PVector{ float x; float y; PVector(float _x, float _y){ x = _x; y = _y; } void add(PVector v) { x += v.x; y += v.y; } void sub(PVector v) { x -= v.x; y -= v.y; } void mul(float n) { x *= n; y *= n; } void div(float n) { x /= n; y /= n; } float mag() { return sqrt(x*x + y*y); } void normalize() { float m = mag(); if (m != 0){ div(m); } } void limit(float max){ if (mag() > max){
normalize();
mul(max);
}
}
}
static PVector add(PVector v1, PVector v2){
PVector v3 = new PVector(v1.x + v2.x, v1.y + v2.y);
return v3;
}
PVector v1 = new PVector(1,1);
PVector v2 = new PVector(2,2);
PVector v3 = add(v1, v2);
println(v3);
///————————————
// 마우스를 향해 움직이는 객체 설계
class Mover{
PVector location;
PVector velocity;
PVector acceleration; // New Item(Acceleration)
float topspeed;
Mover(){
location = new PVector(width/2, height/2);
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
topspeed = 10;
}
void checkEdges1(){
if(location.x > width) {
location.x = 0;
} else if (location.x < 0) { location.x = width; } if(location.y > height){
location.y = 0;
} else if (location.y < 0) { location.y = height; } } void checkEdges2(){ if((location.x > width)||(location.x < 0)){ velocity.x *=-1; acceleration.x *= -1; } if((location.y > height)|| (location.y < 0)) { velocity.y *= -1; acceleration.y *= -1; } } void update(){ PVector mouse = new PVector(mouseX, mouseY); PVector dir = PVector.sub(mouse, location); dir.normalize(); dir.mult(0.5); acceleration = dir; velocity.add(acceleration); // add acceleration to velocity velocity.limit(topspeed); location.add(velocity); } void display(){ stroke(0); //black fill(175); //grey ellipse(location.x, location.y, 16, 16); } } Mover mover; void setup(){ size(640, 480); smooth(); background(255); mover = new Mover(); } void draw(){ background(255); mover.update(); mover.display(); } ///------------------------------------ // 마우스를 향해 움직이는 객체 설계 // 20개의 객체들이 함꺼번에 움직이게 하기 class Mover{ PVector location; PVector velocity; PVector acceleration; // New Item(Acceleration) float topspeed; Mover(){ location = new PVector(random(width), random(height)); velocity = new PVector(0, 0); topspeed = 5; } void checkEdges1(){ if(location.x > width) {
location.x = 0;
} else if (location.x < 0) { location.x = width; } if(location.y > height){
location.y = 0;
} else if (location.y < 0) { location.y = height; } } void checkEdges2(){ if((location.x > width)||(location.x < 0)){ velocity.x *=-1; acceleration.x *= -1; } if((location.y > height)|| (location.y < 0)) { velocity.y *= -1; acceleration.y *= -1; } } void update(){ PVector mouse = new PVector(mouseX, mouseY); PVector acceleration = PVector.sub(mouse, location); acceleration.normalize(); acceleration.mult(0.5); velocity.add(acceleration); // add acceleration to velocity velocity.limit(topspeed); location.add(velocity); } void display(){ stroke(0); //black fill(175); //grey ellipse(location.x, location.y, 48, 48); } } Mover[] mover = new Mover[20]; void setup(){ size(640, 480); smooth(); background(255); for(int i=0; i
'processing' 카테고리의 다른 글
NATURE OF CODE-0.INTRODUCTION (0) | 2016.05.07 |
---|---|
PROCESSING 언어 (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 |