第七章 运动
1.循环运动
//int radius = 40;
//float x = 110;
//float speed = 0.5;
//int direction = 1;
//void setup() {
// size(240, 120);
// smooth();
// ellipseMode(RADIUS);//第二三参数为半径
//}
//void draw() {
// background(0);
// x += speed * direction;
// if ((x > width - radius) || (x < radius)) {
// direction = -direction;
// }
// if (direction == 1) {
// arc(x, 60, radius, radius, 0.52, 5.76);
// } else {
// arc(x, 60, radius, radius, 3.67, 8.9);
// }
//}
2.随机
float speed = 9;
//int diameter = 20;
//float x;
//float y;
//void setup() {
// size(240, 120);
// smooth();
// x = width/2;
// y = height/2;
//}
//void draw() {
// x += random(-speed, speed);
// y += random(-speed, speed);
// x = constrain(x, 0, width);
// y = constrain(y, 0, height);
// ellipse(x, y, diameter, diameter);
//}
3.计时器
//millis(); 返回自启动程序以来的毫秒数 (千分之一秒)。此信息通常用于计时事件和动画序列。
int time1 = 2000;
int time2 = 4000;
float x = 0;
void setup() {
size(480, 120);
smooth();
}
void draw() {
int currentTime = millis();
background(204);
if (currentTime > time2) { //时间长的放在前边先判断
x-= 0.5;
} else if (currentTime >time1) {
x += 2;
}
ellipse(x, 60, 90, 90);
}
4.圆环
对于任意角度,sin(),cos()的返回值在 -1~1之间
//float angle = 0.0;
//float offset = 60;
//float scalar = 30;
//float speed = 0.1;
//void setup(){
//size(120,120);
//smooth();
//}
//void draw(){
background(0);
//float x = offset + cos(angle) * scalar; //顺时针
//float y = offset + sin(angle) * scalar;
println( x);
println( y);
//println(x-y);
float x = offset + sin(angle) * scalar; //逆时针
float y = offset + cos(angle) * scalar;
//ellipse(x,y,40,40);
//angle += speed;
//}
5.平移旋转缩放
5.1平移
//void setup(){
//size(400,400);
//}
//void draw(){
//translate(mouseX,mouseY);//将屏幕(0,0)坐标移动到鼠标位置
//rect(0,0,30,30);
//translate(35,10);
//rect(0,0,15,15);//更小的矩形被平移到(mouseX+35,mouseY+10)
//}
//7.18
//void setup() {
// size(400, 400);
//}
//void draw() {
// /*
//pushMatrix()函数运行时,将会存储一份当前坐标系统拷贝
//然后在popMatrix调用后恢复
//pushMatrix();和popMatrix();之间的坐标系变换是无效的
//两个函数应该成对出现
// */
// pushMatrix();
// translate(mouseX, mouseY);//将屏幕(0,0)坐标移动到鼠标位置
// rect(0, 0, 30, 30);
// popMatrix();
// translate(35, 10);
// rect(0, 0, 15, 15);//更小的矩形被平移到(mouseX+35,mouseY+10)
//}
5.2旋转
/float angle = 0.0;
//float x = 0.0;
//float y = 0.0;
//void setup() {
// size(600, 600);
// smooth();
//}
//void draw() {
// translate(x, y);
// rotate(angle);
// rect(-30, -30, 60, 60);
// angle += 0.1;
// x += random(50);
// y += random(50);
// x = constrain(x, 0, width);
// y = constrain(y, 0, height);
//}
5.3缩放
//float angle = 0.0;
//void setup(){
//size(400,400);
//}
//void draw(){
//translate(mouseX,mouseY);//将屏幕(0,0)坐标移动到鼠标位置
//float scalar = sin(angle) + 2;
//scale(scalar);
strokeWeight(1.0/scalar);
//rect(-15,-15,30,30);//更小的矩形被平移到(mouseX+35,mouseY+10)
//angle += 0.1;
//}
5.4综合实验
//7.22
float angle = 0.0;
float angleDirection = 1;
float speed = 0.005;
void setup(){
size(400,400);
smooth();
}
void draw(){
background(204);
translate(200,200); //移动到开始位置
rotate(angle);
strokeWeight(12);
line(0,0,40,0);
translate(40,0); //移动到下个关节
rotate(angle * 2.0);
strokeWeight(6);
line(0,0,30,0);
translate(30,0); //移动到下个关节
rotate(angle * 2.5);
strokeWeight(3);
line(0,0,20,0);
angle += speed * angleDirection;
if((angle > QUARTER_PI) || (angle < 0)){
angleDirection *= -1;
}
}