第0章 随机游走——《processing》学习,自己完成实验的代码

2023-10-20 16:58

本文主要是介绍第0章 随机游走——《processing》学习,自己完成实验的代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.第一个的尝试:
在这里插入图片描述
完整代码如下:

//主函数代码
Walker[] w;int total = 0;void setup() {frameRate(30);size(600, 400);w = new Walker[20];for (int i = 0; i < w.length; i++) {w[i] = new Walker();}
}void draw() {background(255);int o = int(map(mouseX,mouseY,width,1,8));//int oy = int(map(mouseY,mouseX,width,1,8));//八音度调节noiseDetail(o,0.3);//调整由Perlin噪声功能产生的细节的特征和水平。// noiseDetail(oy,0.3);if (frameCount % 30 == 0) {total = total + 1;if (total > w.length-1) {total = w.length-1;}}for (int i = 0; i < total; i++) {w[i].walk();w[i].display();}
}//调用的Walker类
class Walker {PVector position;PVector noff;Walker() {position = new PVector(width/2, height/2);noff = new PVector(random(1000),random(1000));}void display() {//frameRate(10);strokeWeight(2);int r = int(random(255));int g = int(random(255));int b = int(random(255));// int a = int(random(255));//fill(127);fill(r,g,b);stroke(0);position.x=constrain(position.x,mouseX,width);position.y=constrain(position.y,mouseY,height);ellipse(position.x, position.y, 48, 48);}// Randomly move up, down, left, right, or stay in one placevoid walk() {position.x = map(noise(noff.x),0,1,0,width);position.y = map(noise(noff.y),0,1,0,height);noff.x += 0.01;noff.y += 0.01;}
}

2.第二种尝试:
在这里插入图片描述

//主函数
void setup() {size(640,360);// Create a walker objectw = new Walker[10];for (int i = 0; i < w.length; i++) {w[i] = new Walker();}//w = new Walker();background(255);
}void draw() {if (frameCount % 30 == 0) {total = total + 1;if (total > w.length-1) {total = w.length-1;}}for (int i = 0; i < total; i++) {w[i].step();w[i].render();}
}//调用的Walker类
class Walker {int x, y;Walker() {x = width/2;y = height/2;}void render() {
//设置颜色float r = randomGaussian();float g = randomGaussian();float b = randomGaussian();float sd = 100; float mean = 100;r = constrain((r * sd) + mean,0,255);//repeat for g & bsd = 20; mean = 200;g = constrain((g * sd) + mean,0,255);sd = 50; mean = 0;b = constrain((b * sd) + mean,0,255);//get more gaussian numbers, this time for positionfloat xloc = randomGaussian();float yloc = randomGaussian();sd = width/10;mean = width/2;xloc = ( xloc * sd ) + mean;yloc = ( yloc * sd ) + mean;
////* int r = int(random(255));int g = int(random(255));int b = int(random(255));//int a = int(random(255));*/stroke(255);strokeWeight(10);point(x, y);noFill();stroke(123,g,255);strokeWeight(0.5);ellipse(x, y, r, r);/* stroke(r,120,255);rectMode(CENTER);rect(x-9, y-9, r/2, r/2);//正方形小块的运动*/}// Randomly move up, down, left, right, or stay in one placevoid step() {float r = random(1);// A 50% of moving towards the mouseif (r < 0.5) {    int xdir = (mouseX-x);int ydir = (mouseY-y);if (xdir != 0) {xdir /= abs(xdir);} if (ydir != 0) {ydir /= abs(ydir);}x += xdir;y += ydir;} else {int xdir = int(random(-2, 2));int ydir = int(random(-2, 2));println(xdir);x += xdir;y += ydir;}x = constrain(x, 0, width-1);y = constrain(y, 0, height-1);}
}

加上这一段就会出现rect,代码的稍微改动就会出现不一样的景象:
在这里插入图片描述
在这里插入图片描述

这篇关于第0章 随机游走——《processing》学习,自己完成实验的代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/248430

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代