블로그 이미지
Leeway is... the freedom that someone has to take the action they want to or to change their plans.
maetel

Notice

Recent Post

Recent Comment

Recent Trackback

Archive

calendar

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
  • total
  • today
  • yesterday

Category

'Code'에 해당되는 글 41건

  1. 2007.08.30 simple wave test
  2. 2007.08.26 simple oscillation test
  3. 2007.08.03 2차원 행렬 테스트
  4. 2007.08.01 Lists
  5. 2007.08.01 David Hirmes - lines15
  6. 2007.07.31 David Hirmes - lines14
  7. 2007.07.25 David Hirmes - lines02
  8. 2007.07.18 David Hirmes - blobs01
  9. 2007.07.13 splash_class_multi
  10. 2007.07.13 splash_class
  11. 2007.07.12 progress_class
  12. 2007.07.11 생성자와 소멸자
  13. 2007.07.09 popping_1.1
  14. 2007.07.06 popping_1.0 (수정 중)
  15. 2007.07.06 popping_0.0
  16. 2007.07.05 storing_test
  17. 2007.07.05 pop_test
  18. 2007.07.04 matrix_glimmer_dim
  19. 2007.07.04 matrix-glimmer
  20. 2007.06.27 matrix_color_chain
  21. 2007.06.27 matrix-color change
  22. 2007.06.27 matrix
  23. 2007.06.25 arrive
  24. 2007.06.25 steering
  25. 2007.06.20 ping_orbit
2007. 8. 30. 03:29 Code/Processing

//finn interaction test wave
//ref. http://www.shiffman.net/itp/classes/nature/week04_s06/sine/

int xs = 5;
int w;

float th = 0.0f;
float a = 60.0;
float p = 400.0f;
float dx;
float[] y;

void setup(){
  size(320,240);
  frameRate(30);
  colorMode(RGB,255,255,255,80);
  smooth();
  w = width+8;
  dx = (TWO_PI/p)*xs;
  y = new float[w/xs];
}

void draw(){
  background(0);
  calcWave();
  renderWave();
}

void calcWave(){
  th += 0.02;
  float x = th;
  for (int i=0; i<y.length; i++){
    y[i]=a*sin(x);
    x+=dx;
  }
}

void renderWave(){
  for (int x=0; x<y.length; x++){
    noStroke();
    fill(255,0,0);
    ellipseMode(CENTER);
    ellipse(x*xs,width/2+y[x],5,5);
  }
}

'Code > Processing' 카테고리의 다른 글

stair  (0) 2006.08.11
clock  (0) 2006.06.19
posted by maetel
2007. 8. 26. 15:19 Code/NodeBox
막상 해 보지 않아서 찜찜했었는데, 이걸 이제야 해 보네.

from math import sin, radians, pi

size(320,240)
speed(100)

def setup():
    global x
    global y
    global p ##period
    global a ##amplitude
    global t ##time
   
    p = 100
    a = HEIGHT/4
    t = 0



def draw():
    global x
    global y
    global p
    global a
    global t
   
    t += 1
    x = t
    w = 2*pi*t/p
    y = a*sin(w) + HEIGHT/2
   
    if x >= WIDTH:
        t = 0
   
    oval(x,y,10,10)

'Code > NodeBox' 카테고리의 다른 글

David Hirmes - lines15  (0) 2007.08.01
David Hirmes - lines14  (0) 2007.07.31
David Hirmes - lines02  (0) 2007.07.25
David Hirmes - blobs01  (0) 2007.07.18
splash_class_multi  (0) 2007.07.13
posted by maetel
2007. 8. 3. 16:37 Code/Python
code:
s = 10
c = range(s)

for y in range(s):
    c[y] = [ [x,y] for x in range(s) ]

print c

run:
[[[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0]],
[[0, 1], [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [7, 1], [8, 1], [9, 1]],
[[0, 2], [1, 2], [2, 2], [3, 2], [4, 2], [5, 2], [6, 2], [7, 2], [8, 2], [9, 2]],
[[0, 3], [1, 3], [2, 3], [3, 3], [4, 3], [5, 3], [6, 3], [7, 3], [8, 3], [9, 3]],
[[0, 4], [1, 4], [2, 4], [3, 4], [4, 4], [5, 4], [6, 4], [7, 4], [8, 4], [9, 4]],
[[0, 5], [1, 5], [2, 5], [3, 5], [4, 5], [5, 5], [6, 5], [7, 5], [8, 5], [9, 5]],
[[0, 6], [1, 6], [2, 6], [3, 6], [4, 6], [5, 6], [6, 6], [7, 6], [8, 6], [9, 6]],
[[0, 7], [1, 7], [2, 7], [3, 7], [4, 7], [5, 7], [6, 7], [7, 7], [8, 7], [9, 7]],
[[0, 8], [1, 8], [2, 8], [3, 8], [4, 8], [5, 8], [6, 8], [7, 8], [8, 8], [9, 8]],
[[0, 9], [1, 9], [2, 9], [3, 9], [4, 9], [5, 9], [6, 9], [7, 9], [8, 9], [9, 9]]]


그런데, 다음과 같이 하면 전혀 다른 결과가 나온다.

s = 10
c = range(s)

for y in range(s):
    for x in range(s):
        c[y] = [x,y]

    print c

[[9, 0], 1, 2, 3, 4, 5, 6, 7, 8, 9]
[[9, 0], [9, 1], 2, 3, 4, 5, 6, 7, 8, 9]
[[9, 0], [9, 1], [9, 2], 3, 4, 5, 6, 7, 8, 9]
[[9, 0], [9, 1], [9, 2], [9, 3], 4, 5, 6, 7, 8, 9]
[[9, 0], [9, 1], [9, 2], [9, 3], [9, 4], 5, 6, 7, 8, 9]
[[9, 0], [9, 1], [9, 2], [9, 3], [9, 4], [9, 5], 6, 7, 8, 9]
[[9, 0], [9, 1], [9, 2], [9, 3], [9, 4], [9, 5], [9, 6], 7, 8, 9]
[[9, 0], [9, 1], [9, 2], [9, 3], [9, 4], [9, 5], [9, 6], [9, 7], 8, 9]
[[9, 0], [9, 1], [9, 2], [9, 3], [9, 4], [9, 5], [9, 6], [9, 7], [9, 8], 9]
[[9, 0], [9, 1], [9, 2], [9, 3], [9, 4], [9, 5], [9, 6], [9, 7], [9, 8], [9, 9]]


'Code > Python' 카테고리의 다른 글

Lists  (0) 2007.08.01
생성자와 소멸자  (0) 2007.07.11
posted by maetel
2007. 8. 1. 13:17 Code/Python
ref. 이강성 <열혈강의 파이썬> 5장 리스트

List 리스트: 순서를 가지는 객체들의 집합.

1) 시퀀스 자료형이면서 변경 가능(mutable) 형이다.
    : 시퀀스 자료형의 일반적인 특징(인덱싱, 슬라이싱, 연결, 반복, 멤버십 테스트 등)을 지원하며, 변경이 가능한 특성에 따라 자료의 크기를 동적으로 임의 조절하거나, 내용을 치환하여 변경할 수 있다.
 
2) 리스트는 다른 객체를 직접 저장하지 않고, 객체들의 레퍼런스(Reference)만을 저장한다. (레퍼런스란 객체의 주소를 말한다.)


ref. Guido van Rossum's Python Tutorial: 3.1.4 Lists

- built-in function len()
- to nest lists (create lists containing other lists)
- object semantics

5.1 More on Lists

append(x)
extend(L)
insert(i, x)
remove(x)
pop([i])
index(x)
count(x)
sort(x)
reverse()


Stack 스택: 나중에 넣은 데이터를 먼저 꺼내도록 되어 있는 메모리 구조
Queue 큐: 먼저 넣은 데이터를 먼저 꺼내도록 되어 있는 메모리 구조
    - push 연산 (append 메쏘드) &pop 연산 (pop 메쏘드)


filter(function, sequence)

map(function, sequence)

reduce(function, sequence)


ref. NodeBox | Lists

choice(list) 
    = list[random(len(list)]

'Code > Python' 카테고리의 다른 글

2차원 행렬 테스트  (0) 2007.08.03
생성자와 소멸자  (0) 2007.07.11
posted by maetel
2007. 8. 1. 11:14 Code/NodeBox
도전장: David Hirmes - lines15 (ref. Flash Math Creativity)
원래 이걸 하려고 했는데 lines14에서 조금 더 응용된 것이라 우선 lines14를 먼저 해 보았던 것이다.

5초만에 끝. ^^

s = 300
n = 6

size(s, s)
speed(200)

from math import cos, sin, acos, sqrt

def setup():  
    global x
    global y
    global f    
    global g
   
    x = range(s)
    y = range(s)   
    for i in range(s):
        x[i] = random(s)
        y[i] = random(s)

    f = 0
    g = 0


def draw():
    global x
    global y   
    global f   
    global g

    dx = x[f+1] - x[f]
    dy = y[f+1] - y[f]
       
    dd = dx**2 + dy**2
    d = sqrt(dd)
    l = g * d/n
   
    da = acos(dx/d)

    if dy < 0:
        da = -da
        ph = 6.28
    else:
        ph = 0

    a = g * (da+ph)/n
   
    for k in range(f):
        stroke(0)
        line(x[k], y[k], x[k+1], y[k+1])       
        line(x[f], y[f], x[f]+l*cos(a), y[f]+l*sin(a))
       
       
    g += 1
       
    if g > n:
        g = 0
        f += 1
       
    if f > s-1:
        f = 0

'Code > NodeBox' 카테고리의 다른 글

simple oscillation test  (0) 2007.08.26
David Hirmes - lines14  (0) 2007.07.31
David Hirmes - lines02  (0) 2007.07.25
David Hirmes - blobs01  (0) 2007.07.18
splash_class_multi  (0) 2007.07.13
posted by maetel
2007. 7. 31. 18:46 Code/NodeBox
The mission:
사용자 삽입 이미지

David Hirmes - lines14

from Flash Math Creativity


첫번째 시도... 실패
s = 300
r = 10

size(s, s)
speed(10)

from math import cos, sin, atan, sqrt

def setup():
    global x
    global y    
    global f   
    global g
            
    x = range(s)
    y = range(s)    
    for i in range(s):
        x[i] = random(s)
        y[i] = random(s)

    f = 0
    g = 0
   

def draw():
    global x
    global y
    global f 
    global g
      
    dd = (x[f+1] - x[f])**2 + (y[f+1] - y[f])**2
    d = sqrt(dd)

    ta = atan(y[f+1]/x[f+1]) - atan(y[f]/x[f])
#    ta = atan((y[f+1]-y[f]) / (x[f+1]-x[f]))
    a = g * ta/r
    
    rx = d*cos(a)
    ry = d*sin(a)
    
    for k in range(f):
        stroke(0)
        line(x[k], y[k], x[k+1], y[k+1])        
        line(x[f], y[f], x[f]+rx, y[f]+ry)


    g += 1
        
    if g > r:
        g = 0
        f += 1

아웅~
인덱스가 꼬여 있고, ta가 잘못되었다. (원인은 아는데 해결을 못하고 있다.)
+ 회전하는 line을 그릴 때 (또는 계산할 때) translate를 활용할 수 있음을 기억할 것.


python에서의 삼각함수 테스트:
## testing trigonometry

from math import cos, radians, degrees

a = range(13)
for i in range(13):
    a[i] = 30*i
    x = a[i]
    print x, ":", cos(radians(x)), ":", cos(degrees(x)), ":", cos(x)

결과:
0 : 1.0 : 1.0 : 1.0
30 : 0.866025403784 : -0.912188068927 : 0.154251449888
60 : 0.5 : 0.664174146187 : -0.952412980415
90 : 6.12323399574e-17 : -0.299515394756 : -0.448073616129
120 : -0.5 : -0.117745407075 : 0.814180970527
150 : -0.866025403784 : 0.514327305764 : 0.699250806478
180 : -1.0 : -0.820581056609 : -0.598460069058
210 : -0.866025403784 : 0.982721193088 : -0.883877473182
240 : -0.5 : -0.972272038226 : 0.325781305535
270 : -1.83697019872e-16 : 0.791068712954 : 0.984381950633
300 : 0.5 : -0.470934845091 : -0.0220966192787
330 : 0.866025403784 : 0.0680935809118 : -0.991198821755
360 : 1.0 : 0.346706540931 : -0.283691091487
  
radians이군. 그래, 전에도.
그러나... arctangent 값은 이미 radians이므로 필요없다. (원래 알았었는데 괜히 몇 시간 허비... ㅡㅜ)


회전만 시험하면서 여전히 헤맨다. 역시나 회전 각도 da가 문제인데,
dx와 dy의 부호 때문이다. phase를 잡아 주어야 하는 것.
상식적인데 잘 안 된다. 고등학교 때였으면 바로 됐을 텐데... orz


(몇 십분이 흐른 후...) 2pi 잖아! 바보다...
이제 회전은 의도대로 된다. 몇 가지 변수명을 수정하고 정리하면 다음과 같다.
이제 갖다 붙이기만 하면...

from math import cos, sin, sqrt, acos, degrees

s = 100
size(200, 200)
speed(100)

def setup():
    global x
    global y
    global f
    global ph
   
    x = range(2)
    y = range(2)
    for i in range(2):
        x[i] = random(50,100)
        y[i] = random(s)
   
    f = 0
    ph = 0
   
def draw():
    global x
    global y
    global f
    global ph

    oval(x[0],y[0],5,5)
    oval(x[1],y[1],3,3)

    dx = x[1] - x[0]
    dy = y[1] - y[0]   

    dd = dx**2 + dy**2
    d = sqrt(dd)
   
    da = acos(dx/d)
   
    if dy < 0:   
        da = -da
        ph = 6.28
      
    r = f * (da+ph)/10   
   
    stroke(0)
    translate(x[0], y[0])
    line(0, 0, d*cos(r), d*sin(r))
        
    if f < 10:
        f += 1

cos을 통해 각도 차이를 구했으므로 dy에 대해서만 고려해 주면 된다. sin 함수 역시 2pi를 주기로 한다. 바보야. (sin(a) = sin(2pi + a))


성공!

s = 200
n = 5

size(s, s)
speed(200)

from math import cos, sin, acos, sqrt

def setup():   
    global x
    global y
    global f     
    global g
    
    x = range(s)
    y = range(s)    
    for i in range(s):
        x[i] = random(s)
        y[i] = random(s)

    f = 0
    g = 0


def draw():
    global x
    global y    
    global f    
    global g
    
    #oval(x[f], y[f], 5, 5)
    #oval(x[f+1], y[f+1], 3, 3)

    dx = x[f+1] - x[f]
    dy = y[f+1] - y[f]
        
    dd = dx**2 + dy**2
    d = sqrt(dd)
   
    da = acos(dx/d)

    if dy < 0:
        da = -da
        ph = 6.28
    else:
        ph = 0

    a = g * (da+ph)/n
    #rx = d*cos(a)
    #ry = d*sin(a)
    #print f, g, da, a
       
    for k in range(f):
        stroke(0)
        line(x[k], y[k], x[k+1], y[k+1])        
        line(x[f], y[f], x[f]+d*cos(a), y[f]+d*sin(a))
        
        
    g += 1
        
    if g > n:
        g = 0
        f += 1
        
    if f > s-1:
        f = 0


'Code > NodeBox' 카테고리의 다른 글

simple oscillation test  (0) 2007.08.26
David Hirmes - lines15  (0) 2007.08.01
David Hirmes - lines02  (0) 2007.07.25
David Hirmes - blobs01  (0) 2007.07.18
splash_class_multi  (0) 2007.07.13
posted by maetel
2007. 7. 25. 23:10 Code/NodeBox
The mission:
사용자 삽입 이미지

David Hirmes - lines02

from Flash Math Creativity

노드박스에서는 (프로세싱과 달리) draw()가 실행될 때마다 자동으로 background를 깔아 주므로 애니메이션을 구현하기는 편리하나, 자취를 남기는 방법에 대해서는 따로 생각해 주어야 한다.

s = 300

size(s, s)
speed(20)

def setup():
    global x
    global y
    global f

    f = 0
       
    x = range(s)
    y = range(s)
   
    for i in range(s):
        x[i] = random(s)
        y[i] = random(s)
       

def draw():
    global x
    global y
    global f
   
    f += 1

    if f >= s-1:
        f = 0
   
    for k in range(f):
        stroke(0)
        line(x[k], y[k], x[k+1], y[k+1])
        fill(1, 0, 0)
        ovalc(x[k+1], y[k+1], 8, 8)
        ovalc(x[k], y[k], 8, 8) #to hide the segments of lines in ovals


def ovalc(cx, cy, w, h):
    x = cx - w/2
    y = cy - h/2
    oval(x, y, w, h)

위 코드에서 파란색 라인을 넣지 않으면 아래와 같이 다음 선분이 그려지면서 원 안에 자국이 남는다.

사용자 삽입 이미지


이를 없애기 위해 추측했던 바가 맞아 떨어짐을 다음과 같이 확인할 수 있었다.
 
사용자 삽입 이미지

그런데 이 역시, 이후 랜덤하게 그려지는 선들이 기존의 꼭지점들과 교차하면서 나타나는 overlapping을 cover하지 못하고 있다. (이런 국적불명의 문장이란...) 고민해 봐야 할 문제다.

뭐, 이런 무식한 방법이 있기도 하다.

s = 300
d = 8

size(s, s)
speed(5)

def setup():
    global x
    global y
    global f

    f = 0
        
    x = range(s)
    y = range(s)
    
    for i in range(s):
        x[i] = random(d/2, s-d/2)
        y[i] = random(d/2, s-d/2)
        

def draw():
    global x
    global y
    global f
    
    f += 1

    if f >= s-1:
        f = 0
    
    for k in range(f):
        stroke(0)
        line(x[k], y[k], x[k+1], y[k+1])
        
        for p in range(k+1):
            fill(1, 0, 0)
            ovalc(x[p+1], y[p+1], d, d)
            ovalc(x[0], y[0], d, d)

def ovalc(cx, cy, w, h):
    x = cx - w/2
    y = cy - h/2
    oval(x, y, w, h)


그리고 결과야 나온다. k가 증가할수록 속도가 느려져서 문제지.

사용자 삽입 이미지

s = 300
d = 8

size(s, s)
speed(10)

def setup():
    global x
    global y
    global f
    global zz
    f = 0
       
    x = range(s)
    y = range(s)

    for i in range(s):
        x[i] = random(d/2, s-d/2)
        y[i] = random(d/2, s-d/2)
       

def draw():
    global x
    global y
    global f
    global zz
   
    f += 1

    if f >= s-1:
        f = 0

    for k in range(f):
        stroke(0)
        line(x[k], y[k], x[k+1], y[k+1])
       
    for p in range(f):
        fill(1, 0, 0)
        ovalc(x[p+1], y[p+1], d, d)

    ovalc(x[0], y[0], d, d)

def ovalc(cx, cy, w, h):
    x = cx - w/2
    y = cy - h/2
    oval(x, y, w, h)

세상에... line과 oval이 관련되어 있다는 사실 때문에 p가 k에 dependent해야 하는 줄 알았었다. line과 oval은 종속변수를 공유하고 있을 뿐이며 오로지 그것으로 충분하다. simulation과 visualization의 차이를 다시 한 번 실감한다.

'Code > NodeBox' 카테고리의 다른 글

David Hirmes - lines15  (0) 2007.08.01
David Hirmes - lines14  (0) 2007.07.31
David Hirmes - blobs01  (0) 2007.07.18
splash_class_multi  (0) 2007.07.13
splash_class  (0) 2007.07.13
posted by maetel
2007. 7. 18. 12:41 Code/NodeBox
The mission I chose:
사용자 삽입 이미지

David Hirmes - blobs01

from Flash Math Creativity


우선, blob 하나의 움직임을 코딩해 보자.
#hirmes_blobs01_0.0

size(400, 200)
speed(100)

def setup():
    global x
    global v
    global a
   
    global check
    global c

    x = 0
    v = 10
    a = -0.1*v
   
    check = 0
    c = 0

def draw():
    global x
    global v
    global a
   
    global check
    global c
     
    v += a
    x += v
   
    if x > WIDTH:
        x = 0
    if x < 0:
        x = WIDTH
   
    check += c
       
    if v < 0:
        v = 0
        a = 0
        c = 1
    if check == 40:
        v = 15
        a = -1
        check = 0
          
    oval(x, 50, 50, 50)
전에 배운 pattern에서 실마리를 얻어 새로운 pattern을 만들어 보았다.
내가 생각해도 good idea! 쿄쿄.


이것을 아래의 move 메쏘드에 적용하면 된다.  
원하는 결과를 위해서는 다른 색의 방울 두 개에 동시에 동일한 움직임을 주어야 했는데, 이를 위해 Blob 클래스에서 움직임을 주는 메쏘드(move)와 색을 넣어 그리는 메쏘드(ball)를 분리하는 것을 하나의 해결책으로 제시해 보았다.
# hirmes_blobs01_0.1

class Blob:
    def __init__(self):
        self.f = 0
        
        self.x0 = 20
        self.y0 = 100
        self.x1 = 100
        self.y1 = 120
        
        self.v = 10
        self.a = -0.1*self.v
        self.check = 0
        self.c = 0
        
    def move(self):
        self.v += self.a

        self.x0 += self.v
        self.x1 += self.v * 0.8

        ###borders     
        if self.x0 > WIDTH:
            self.x0 = 0
        if self.x1 > WIDTH:
            self.x1 = 0
        if self.x0 < 0:
            self.x0 = WIDTH
        if self.x1 < 0:
            self.x1 = WIDTH  
       
       ###pauses
        self.check += self.c
        
        if self.v < 0:
            self.v = 0
            self.a = 0
            self.c = 1

        if self.check == 40:
            self.v = 15
            self.a = -1
            self.check = 0
        
    def ball(self, d, r, g, b):                    
        fill(r, g, b)
        ovalc(self.x0, self.y0, d, d)
        ovalc(self.x1, self.y1, d, d)
        
       
size(400, 300)
speed(30)


def setup():
    global bb
    
    bb = Blob()

    
def draw():
    global bb
    
    bb.move()
    bb.ball(80, 1, 0, 0)
    bb.ball(70, 1, 1, 1)

def ovalc(cx, cy, w, h):
    x = cx-w/2
    y = cy-h/2
    oval(x, y, w, h)  


이제 방울 수를 늘려 보자.
# hirmes_blobs01_1.0

class Blob:
    def __init__(self):
        ###initial position
        self.x0 = random(-WIDTH,WIDTH)
        self.x1 = random(-WIDTH,WIDTH)
        self.x2 = random(-WIDTH,WIDTH)
        self.x3 = random(-WIDTH,WIDTH)
        self.x4 = random(-WIDTH,WIDTH)
        self.x5 = random(-WIDTH,WIDTH)
        self.x6 = random(-WIDTH,WIDTH)
        self.x7 = random(-WIDTH,WIDTH)
        self.x8 = random(-WIDTH,WIDTH)
        self.x9 = random(-WIDTH,WIDTH)

        tb = 50
        self.y0 = random(tb,HEIGHT-tb)
        self.y1 = random(tb,HEIGHT-tb)
        self.y2 = random(tb,HEIGHT-tb)
        self.y3 = random(tb,HEIGHT-tb)
        self.y4 = random(tb,HEIGHT-tb)
        self.y5 = random(tb,HEIGHT-tb)
        self.y6 = random(tb,HEIGHT-tb)
        self.y7 = random(tb,HEIGHT-tb)
        self.y8 = random(tb,HEIGHT-tb)      
        self.y9 = random(tb,HEIGHT-tb)
              
        self.x = range(10)
        self.x[0] = self.x0
        self.x[1] = self.x1
        self.x[2] = self.x2
        self.x[3] = self.x3
        self.x[4] = self.x4
        self.x[5] = self.x5
        self.x[6] = self.x6
        self.x[7] = self.x7
        self.x[8] = self.x8
        self.x[9] = self.x9
              
        ###factors to make a move
        self.v = 15
        self.a = -0.1*self.v
      
        self.check = 0
        self.c = 0
 
    def move(self):
        self.v += self.a
        self.check += self.c
                 
        ###stop and then move again
        if self.v < 0:
            self.v = 0
            self.a = 0
            self.c = 1
       
        if self.check == 30:
            self.v = 15
            self.a = -0.1*self.v
            self.check = 0
      
       ###to move the centers of blobs
        for i in range(10):
            self.x[i] += self.v * 0.5 * (i+1)
          
            ###borders              
            if self.x[i] > WIDTH:
                self.x[i] = 0
            if self.x[i] > WIDTH:
                self.x[i] = 0
  
    def ball(self, d, r, g, b):                               
        y = range(10)
        y[0] = self.y0
        y[1] = self.y1
        y[2] = self.y2
        y[3] = self.y3
        y[4] = self.y4
        y[5] = self.y5
        y[6] = self.y6
        y[7] = self.y7
        y[8] = self.y8
        y[9] = self.y9
      
        fill(r, g, b)     
        for i in range(10):      
            ovalc(self.x[i], y[i], d, d)

 
size(400, 300)
speed(30)

def setup():
    global bb
  
    bb = Blob()

  
def draw():
    global bb
  
    bb.move()
    bb.ball(110, 1, 1, 1)
    bb.ball(100, 0.6, 0.6, 1)
    background(0, 0, 1)

def ovalc(cx, cy, w, h):
    x = cx-w/2
    y = cy-h/2
    oval(x, y, w, h)

각 방울들이 range 변수(클래스 멤버 self.x)로 조정되고 있는 데 반해, 휴지기를 설정해 주는  self.check 값이 정수로 고정되어 있어 모든 방울들이 일시에 멈추고 일시에 움직인다. self.check를 리스트 x에 관해 정의해야겠다는 결론이다. (이 결정은 총 10시간이 넘는 시도 끝에 포기되었고...)

(다음 날...)
애초에 방울들의 집합을 클래스 안에 넣었던 이유는, 그렇게 하지 않으면 draw()에서 개체별로 방울들을 그리게 되어 원치않은 윤곽선(큰 원)이 남는다고 생각했기 때문이었다. 하지만 아래와 같이 별개의 for 구문을 써서 그리게 되면(ball()) 이 문제가 해결된다. 그러므로 굳이 클래스 안에서 클래스 멤버를 리스트로 만들 이유가 없다. (사실, 아니라면 클래스를 이용해서 얻는 효용이 무엇이란 말인가... 어제의 조악한 코드를 보라. 하나의 판단 착오가 엄청난 삽질을 가져왔음을 알 수 있다.)
v0.0의 코드를 그대로 적용하되, 클래스 외부에서 클래스 멤버에 접근하는 (친숙한) 방식으로 코딩한 다음, 클래스를 보다 깔끔하게 만들기 위해 메쏘드들을 단지 재정리하기만 했다.
 

# hirmes_blobs01_2.1

class Blob:
    def __init__(self,x):      
        self.x = x
        tb = HEIGHT/10
        self.y = random(tb, HEIGHT-tb)
       
        self.v = 20
        self.a = -0.1*self.v
       
        self.check = 0
        self.c = 0
       
    def move(self, m):
        self.v += self.a
        self.x += self.v * m

        ###borders    
        if self.x > WIDTH:
            self.x = 0
        if self.x < 0:
            self.x = WIDTH 
   
    def pause(self, p):
        self.check += self.c
       
        if self.v <= 0:
            self.v = 0
            self.a = 0
            self.c = 1

        if self.check == 20 * p:
            self.v = 20
            self.a = -0.1*self.v
            self.check = 0
            self.c = 0
       
    def ball(self, d, r, g, b):                   
        fill(r, g, b)
        ovalc(self.x, self.y, d, d)
       
def ovalc(cx, cy, w, h):
    x = cx-w/2
    y = cy-h/2
    oval(x, y, w, h)  
       
      
size(600, 300)
speed(60)


def setup():
    global bb
   
    bb = range(10)
    b = range(10)
   
    for i in range(10):
        b[i] = random(WIDTH)   
        bb[i] = Blob(b[i])
   
def draw():
    global bb

    m = range(10)
    p = range(10)
   
    for i in range(10):
        m[i] = (i+1)*0.5       
        p[i] = i+1
       
   
    background(0, 0, 1)
   
    for i in range(10):       
        bb[i].move(m[i])
        bb[i].pause(p[i])
        bb[i].ball(120, 1, 1, 1)

    for i in range(10):
        bb[i].ball(110, 0.6, 0.6, 1)

m과 p에 의해 방울들이 움직이는 속력(거리)과 멈추는 시간(차례)에 변화를 줄 수 있다. 비로소 뭔가 내어 놓을 수 있는 코드가 된 것 같다.
다시 한 번 짚어 보면, 윤곽선을 남기지 않는 문제는 class 내에서 그리는 메쏘드를 별도로 정의해 주는 것만으로 완전히 해결되었던 것이다.

사용자 삽입 이미지

'Code > NodeBox' 카테고리의 다른 글

David Hirmes - lines14  (0) 2007.07.31
David Hirmes - lines02  (0) 2007.07.25
splash_class_multi  (0) 2007.07.13
splash_class  (0) 2007.07.13
progress_class  (0) 2007.07.12
posted by maetel
2007. 7. 13. 17:52 Code/NodeBox
사용자 삽입 이미지
Splash를 여러 개 만들기.
초기 위치와  터지는 위치를 설정하여 여러 개의 물방울을 만들자.

from math import sin, cos, radians

def ovalc(cx, cy, w, h):
    x = cx-w/2
    y = cy-h/2
    oval(x, y, w, h)

class Splash:
    def __init__(self,x,y,p):
        self.f = 0
        self.x = x
        self.y = y
        self.p = p

        self.d = range(6)
        self.hit = 0
            
    def fall(self):
        self.f += 1
        
        if self.y <= self.p:
            self.y = self.f*10        
            ovalc(self.x, self.y, 20, 20)


        if (self.y > self.p) & (self.hit==0):
            for i in range(6):
                self.d[i] = Drop(self.x,self.p,i*60)
                    
            self.hit = 1
        
        if self.hit==1:
            for i in range(6):
                k = self.d[i]
                k.scatter()

            
class Drop:       
    def __init__(self,x,p,a):
        self.f = 0
        self.x = x
        self.p = p
        self.a = a
        
    def scatter(self):
        r = 2*self.f
        self.f += 1
        a = self.a
        
        x = self.x + r*cos(radians(a))
        y = self.p + r*sin(radians(a))             
 
        ovalc(x, y, 10, 10)

size(300,300)
speed(150)

def setup():
    global c
    global c0
    global c1
    global c2
    global c3
    global c4
 
    c = range(5)   
    c[0] = Splash(50,0,100)
    c[1] = Splash(100,0,250)
    c[2] = Splash(150,0,150)
    c[3] = Splash(200,0,100)
    c[4] = Splash(250,0,200)
    
    c0 = c[0]
    c1 = c[1]
    c2 = c[2]
    c3 = c[3]
    c4 = c[4]
    
def draw():
    global c
    global c0
    global c1
    global c2
    global c3
    global c4
   
    c0.fall()
    c1.fall()
    c2.fall()
    c3.fall()
    c4.fall()

Drop 클래스의 self.x 와 self.y 는 Splash 클래스의 것들과 다르다.
메쏘드 fall에서 Drop 클래스의 인스턴스를 생성할 때 인수를 클래스 멤버로 입력하면, 결과적으로 다른 클래스의 멤버에 접근할 수 있게 된다.

'Code > NodeBox' 카테고리의 다른 글

David Hirmes - lines02  (0) 2007.07.25
David Hirmes - blobs01  (0) 2007.07.18
splash_class  (0) 2007.07.13
progress_class  (0) 2007.07.12
popping_1.1  (0) 2007.07.09
posted by maetel
2007. 7. 13. 16:40 Code/NodeBox
splash 예제를 두 개의 class를 써서 코딩하기

from math import sin, cos, radians

def ovalc(cx, cy, w, h):
    x = cx-w/2
    y = cy-h/2
    oval(x, y, w, h)

class Splash:
    def __init__(self):
        self.f = 0
        self.x = 0
        self.y = 0

        self.d = range(6)
        self.hit = 0
            
    def fall(self):
        self.x = WIDTH/2
        self.f += 1
        
        if self.y <= HEIGHT/2:
            self.y = self.f*10        
            ovalc(self.x, self.y, 20, 20)


        if (self.y > HEIGHT/2) & (self.hit==0):
            for i in range(6):
                self.d[i] = Drop(i*60)
                    
            self.hit = 1
        
        if self.hit==1:
            for i in range(6):
                ttt=self.d[i]
                ttt.scatter()
 
            
class Drop:       
    def __init__(self,a):
        self.f = 0
        self.a = a
        
    def scatter(self):
        r = 2*self.f
        self.f += 1
        a = self.a
        
        x = WIDTH/2 + r*cos(radians(a))
        y = HEIGHT/2 + r*sin(radians(a))             
 
        ovalc(x, y, 10, 10)


size(300,300)
speed(150)

def setup():
    global c    
    c = Splash()
    
def draw():
    global c
    c.fall()

lesson 1. class를 range 변수로 선언하는 것이 가능하다. (class member를 range의 순차번호를 매개로 했다.)
lesson 2. 어떠한 사건이 일어난 첫순간을 인식하여 사용할 때 쓰이는 pattern을 배웠다. (상기 코드의 self.hit 변수)

'Code > NodeBox' 카테고리의 다른 글

David Hirmes - blobs01  (0) 2007.07.18
splash_class_multi  (0) 2007.07.13
progress_class  (0) 2007.07.12
popping_1.1  (0) 2007.07.09
popping_1.0 (수정 중)  (0) 2007.07.06
posted by maetel
2007. 7. 12. 19:05 Code/NodeBox
progress 예제를 class를 써서 코딩하기

#1. 클래스 멤버를 동적으로 호출한다.
class Progress:
    def __init__(self):
        self.f= 0
        self.x= 0
        self.y= 0
                    
    def march(self):
        self.f+=1
        self.x = self.f*10
        
        if self.x > WIDTH:
            self.f = 0
            self.y += 50
        if self.y > HEIGHT:
            self.f = 0    
            self.y = 0
        
    def x(self):
        return self.x

    def y(self):
        return self.y
        
p = Progress()
#print p.march()

size(200,200)
speed(20)

def setup():
    global p
    p = Progress()
     
def draw():    
    global p
    p.march()

    fill(0)
    rect (p.x, p.y, 10, 10)          
 

#2. draw()에서 Progress.march()를 호출하므로 y의 초기값을  밖으로 빼 주어야 한다.
이 때, x는 self.f를 매개변수로 하므로 method 안에서 선언해도 상관이 없다.
class Progress:
    def __init__(self):
        self.f = 0
        self.y = 0
        
    def march(self):
        x = self.f*10
        self.f += 1
       
        if x > WIDTH:
            self.f = 0
            self.y+=50
           
        if self.y > HEIGHT:
            self.f = 0   
            self.y=0
           
        rect (x, self.y, 10, 10)


p = Progress()

size(200,200)
speed(20)

def setup():
    global p
    p = Progress()
    
def draw():   
    global p

    fill(0)     
    p.march()

class 안의 method에서 선언된 변수는 외부에서 직접 호출할 수 없다. 이에 대해 더 확실히 정리할 것!

'Code > NodeBox' 카테고리의 다른 글

splash_class_multi  (0) 2007.07.13
splash_class  (0) 2007.07.13
popping_1.1  (0) 2007.07.09
popping_1.0 (수정 중)  (0) 2007.07.06
popping_0.0  (0) 2007.07.06
posted by maetel
2007. 7. 11. 20:37 Code/Python
이강성 <파이썬> 379p - 생성자와 소멸자

from time import time, ctime, sleep

class Life:
    def __init__(self):
        self.birth = ctime()
        print 'Birthday', self.birth
    def __del__(self):
        print 'Deathday', ctime()
        
def test():
    mylife = Life()
    print 'Sleepling for 3 sec'
    sleep(3)
    
test()

실행하면
Birthday Wed Jul 11 20:29:36 2007
Sleepling for 3 sec
Deathday Wed Jul 11 20:29:39 2007


test 함수가 불리면서 Life 클래스 인스턴스가 하나 생성된다. 이때 자동으로 __init__ 함수가 호출되어 Birthday를 츨력한다. 3초 후에 test 함수가 종료하면서 (mylife는 지역 이름이므로 함수가 종료되면서 사라진다.) 인스턴스가 소멸된다. 이때 자동으로 __del__ 함수가 호출된다.

'Code > Python' 카테고리의 다른 글

2차원 행렬 테스트  (0) 2007.08.03
Lists  (0) 2007.08.01
posted by maetel
2007. 7. 9. 19:09 Code/NodeBox
1. 언급하였듯 i=1일 때가 문제였으므로, 그릴 때 i=1부터 그리게 했더니 해결되었다.
2. 마우스가 움직이는 동안 따라오는 최초의 사각형(t번째)에 t-1번째 사각형이 더해지는 현상을 없애기 위해 u[i]의 매개변수를 조정해 주면 된다는 것을 알았다.

size(400,400)
speed(100)


def setup():
    global mx
    global my
    global t

    global x0
    global y0
    global x1
    global y1
    global x2
    global y2
    global x3
    global y3
    global x4
    global y4
    global x5
    global y5
    global x6
    global y6
    global x7
    global y7
    
    global d
    global s
    global r
    global u
    global p
    
    t=20 #term
    mx=range(t)
    my=range(t)

    for n in range(t):
        mx[n]=-WIDTH
        my[n]=-HEIGHT
        
    #x=range(8)
    #y=range(8)    
    x0=range(t)
    y0=range(t)
    x1=range(t)
    y1=range(t)
    x2=range(t)
    y2=range(t)
    x3=range(t)
    y3=range(t)
    x4=range(t)
    y4=range(t)
    x5=range(t)
    y5=range(t)
    x6=range(t)
    y6=range(t)
    x7=range(t)
    y7=range(t)
    
    u=range(t)    
    p=range(t)
            
    d=20 #distance
    s=2 #initial size
    r=0.99 #ratio of size decreasing

    
def draw():
    global mx
    global my
    global t

    global x0
    global y0
    global x1
    global y1
    global x2
    global y2
    global x3
    global y3
    global x4
    global y4
    global x5
    global y5
    global x6
    global y6
    global x7
    global y7
    
    global d
    global s
    global r
    global u
    global p   
        
    background(0,0,1)    
    fill(1)
    
    for i in range(t-1):
        mx[i]=mx[i+1]
        my[i]=my[i+1]
        
        mx[t-1]=MOUSEX
        my[t-1]=MOUSEY

    rectc(mx[i],my[i],s,s)
            
    for i in range(t):        
        u[i]=(t-1-i)*2 ##to controll the stages of popping
        p[i]=-i ##to controll the sizes of popping


        if (i>=1) & ((mx[i]-mx[i-1])==0):    
            u[i]=0
           
        x0[i]=mx[i]+u[i]
        y0[i]=my[i]
        x1[i]=mx[i]+u[i]
        y1[i]=my[i]+u[i]
        x2[i]=mx[i]
        y2[i]=my[i]+u[i]
        x3[i]=mx[i]-u[i]
        y3[i]=my[i]+u[i]
        x4[i]=mx[i]-u[i]
        y4[i]=my[i]
        x5[i]=mx[i]-u[i]
        y5[i]=my[i]-u[i]
        x6[i]=mx[i]
        y6[i]=my[i]-u[i]
        x7[i]=mx[i]+u[i]
        y7[i]=my[i]-u[i]
        
        if i>0:
            rectc(x0[i], y0[i], s*(r**p[i]),s*(r**p[i]))
            rectc(x1[i], y1[i], s*(r**p[i]),s*(r**p[i]))
            rectc(x2[i], y2[i], s*(r**p[i]),s*(r**p[i]))
            rectc(x3[i], y3[i], s*(r**p[i]),s*(r**p[i]))
            rectc(x4[i], y4[i], s*(r**p[i]),s*(r**p[i]))
            rectc(x5[i], y5[i], s*(r**p[i]),s*(r**p[i]))
            rectc(x6[i], y6[i], s*(r**p[i]),s*(r**p[i]))
            rectc(x7[i], y7[i], s*(r**p[i]),s*(r**p[i]))
                
def rectc(xc,yc,w,h):
    x=xc-w/2
    y=yc-h/2
    rect(x,y,w,h)

가시적인 결과는 원하던 대로 나왔으나, 뭔가... 깔끔한 코드가 아니라는 생각이 든다.

'Code > NodeBox' 카테고리의 다른 글

splash_class  (0) 2007.07.13
progress_class  (0) 2007.07.12
popping_1.0 (수정 중)  (0) 2007.07.06
popping_0.0  (0) 2007.07.06
storing_test  (0) 2007.07.05
posted by maetel
2007. 7. 6. 16:50 Code/NodeBox
mission: 마우스가 정지해 있을 때 popping 효과 제거하기

size(400,400)
speed(15)

def setup():
    global mx
    global my
    global t

    global x0
    global y0
    global x1
    global y1
    global x2
    global y2
    global x3
    global y3
    global x4
    global y4
    global x5
    global y5
    global x6
    global y6
    global x7
    global y7
    
    global d
    global s
    global r
    global u
    global p
    
    t=20 #term
    mx=range(t)
    my=range(t)

    for n in range(t+1):
        mx[n]=-WIDTH
        my[n]=-HEIGHT
         
    x0=range(t)
    y0=range(t)
    x1=range(t)
    y1=range(t)
    x2=range(t)
    y2=range(t)
    x3=range(t)
    y3=range(t)
    x4=range(t)
    y4=range(t)
    x5=range(t)
    y5=range(t)
    x6=range(t)
    y6=range(t)
    x7=range(t)
    y7=range(t)
    
    u=range(t)    
    p=range(t)
            
    d=20 #distance
    s=3 #initial size
    r=0.99 #ratio of size decreasing

    
def draw():
    global mx
    global my
    global t

    global x0
    global y0
    global x1
    global y1
    global x2
    global y2
    global x3
    global y3
    global x4
    global y4
    global x5
    global y5
    global x6
    global y6
    global x7
    global y7
    
    global d
    global s
    global r
    global u
    global p   
        
    background(0,0,1)    
    fill(1)
    
    for i in range(t-1):
        mx[i]=mx[i+1]
        my[i]=my[i+1]
        
        mx[t-1]=MOUSEX
        my[t-1]=MOUSEY

    rectc(mx[i],my[i],s,s)
        
    for i in range(t):        
        u[i]=(t-i)*2 ##to controll the stages of popping
        p[i]=-i ##to controll the sizes of popping


        if (i>=1) & ((mx[i]-mx[i-1])==0):
            u[i]=0
           
        x0[i]=mx[i]+u[i]
        y0[i]=my[i]
        x1[i]=mx[i]+u[i]
        y1[i]=my[i]+u[i]
        x2[i]=mx[i]
        y2[i]=my[i]+u[i]
        x3[i]=mx[i]-u[i]
        y3[i]=my[i]+u[i]
        x4[i]=mx[i]-u[i]
        y4[i]=my[i]
        x5[i]=mx[i]-u[i]
        y5[i]=my[i]-u[i]
        x6[i]=mx[i]
        y6[i]=my[i]-u[i]
        x7[i]=mx[i]+u[i]
        y7[i]=my[i]-u[i]
        
                
        rectc(x0[i], y0[i], s*(r**p[i]),s*(r**p[i]))
        rectc(x1[i], y1[i], s*(r**p[i]),s*(r**p[i]))
        rectc(x2[i], y2[i], s*(r**p[i]),s*(r**p[i]))
        rectc(x3[i], y3[i], s*(r**p[i]),s*(r**p[i]))
        rectc(x4[i], y4[i], s*(r**p[i]),s*(r**p[i]))
        rectc(x5[i], y5[i], s*(r**p[i]),s*(r**p[i]))
        rectc(x6[i], y6[i], s*(r**p[i]),s*(r**p[i]))
        rectc(x7[i], y7[i], s*(r**p[i]),s*(r**p[i]))
       

def rectc(xc,yc,w,h):
    x=xc-w/2
    y=yc-h/2
    rect(x,y,w,h)


range의 범위를 초과하지 않기 위한 삽입 조건 "i>=1"에 의해 i=1일 때의 상이 남는다.
현재, mx와 my등의 range를 t에서 t+1로 변경하여 적용하는 방법을 시도 중이다.

    mx=range(t+1)
    my=range(t+1)
       
    for i in range(t):
        mx[i-1]=mx[i]
        my[i-1]=my[i]
       
        mx[t]=MOUSEX
        my[t]=MOUSEY

    rectc(mx[i+1],my[i+1],s,s)

중략하고 아래와 같이...

        if (mx[i+1]-mx[i])==0:
            u[i]=0

그러자, 아예 전체적으로 popping 효과가 사라져 버린다. orz

'Code > NodeBox' 카테고리의 다른 글

progress_class  (0) 2007.07.12
popping_1.1  (0) 2007.07.09
popping_0.0  (0) 2007.07.06
storing_test  (0) 2007.07.05
pop_test  (0) 2007.07.05
posted by maetel
2007. 7. 6. 15:22 Code/NodeBox


size(400,400)
speed(15)

def setup():
    global mx
    global my
    global t

    global x0
    global y0
    global x1
    global y1
    global x2
    global y2
    global x3
    global y3
    global x4
    global y4
    global x5
    global y5
    global x6
    global y6
    global x7
    global y7
   
    global d
    global s
    global r
    global u
    global p
   
    t=20 #term
    mx=range(t)
    my=range(t)

    for n in range(t):
        mx[n]=-200
        my[n]=-200
       
    x0=range(t)
    y0=range(t)
    x1=range(t)
    y1=range(t)
    x2=range(t)
    y2=range(t)
    x3=range(t)
    y3=range(t)
    x4=range(t)
    y4=range(t)
    x5=range(t)
    y5=range(t)
    x6=range(t)
    y6=range(t)
    x7=range(t)
    y7=range(t)
   
    u=range(t)   
    p=range(t)
           
    d=10 #distance
    s=2 #initial size
    r=0.9 #ratio of size decreasing

   
def draw():
    global mx
    global my
    global t

    global x0
    global y0
    global x1
    global y1
    global x2
    global y2
    global x3
    global y3
    global x4
    global y4
    global x5
    global y5
    global x6
    global y6
    global x7
    global y7
   
    global d
    global s
    global r
    global u
    global p  
       
    for i in range(t-1):
        mx[i]=mx[i+1]
        my[i]=my[i+1]
       
        mx[t-1]=MOUSEX
        my[t-1]=MOUSEY

    rectc(mx[i],my[i],s,s)
   
    for i in range(t):       
        u[i]=(t-i)*5 #to controll the stages of popping
        p[i]=-i #to controll the sizes of popping
       
        x0[i]=mx[i]+u[i]
        y0[i]=my[i]
        x1[i]=mx[i]+u[i]
        y1[i]=my[i]+u[i]
        x2[i]=mx[i]
        y2[i]=my[i]+u[i]
        x3[i]=mx[i]-u[i]
        y3[i]=my[i]+u[i]
        x4[i]=mx[i]-u[i]
        y4[i]=my[i]
        x5[i]=mx[i]-u[i]
        y5[i]=my[i]-u[i]
        x6[i]=mx[i]
        y6[i]=my[i]-u[i]
        x7[i]=mx[i]+u[i]
        y7[i]=my[i]-u[i]
               
        rectc(x0[i], y0[i], s*(r**p[i]),s*(r**p[i]))
        rectc(x1[i], y1[i], s*(r**p[i]),s*(r**p[i]))
        rectc(x2[i], y2[i], s*(r**p[i]),s*(r**p[i]))
        rectc(x3[i], y3[i], s*(r**p[i]),s*(r**p[i]))
        rectc(x4[i], y4[i], s*(r**p[i]),s*(r**p[i]))
        rectc(x5[i], y5[i], s*(r**p[i]),s*(r**p[i]))
        rectc(x6[i], y6[i], s*(r**p[i]),s*(r**p[i]))
        rectc(x7[i], y7[i], s*(r**p[i]),s*(r**p[i]))
       
       
def rectc(xc,yc,w,h):
    x=xc-w/2
    y=yc-h/2
    rect(x,y,w,h)

## lesson 1. 마우스의 궤적을 따라 퍼져나가는 사각형들의 배열을 구성해 주는 range u변수는 전체 인덱스와 동일한 i에 관하여 정의되어야 한다.
##  또한, 새로운 자취일수록 그 배열의 진행 단계가 나중이어야, 즉 덜 퍼진 상태여야 하므로, "t(=range의 범위)-i"가 된다.
## 그러므로, 물론 사각형 배열의 크기를 (작아지게) 조절해 주는 range 변수 p도 i에 관하여 다시 정의해 주어야 한다.
  

'Code > NodeBox' 카테고리의 다른 글

popping_1.1  (0) 2007.07.09
popping_1.0 (수정 중)  (0) 2007.07.06
storing_test  (0) 2007.07.05
pop_test  (0) 2007.07.05
matrix_glimmer_dim  (0) 2007.07.04
posted by maetel
2007. 7. 5. 16:54 Code/NodeBox
프로세싱 홈피에서 다음 예제를 발견!

Storing Input.
Move the mouse across the screen to change the position of the circles. The positions of the mouse are recorded into an array and played back every frame. Between each frame, the newest value are added to the end of each array and the oldest value is deleted. (Updated 21 August 2002)

이렇게 적용!

size(200,200)
speed(100)

def setup():
    global x
    global y
    global t
    
    t=10
    x=range(t)
    y=range(t)  
   
    for t in range(10):
        x[t]=-100
        y[t]=-100
      
    
def draw():
    global x
    global y
    global t

    for i in range(t-1):
        x[i]=x[i+1]
        y[i]=y[i+1]
        
        x[t-1]=MOUSEX
        y[t-1]=MOUSEY
    
    for i in range(t):    
        rect(x[i],y[i],10,10)


#x, y range의 초기값이 자동으로 설정되어 있어서 발생하는 원치않은 이미지를 size 밖으로 빼준다.


'Code > NodeBox' 카테고리의 다른 글

popping_1.0 (수정 중)  (0) 2007.07.06
popping_0.0  (0) 2007.07.06
pop_test  (0) 2007.07.05
matrix_glimmer_dim  (0) 2007.07.04
matrix-glimmer  (0) 2007.07.04
posted by maetel
2007. 7. 5. 14:28 Code/NodeBox

size(400,400)
speed(15)

def setup():
    global mx
    global my
    global x
    global y
    global n
    global d
    global f
    global s
    global r
   
    d=10 #distance
    s=15 #initial size
    r=0.8 #ratio of size decreasing
    f=0 #frame
    n=range(8)
    x=range(8)
    y=range(8)

def draw():
    global mx
    global my
    global x
    global y
    global n
    global d
    global f
    global s
    global r   

    mx=MOUSEX
    my=MOUSEY

    rect(mx,my,s,s)

    f+=1
   
    x[0]=mx+f*d
    y[0]=my
    x[1]=mx+f*d
    y[1]=my+f*d
    x[2]=mx
    y[2]=my+f*d
    x[3]=mx-f*d
    y[3]=my+f*d
    x[4]=mx-f*d
    y[4]=my
    x[5]=mx-f*d
    y[5]=my-f*d
    x[6]=mx
    y[6]=my-f*d
    x[7]=mx+f*d
    y[7]=my-f*d
   
    for n in range(8):
        rect(x[n],y[n],s*(r**f),s*(r**f))
 
    if f>10: #term
        f=1

 
so far, easy.

'Code > NodeBox' 카테고리의 다른 글

popping_0.0  (0) 2007.07.06
storing_test  (0) 2007.07.05
matrix_glimmer_dim  (0) 2007.07.04
matrix-glimmer  (0) 2007.07.04
matrix_color_chain  (0) 2007.06.27
posted by maetel
2007. 7. 4. 17:44 Code/NodeBox


size(200,200)
speed(100)

def setup():
    global x
    global y
    global c
    global n
    global m
    
    c=range(16) #color of a cell
    n=range(16) #cell number
    m=range(16) #maximum color of a cell
    
    for n in range(16):
        c[n]=0

def draw():
    global x
    global y
    global c
    global n
    global m
    
    x=MOUSEX
    y=MOUSEY
    s=50  #size of a cell
    b=1 #blue    

    
    for n in range(16):
        c[n]-=0.1
        
    for h in range(4):
        for w in range(4):
            n=h*4+w
         
            if ((x>=w*s)&(x<=(w+1)*s)) & ((y>=h*s)&(y<=(h+1)*s)):
                c[n]=1
                m[n]=1
            
            if (n<=11) & (c[n]>0) & (c[n]<=0.1):
                c[n+4]=m[n]*0.6
                m[n+4]=m[n]*0.6
                                       
            stroke(1)
            fill(c[n],c[n],b)
            rect(w*s,h*s,s,s)
                

### 각 셀의 최대(색상)값을 보유하기 위해 새로운 range 변수를 쓴다.

           

'Code > NodeBox' 카테고리의 다른 글

storing_test  (0) 2007.07.05
pop_test  (0) 2007.07.05
matrix-glimmer  (0) 2007.07.04
matrix_color_chain  (0) 2007.06.27
matrix-color change  (0) 2007.06.27
posted by maetel
2007. 7. 4. 16:30 Code/NodeBox


size(200,200)
speed(100)

def setup():
    global x
    global y
    global c
    global n

    c=range(16) #color of a cell
    n=range(16) #cell number
    
    for n in range(16):
        c[n]=0

def draw():
    global x
    global y
    global c
    global n

    x=MOUSEX
    y=MOUSEY
    s=50  #size of a cell
    b=1 #blue    

    
    for n in range(16):
        c[n]-=0.1
        
    for h in range(4):
        for w in range(4):
            n=h*4+w
            
            if ((x>=w*s)&(x<=(w+1)*s)) & ((y>=h*s)&(y<=(h+1)*s)):
                #if mousedown:
                c[n]=1

            if (n<=11) & (c[n]>0) & (c[n]<=0.1):
                c[n+4]=1                     

            stroke(1)
            fill(c[n],c[n],b)
            rect(w*s,h*s,s,s)

             

'Code > NodeBox' 카테고리의 다른 글

pop_test  (0) 2007.07.05
matrix_glimmer_dim  (0) 2007.07.04
matrix_color_chain  (0) 2007.06.27
matrix-color change  (0) 2007.06.27
matrix  (0) 2007.06.27
posted by maetel
2007. 6. 27. 18:54 Code/NodeBox


size(200,200)
speed(100)

def setup():
    global x
    global y
    global f
    global c
    global n
    global f
   
    f=0
    n=0
       
    c=range(16) #color of a cell
    n=range(16)
    for cell in range(16): #cell number
        c[cell]=1
        n[cell]=0
       
def draw():
    global x
    global y
    global f
    global c
    global n

    f+=1 #frame
    x=MOUSEX
    y=MOUSEY   
    s=50 #size of a cell   
    b=1 #blue
 
 
    for cell in range(16):
        c[cell]-=0.05-0.01*n[cell]
#        c[cell]+=0.027*n[cell]
   
    for h in range(4):
        for w in range(4):
            cell=h*4+w #cell number
            pl=0
            pr=0
            pt=0
            pb=0
            if ((x>=w*s)&(x<=(w+1)*s)) & ((y>=h*s)&(y<=(h+1)*s)):
                c[cell]=1

            if w>0:
                if (c[cell]<c[cell-1]):
                    pl=1       
            if w<3:   
                if (c[cell]<c[cell+1]):
                    pr=1
            if h>0:
                if (c[cell]<c[cell-4]):
                    pt=1
            if h<3:
                if (c[cell]<c[cell+4]):
                    pb=1                                      
           
            n[cell]=pl+pr+pt+pb
           
            fill(c[cell],c[cell],b)
            stroke(1)
            rect(w*s,h*s,s,s)
           
           
## lesson 1. range 변수의 매개변수들은 모두 range여야 한다.

           

'Code > NodeBox' 카테고리의 다른 글

matrix_glimmer_dim  (0) 2007.07.04
matrix-glimmer  (0) 2007.07.04
matrix-color change  (0) 2007.06.27
matrix  (0) 2007.06.27
arrive  (0) 2007.06.25
posted by maetel
2007. 6. 27. 17:09 Code/NodeBox


size(200,200)
speed(100)

def setup():
    global x
    global y
    global f
    global c

    f=0
    
    c=range(16) #color value
    for cell in range(16): #cell number
        c[cell]=1
    
def draw():
    global x
    global y
    global f
    global c
    
    f+=1 #frame
    x=MOUSEX
    y=MOUSEY    
    s=50 #size of a cell    
    b=1 #blue
 
    for cell in range(16):
        c[cell]-=0.05
    
    for h in range(4):
        for w in range(4):
            cell=h*4+w #cell number (defined again for visual construction)
            if ((x>=w*s)&(x<=(w+1)*s)) & ((y>=h*s)&(y<=(h+1)*s)):
                c[cell]=1
                
            fill(c[cell],c[cell],b)
            stroke(1)
            rect(w*s,h*s,s,s)
 
## my miss - range를 매개변수로서의 용도로만 한정했다.
## lesson - range는 array의 역할을 할 뿐 아니라 그 자체로 값을 가질 수 있다.

'Code > NodeBox' 카테고리의 다른 글

matrix-glimmer  (0) 2007.07.04
matrix_color_chain  (0) 2007.06.27
matrix  (0) 2007.06.27
arrive  (0) 2007.06.25
steering  (0) 2007.06.25
posted by maetel
2007. 6. 27. 11:46 Code/NodeBox


size(200,200)
speed(100)

def setup():
    global x
    global y

    
def draw():
    global x
    global y
    
    s=50
    x=MOUSEX
    y=MOUSEY
   
    for h in range(4):
        for w in range(4):
            if ((x>=w*s)&(x<=(w+1)*s)) & ((y>=h*s)&(y<=(h+1)*s)):
                r=1
                g=1
                b=1
             
            else:
                r=0
                g=0
                b=1
                    
            stroke(1)
            fill(r,g,b)
            rect(w*s,h*s,s,s)
    
    rotate(135)
    arrow(x,y,20)

so far, a piece of cake~          

'Code > NodeBox' 카테고리의 다른 글

matrix_color_chain  (0) 2007.06.27
matrix-color change  (0) 2007.06.27
arrive  (0) 2007.06.25
steering  (0) 2007.06.25
ping_orbit  (0) 2007.06.20
posted by maetel
2007. 6. 25. 18:50 Code/NodeBox
사용자 삽입 이미지
flock

class Boid:
    def __init__(self,x,y,vx,vy):
        self.x=x
        self.y=y
        self.vx=vx
        self.vy=vy
        self.tx=0
        self.ty=0
        
    def target(self,tx,ty):
        self.tx=tx
        self.ty=ty
#        oval(tx,ty,30,30)
        
    def chase(self):
        dx=self.tx-self.x
        dy=self.ty-self.y
        sx=dx-self.vx
        sy=dy-self.vy   
        ax=0
        ay=0
        ax+=sx/random(10,100)
        ay+=sy/random(10,100)
        self.vx+=ax
        self.vy+=ay
        self.x+=self.vx
        self.y+=self.vy
        
        r=10
        maxs=10

        if (dx==0) & (dy==0):
            self.vx=0
            self.vy=0
            
        if (dx>-r) & (dy<r):
            self.vx/=2
            self.vy/=2

        if (self.vx>maxs):
            self.vx=maxs
        if (self.vx<-maxs):
            self.vx=-maxs
        if (self.vy>maxs):
            self.vy=maxs
        if (self.vy<-maxs):
            self.vy=-maxs
            
    def render(self):
        fill(0.5)
        ovalc(self.x, self.y, 10, 10)
            
    #def update(self,ax,ay):
    #    self.ax=0
    #    self.ay=0    
    def boarders(self):
        r=10
        if (self.x<-r):
            self.x=WIDTH+r
        if (self.y<-r):
            self.y=HEIGHT+r
        if (self.x>WIDTH+r):
            self.x=-r
        if (self.y>HEIGHT+r):
            self.y=-r
            
def ovalc(x,y,w,h):
    x=x-w/2
    y=y-h/2
    oval(x,y,w,h)            
        
size(300,300)
speed(100)

def setup():
    global b
    b=range(20)
    
    for i in range(20):
        b[i]=Boid(i*random(100), i*random(100), i*random(10), i*random(10))
        
def draw():
    global b

    fill(0)
    ovalc(MOUSEX,MOUSEY,30,30)
    
    for i in b:
        i.target(MOUSEX,MOUSEY)
        i.chase()
        i.boarders()
        i.render()

'Code > NodeBox' 카테고리의 다른 글

matrix-color change  (0) 2007.06.27
matrix  (0) 2007.06.27
steering  (0) 2007.06.25
ping_orbit  (0) 2007.06.20
orbit  (0) 2007.06.20
posted by maetel
2007. 6. 25. 17:54 Code/NodeBox


###steering
###http://www.red3d.com/cwr/steer/

size(300,300)
speed(100)

def setup():
    global x
    global y
    global x1
    global y1
    global vx1
    global vy1
    global x2
    global y2
    global vx2
    global vy2
      
    x1=WIDTH/2-10
    y1=HEIGHT/2-10
    x2=WIDTH/2+10
    y2=HEIGHT/2+10
    vx1=random(5)
    vy1=random(5)
    vx2=random(5)
    vy2=random(5)

        
def draw():    
    global x
    global y
    global x1
    global y1
    global vx1
    global vy1
    global x2
    global y2
    global vx2
    global vy2
    
    #target    
    x=MOUSEX
    y=MOUSEY
    ovalc(x,y,30,30)
    
    maxs=10
#    maxf=0.1
    r=10
    
    #reset
    ax1=0
    ay1=0
    ax2=0
    ay2=0
    
    #direction d=t-p
    dx1=x-x1
    dy1=y-y1
    dx2=x-x2
    dy2=y-y2
    
    
 
    if (dx1==0) & (dy1==0):
        vx1=0
        vy1=0
        
    if (dx2==0) & (dy2==0):
        vx2=maxs
        vy2=maxs
        
    if (dx1>-r) & (dx1<r):
        vx1/=2
        
    if (dy1>-r) & (dy1<r):
        vy1/=2
        
    #if(dx2>-r) & (dx2<r):
    #    vx2=maxs/10
        
    #if(dy2>-r) & (dy2<r):
    #    vy2=maxs/10
    
# maximum speed limitation  
    if (vx1>maxs):
        vx1=maxs
    if (vx1<-maxs):
        vx1=-maxs
    if (vy1>maxs):
        vy1=maxs
    if(vy1<-maxs):
        vy1=-maxs
        
    if (vx2>maxs):
        vx2=maxs
    if (vx2<-maxs):
        vx2=-maxs
    if (vy2>maxs):
        vy2=maxs    
    if (vy2<-maxs):
        vy2=-maxs   
    
        
# steering force: s=d-v=(t-p)-v    
    sx1=dx1-vx1
    sy1=dy1-vy1
    sx2=dx2-vx2
    sy2=dy2-vy2
    
# seek    
    ax1+=sx1/100
    ay1+=sy1/100
    ax2+=sx2/100
    ay2+=sy2/100
    
    vx1+=ax1
    vy1+=ay1  
    vx2+=ax2
    vy2+=ay2

    
    x1+=vx1
    y1+=vy1
    x2+=vx2
    y2+=vy2
    
# at boarders     
    if (x2<-r):
         x2=WIDTH+r
         
    if (y2<-r):
        y2=HEIGHT+r
        
    if (x2>WIDTH+r):
        x2=-r
        
    if (y2>HEIGHT+r):
        y2=-r

    
    stroke(0)
    fill(1)   
    ovalc(x1, y1, r, r)
#    line(x1, y1, x1+sx1, y1+sy1)
    fill(0.5)
    ovalc(x2, y2, r, r)
#    line(x2, y2, x2+sx2, y2+sy2)
    

 
def ovalc(x,y,w,h):
    x=x-w/2
    y=y-h/2
    oval(x,y,w,h)
    

'Code > NodeBox' 카테고리의 다른 글

matrix  (0) 2007.06.27
arrive  (0) 2007.06.25
ping_orbit  (0) 2007.06.20
orbit  (0) 2007.06.20
particle(rect+oval)_class  (0) 2007.06.20
posted by maetel
2007. 6. 20. 19:10 Code/NodeBox


from math import cos, sin, radians

class Particle:
    def __init__(self, x, y, vx, vy,a):
        self.x=x
        self.y=y
        self.vx=vx
        self.vy=vy
        self.a=a
             
    def dynamics(self):
        c=0.001
        r=1.1
        
        ax=-self.vx*c   
        ay=-self.vy*c
        self.vx+=ax
        self.vy+=ay
        self.x+=self.vx
        self.y+=self.vy
                
        if (self.x>=WIDTH):
            self.vx*=-r
            self.x=WIDTH
            
        if (self.x<=0):
            self.vx*=-r
            self.x=0
            
        if (self.y>=HEIGHT):
            self.vy*=-r
            self.y=HEIGHT
            
        if (self.y<=0):
            self.vy*=-r
            self.y=0
            
    def oval(self):
            self.a+=10        
            
            oval(self.x-30+100*cos(radians(self.a)), self.y+30-100*sin(radians(self.a)), 20, 20)

    def rect(self):
        rect(self.x-30, self.y+30, 30, 30)
                 
        
size(300,300)
speed(100)    
    
def setup():
    global ping
    
    ping=range(5)
    
    for i in range(5):
        ping[i]=Particle(i*20+150, i*20+150, i*5+2, i*5+1, 0)

        
def draw():
    global ping
                
    for i in ping:
        i.dynamics()
        i.oval()
        i.rect()

'Code > NodeBox' 카테고리의 다른 글

arrive  (0) 2007.06.25
steering  (0) 2007.06.25
orbit  (0) 2007.06.20
particle(rect+oval)_class  (0) 2007.06.20
ping_class  (0) 2007.06.20
posted by maetel