728x90
반응형
constrain()
value가 가질 수 있는 값을 제한해주는 함수이다.
constrain(변수,min,max) 순으로 쓴다.
예를 들어 r,g,b 값을 제한해주고 싶으면
r=constrain(r,0,255); --> 이런식으로 쓰면 된다.
Rollovers
칸이 네칸으로 나눠져 있고 네 칸 중 한 칸에 마우스를 가져갔을 때 검은색으로 사각형이 변하게 만들어볼 것이다.
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
|
float r,g,b=0;
void setup(){
size(480,270);
}
void draw(){
background(255);
line(width/2,0,width/2,height);
line(0,height/2,width,height/2);
int x=width/2;
int y=height/2;
if(mouseX<x&&mouseY<y){
fill(0);
rect(0,0,x,y);
}
else if(mouseX>x&&mouseY<y){
fill(0);
rect(x,0,x,y);
}
else if(mouseX<x&&mouseY>y){
fill(0);
rect(0,y,x,y);
}else if(mouseX>x&&mouseY>y){
fill(0);
rect(x,y,x,y);
}
}
|
cs |
가장자리를 돌아댕기는 사각형
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
|
int x,y=0;
int speed=5;
int state=0;
void setup(){
size(480,270);
}
void draw(){
background(0);
fill(128);
stroke(255);
rect(x,y,9,9);
if(state==0){
x+=speed;
if(x>width-9){
x=width-10;
state=1;
}
}else if(state==1){
y+=speed;
if(y>height-9){
y=height-10;
state=2;
}
}else if(state==2){
x-=speed;
if(x<9){
x=10;
state=3;
}
}
else if(state==3){
y-=speed;
if(y<9){
y=10;
state=0;
}
}
}
|
cs |
gravity
사각형이 바닥으로 떨어진다음에 튕기는 효과를 내 볼 것이다.
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
|
int x=240;
int y=0;
float speed=0;
float gravity=0.1;
void setup(){
size(480,270);
}
void draw(){
background(255);
fill(128);
rect(x,y,10,10);
y+=speed;
speed+=gravity;
if(y>height){
speed*=-0.75;
}
}
|
cs |
우선 speed에 중력인 0.1을 계속 더해주고 만일 y 값이 캔버스크기보다 커진다면
방향을 바꿔줘서 speed*=-0.75 정도 해주는게 좋다. 이 이상으로 수를 설정해주면
캔버스 크기 밖으로 도형이 나간다. (너무 당연한거지만 당연히 마이너스 붙여줘야 한다!)
728x90
반응형
'processing' 카테고리의 다른 글
[프로세싱 시험대비] Lecture08_transform (0) | 2022.06.04 |
---|---|
[Processing] Chapter 7_function/ Chapter8_Object (0) | 2022.03.13 |
[Processing] Chapter4_variables (0) | 2022.03.12 |
[Processing] bounce ball (0) | 2022.03.10 |
[processing] Chapter3_Interaction (0) | 2022.03.10 |
댓글