Computer/Coding

[Processing] particle/interaction project

순박한시골청년 2017. 3. 30. 15:39

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import processing.video.*;
 
int stat;
int btn = 0;
int btn2 = 0;
int btn3 = 0;
float nbsum = 0;
float pbsum = 0;
float nbsum2 = 0;
float pbsum2 = 0;
float nbsum3 = 0;
float pbsum3 = 0;
 
ParticleSystem ps;
Capture video;
PImage img, dback;
double sum4;
 
void setup() {
  size(600480);
  video = new Capture(this, 64048030);
  video.start();
 
  img = createImage(640480, RGB);
  dback = createImage(640480, GRAY);
  background(0);
  
  ps = new ParticleSystem(new PVector(width/2-1500));
}
 
 
void draw() {
  if(video.available()){
    video.read();
  }
 
  video.loadPixels();
  img.loadPixels();
  for(int x = 50; x < 630; x++){
    for(int y = 0; y < 480; y++){
      img.pixels[y*img.width +(640-x)] = video.pixels[y*img.width + x];
    }
  }
  img.filter(GRAY);
  checkInterection2();
  dithering();
  checkInterection();
  if(btn == 1){
    ps.addParticle();
    ps.run();
  }
  
  image(img, 0480);
  //rect(300, 0, 20, 20);
}
 
void checkInterection(){
  img.loadPixels();
  nbsum = 0;
  for(int dx = 0; dx < 20; dx++){
    for(int dy = 0; dy < 20; dy++){
      nbsum = nbsum + brightness(img.pixels[(dy)*img.width + (500+dx)]);
    }
  }
 
  if(abs(nbsum - pbsum) > 6000){
    if(btn == 0){btn = 1;}
    else if(btn == 1){btn = 0;}
  }
  //text("brightness difference : " + abs(nbsum - pbsum), 10, 480);
  
  pbsum = nbsum;
}
 
void checkInterection2(){
  img.loadPixels();
  nbsum2 = 0;
  for(int dx = 0; dx < 20; dx++){
    for(int dy = 0; dy < 20; dy++){
      nbsum2 = nbsum2 + brightness(img.pixels[(dy)*img.width + (100+dx)]);
    }
  }
  
  if(abs(nbsum2 - pbsum2) > 10000){
    if(btn2 == 0){if(btn3==1){btn2 = 2;}else{btn2 = 1;}}
    else if(btn2 == 1){btn2 = 2;}
    else if(btn2 == 2){btn2 = 3;}
    else if(btn2 == 3){btn2 = 4;}
    else if(btn2 == 4){btn2 = 0;}
  }
  //text("brightness difference : " + abs(nbsum2 - pbsum2), 10, 480);
  
  pbsum2 = nbsum2;
}
 
void backbtn(){
  img.loadPixels();
  nbsum3 = 0;
  for(int dx = 0; dx < 20; dx++){
    for(int dy = 0; dy < 20; dy++){
      nbsum3 = nbsum3 + brightness(img.pixels[(dy)*img.width + (500+dx)]);
    }
  }
  
  if(abs(nbsum3 - pbsum3) > 10000){
    if(btn3 == 0){btn3 = 1;}
    else if(btn3 == 1){btn3 = 2;}
    else if(btn3 == 2){btn3 = 3;}
    else if(btn3 == 3){btn3 = 4;}
    else if(btn3 == 4){btn3 = 0;}
  }
  text("brightness difference : " + abs(nbsum3 - pbsum3), 10480);
  
  pbsum3 = nbsum3;
}
 
 
void dithering(){
  stat = 1;
  
  img.loadPixels();
  if(btn2 == 0){background(0);}
  else if(btn2 == 1){background(#25DBD6);}
  else if(btn2 == 2){background(#0130F7);}
  else if(btn2 == 3){background(#C03A1A);}
  else if(btn2 == 4){background(#8A3C86);}
  for(int x = 5; x < 635; x+=10){
    for(int y = 5; y < 475; y+=10){
      sum4 = 0;
      for(int a = -5; a<5; a++){
        for(int b = -5; b<5; b++){
          sum4 = sum4 + brightness(img.pixels[(y+b)*img.width + (x+a)]);
        }
      }     
 
      if (sum4 > 25000){
        ellipse(x, y, 1010);
        noStroke();
      }else if ((20000<sum4)&&(sum4<25000)){
        ellipse(x, y, 88);
        noStroke();
      }else if ((15000<sum4)&&(sum4<20000)){
        ellipse(x, y, 66);
        noStroke();
      }else if ((10000<sum4)&&(sum4<15000)){
        ellipse(x, y, 44);
        noStroke();
      }else if ((5000<sum4)&&(sum4<10000)){
        ellipse(x, y, 22);
        noStroke();
      }
      if(btn2 == 0){
        int crand = int(random(1,4));
        if (crand == 1){fill(#DA2429);}
        else if (crand == 2){fill(#FECF08);}
        else if (crand == 3){fill(#3FC5E5);}
        else if (crand == 4){fill(#75C379);}
      }
      else if(btn2 == 1){fill(#DA2429);}
      else if(btn2 == 2){fill(#FECF08);}
      else if(btn2 == 3){fill(#3FC5E5);}
      else if(btn2 == 4){fill(#75C379);}
    }
  }
}
 
 
class ParticleSystem {
  ArrayList<Particle> particles;
  PVector origin;
 
  ParticleSystem(PVector position) {
    origin = position.copy();
    particles = new ArrayList<Particle>();
  }
 
  void addParticle() {
    particles.add(new Particle(origin));
  }
 
  void run() {
    for (int i = particles.size()-1; i >= 0; i--) {
      Particle p = particles.get(i);
      p.run();
      if (p.isDead()) {
        particles.remove(i);
      }
    }
  }
}
 
class Particle {
  PVector position;
  PVector velocity;
  PVector acceleration;
  float lifespan;
 
  Particle(PVector l) {
    acceleration = new PVector(00.05);
    velocity = new PVector(random(-11), random(-20));
    position = l.copy();
    lifespan = 255.0;
  }
 
  void run() {
    update();
    display();
  }
 
  void update() {
    velocity.add(acceleration);
    position.add(velocity);
    lifespan -= 0.4;
  }
 
  void display() {
    //stroke(255, lifespan);
    noStroke();
    int crand = int(random(1,4));
    if (crand == 1){fill(#DA2429, lifespan);}
    else if (crand == 2){fill(#FECF08, lifespan);}
    else if (crand == 3){fill(#3FC5E5, lifespan);}
    else if (crand == 4){fill(#75C379, lifespan);}
    //fill(#854845, lifespan); //change color
    int rands = int(random(8,20));
    ellipse(position.x, position.y, rands, rands);
  }
 
  boolean isDead() {
    if (lifespan < 0.0) {
      return true;
    } else {
      return false;
    }
  }
}
cs



<실행 영상>