(九)利用processing展示简单函数的使用
来源:互联网 发布:davinci监控软件 编辑:程序博客网 时间:2024/04/27 21:09
随机绘制直线:
void setup(){ size(480, 120); smooth(); frameRate(2);}void draw(){ background(204); for(int x = 20; x < width; x+=20){ float mx = mouseX/10; float offsetA = random(-mx, mx); float offsetB = random(-mx, mx); line(x+offsetA, 20, x-offsetB, 100); } saveFrame("Shape.png");}程序运行结果:
随机移动图像:
float speedX = 10.0;float speedY = 2.5;int diameter = 20;float x;float y;void setup(){ size(640, 120); smooth(); x = width/2; y = height/2; randomSeed(20);}void draw(){ x += random(-speedX, speedX); y += random(-speedY, speedY); x = constrain(x, 0, width); y = constrain(y, 0, height); ellipse(x, y, diameter, diameter);}程序运行结果:
函数使用说明:
randomSeed(Auto) //设置种子数, Auto 相同的话生成的种子数相同,随机数也就是相同的random(-x, x) //在-x 到 x 之间随机成生一个数字constrain(x, 0, width) //将x 的值设置在(0, width)这范围中时光流逝,计时器 timer:
//通过计时器实现一个圆形的向左向右移动int timer1 = 2000;int timer2 = 4000;float x;void setup(){ size(640, 120); smooth(); x = width/2;}void draw(){ int currentTime = millis(); background(204); if(currentTime > timer2){ x -= 0.5; }else if(currentTime > timer1){ x += 0.5; } ellipse(x, 60, 90, 90);}程序运行结果:
函数使用说明:
millis() //返回当前程序的运行时间,单位为毫秒
实现sin函数的图形化显示:
float angle = 0.0;float x;float y;float stepX = 0.2;int rate = 0;void display_XY(){ line(40, 40, 40, 320); line(40, height/2, width-100, height/2); text("Y", 35, 35); line(40, 40, 35, 45); line(40, 40, 45, 45); text("O", 30, height/2); text("X", 560, height/2); line(width-105, height/2-5,width-100, height/2); line(width-105, height/2+5, width-100, height/2); x = 40; y = height/2; strokeWeight(4); point(x, y); strokeWeight(1);}void setup(){ size(640, 360); smooth(); background(204); frameRate(120); display_XY();}void draw(){ rate++; if(rate == 60){ rate = 0; } angle += 0.5; y = sin(radians(angle))*100+height/2; x += stepX; point(x, y); if(y>-0.01+height/2 && y < 0.01 + height/2){ noStroke(); fill(255, 255, 0); ellipse(x, y, 10, 10); noFill(); stroke(0); } if(x >= 540){ saveFrame("Shape.png"); noLoop(); //exit(); }}程序运行结果:
函数说明:
sin(float) //读入弧度值,函数结果 位于 -1,1之间利用sin()值做x, cos()值做y, 绘制小螺旋:
float angle = 0.0;float offset = 60;float scalar = 2;float speed = 0.05;void setup(){ size(120, 120); fill(0); smooth();}void draw(){ float x = offset + cos(angle) * scalar; float y = offset + sin(angle) * scalar; ellipse(x, y, 2, 2); angle += speed; scalar += speed;}
程序运行结果:
图形的平移,旋转和缩放:
float angle = 0.0;float angleDirection = 1;float speed = 0.005;void setup(){ size(200, 200); smooth();}void draw(){ background(204); translate(100, 100); 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 * 3.0); strokeWeight(3); line(0, 0, 20, 0); angle += speed*angleDirection; if((angle > QUARTER_PI) || (angle < 0)){ angleDirection *= -1; } saveFrame("Shape.png");}程序运行结果:
函数使用说明:
translate(x, y) //设置x, y为新的 0,0点rotate(angle) //将坐标系统进行旋转,angle为旋转的角度,旋转是绕着原点进行的scale(num) //num 为缩放比例,同时笔画的粗细也会进行缩放,strokeWeight(1.0/num)修改笔画的权重nf(i, 4) //nf(1, 4)指(0001), nf(11, 4)指(0011)
0 0
- (九)利用processing展示简单函数的使用
- processing的入门函数
- (九)BaseModel的简单使用
- (三)利用processing绘制简单规律图像
- Processing使用的例子
- Processing 简单的应用格式
- pagerslidingtabstrip的简单展示
- Pagerslidingtabstrip的简单展示
- (六)利用processing实现图案的绘制
- LIST-PROCESSING命令的使用
- Android之使用ViewPager实现图片展示(最简单的)
- 使用HorizontalScrollView和ImageView编写简单的图片展示程序
- 使用ListView实现一个简单的学生信息展示效果
- Tablayout的简单使用-单文字+图片文字展示效果
- ListView的简单使用《一》—>普通图文展示案例
- Mac下使用python实现简单的目录树展示
- 利用JMF实现简单的音频播放器(展示歌词)
- 基于processing的简单音频可视化
- 阿里云服务器Web项目配置发布全过程
- 初识J2EE
- [土狗之路]coursera C语言进阶习题 二维数组右上左下遍历
- UML用例图详解一
- 4.8.4 如何去掉Android 应用的actionbar
- (九)利用processing展示简单函数的使用
- 知人知心-DISC的理解
- 关于游戏架构设计的一些整理吧
- 当键盘出现的时候,如何让UITextField自动上移
- CSS3中如何使元素曲线运动
- 构造函数constructor
- 非原创,jquery.pagination 分页插件
- 【poj3233】Matrix Power Series——矩阵快速幂
- jquery.fileupload.js插件使用--单视频或音频上传预览