第3章 振荡——《processing》学习,自己完成实验的代码

2023-10-20 16:59

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

第四个实验的完成结果为:
在这里插入图片描述
主函数显示为:

Spaceship ship;
Bob b1;
Bob b2;
Bob b3;Spring s1;
Spring s2;
Spring s3;newmover[] movers = new newmover[30];
Attractor a;float startAngle = 0;
float angleVel = 0.23;void setup() {size(840, 560);ship = new Spaceship();b1 = new Bob(width/5, 200);b2 = new Bob(width/6, 300);b3 = new Bob(width/4, 500);s1 = new Spring(b1,b2,100);s2 = new Spring(b2,b3,100);s3 = new Spring(b1,b3,100);/*attractor*/for (int i = 0; i < movers.length; i++) {movers[i] = new newmover(random(0.1,2),random(width),random(height)); }a = new Attractor();
}void draw() {background(39,64,139); /**wave**/startAngle += 0.035;float angle = startAngle;for (int x = 0; x <= width; x += 24) {float y = map(sin(angle),-1,1,height*4/5,height);noStroke();fill(238,233,233,120);ellipse(x,y,30,30);angle += angleVel;} for (int x = 0; x <= width; x +=24) {float y = map(sin(angle),-1,1,height*4/5,height);noStroke();fill(238,233,233,120);ellipse(x,y,30,30);angle += angleVel;} /*拉扯星球*/s1.update();s2.update();s3.update();s1.display();s2.display();s3.display();b1.update();b1.display2();b2.update();b2.display();b3.update();b3.display();b1.drag(mouseX, mouseY);/*attractor*/a.drag();a.hover(mouseX,mouseY);//拖拽中心物体a.display();for (int i = 0; i < movers.length; i++) {PVector force = a.attract(movers[i]);movers[i].applyForce(force);movers[i].update();movers[i].display();}/*SPACESHIP*/// Update positionship.update();// Wrape edgesship.wrapEdges();// Draw shipship.display();fill(0);text("First click !and then Left Right arrows to turn, Up to thrust",10,height-5);text("首先点击屏幕!左右控制方向转向,上键飞船启动",10,height-20);// Turn or thrust the ship depending on what key is pressedif (keyPressed) {if (key == CODED && keyCode == LEFT) {ship.turn(-0.03);} else if (key == CODED && keyCode == RIGHT) {ship.turn(0.03);} else if (key == CODED && keyCode == UP) {ship.thrust(); }}
}void mousePressed() {b1.clicked(mouseX, mouseY);a.clicked(mouseX, mouseY);
}void mouseReleased() {b1.stopDragging();a.stopDragging();
}

在这里插入图片描述
引用类Mover

class Bob { PVector position;PVector velocity;PVector acceleration;float mass = 12;// Arbitrary damping to simulate friction / drag float damping = 0.95;// For mouse interactionPVector dragOffset;boolean dragging = false;// ConstructorBob(float x, float y) {position = new PVector(x,y);velocity = new PVector();acceleration = new PVector();dragOffset = new PVector();} // Standard Euler integrationvoid update() { velocity.add(acceleration);velocity.mult(damping);position.add(velocity);acceleration.mult(0);}// Newton's law: F = M * Avoid applyForce(PVector force) {PVector f = force.get();f.div(mass);acceleration.add(f);}// Draw the bobvoid display() { stroke(255,222,173);strokeWeight(2);fill(255,215,0);if (dragging) {strokeWeight(4);fill(255,106,106);}ellipse(position.x,position.y,mass*2,mass*2);} void display2() { stroke(255,0,173);strokeWeight(2);fill(255,215,0);if (dragging) {strokeWeight(4);fill(255,106,106);}ellipse(position.x,position.y,mass*2,mass*2);} // The methods below are for mouse interaction// This checks to see if we clicked on the movervoid clicked(int mx, int my) {float d = dist(mx,my,position.x,position.y);if (d < mass) {dragging = true;dragOffset.x = position.x-mx;dragOffset.y = position.y-my;}}void stopDragging() {dragging = false;}void drag(int mx, int my) {if (dragging) {position.x = mx + dragOffset.x;position.y = my + dragOffset.y;}}
}

SpaceShip代码:

class Spaceship { // All of our regular motion stuffPVector position;PVector velocity;PVector acceleration;// Arbitrary damping to slow down shipfloat damping = 0.995;float topspeed = 6;// Variable for heading!float heading = 0;// Sizefloat r = 40;// Are we thrusting (to color boosters)boolean thrusting = false;Spaceship() {position = new PVector(width/2,height/2);velocity = new PVector();acceleration = new PVector();} // Standard Euler integrationvoid update() { velocity.add(acceleration);velocity.mult(damping);velocity.limit(topspeed);position.add(velocity);acceleration.mult(0);}// Newton's law: F = M * Avoid applyForce(PVector force) {PVector f = force.get();//f.div(mass); // ignoring mass right nowacceleration.add(f);}// Turn changes anglevoid turn(float a) {heading += a;}// Apply a thrust forcevoid thrust() {float angle = heading - PI/2;PVector force = new PVector(cos(angle),sin(angle));force.mult(0.1);applyForce(force); // To draw boosterthrusting = true;}void wrapEdges() {float buffer = r*2;if (position.x > width +  buffer) position.x = -buffer;else if (position.x <    -buffer) position.x = width+buffer;if (position.y > height + buffer) position.y = -buffer;else if (position.y <    -buffer) position.y = height+buffer;}// Draw the shipvoid display() { stroke(0);strokeWeight(2);pushMatrix();translate(position.x,position.y+r);rotate(heading);fill(175);if (thrusting) fill(255,0,0);// Booster rocketsrect(-r/2,r,r/3,r/2);rect(r/2,r,r/3,r/2);fill(240,255,255);// A trianglebeginShape();vertex(-r,r);vertex(0,-r);vertex(r,r);endShape(CLOSE);rectMode(CENTER);popMatrix();thrusting = false;}
}

Spring类的代码

class Spring { PVector anchor;float len;float k = 0.2;Bob a;Bob b;// ConstructorSpring(Bob a_, Bob b_, int l) {a = a_;b = b_;len = l;} void update() {// Vector pointing from anchor to bob positionPVector force = PVector.sub(a.position, b.position);// What is distancefloat d = force.mag();// Stretch is difference between current distance and rest lengthfloat stretch = d - len;force.normalize();force.mult(-1 * k * stretch);a.applyForce(force);force.mult(-1);b.applyForce(force);}void display() {strokeWeight(2);stroke(0);line(a.position.x, a.position.y, b.position.x, b.position.y);}
}

Attractor类的代码:

// A class for a draggable attractive body in our world
class Attractor {float mass;         // Mass, tied to sizePVector position;   // positionfloat g;boolean dragging = false; // Is the object being dragged?boolean rollover = false; // Is the mouse over the ellipse?PVector dragOffset;  // holds the offset for when object is clicked onAttractor() {position = new PVector(width*3/4, height/4);mass = 20;g = 0.4;dragOffset = new PVector(0.0,0.0);}PVector attract(newmover m) {PVector force = PVector.sub(position, m.position);             // Calculate direction of forcefloat distance = force.mag();                                 // Distance between objectsdistance = constrain(distance, 5.0, 25.0);                             // Limiting the distance to eliminate "extreme" results for very close or very far objectsforce.normalize();                                            // Normalize vector (distance doesn't matter here, we just want this vector for direction)float strength = (g * mass * m.mass) / (distance * distance); // Calculate gravitional force magnitudeforce.mult(strength);                                         // Get force vector --> magnitude * directionreturn force;}// Method to displayvoid display() {stroke(255,255,255,120);strokeWeight(6);if (dragging) fill (50);else if (rollover) fill(100);else fill(0,191,255);ellipse(position.x, position.y, 70, 70);}//mouse interactionvoid clicked(int mx, int my) {float d = dist(mx,my,position.x,position.y);if (d < mass) {dragging = true;dragOffset.x = position.x-mx;dragOffset.y = position.y-my;}}void hover(int mx, int my) {float d = dist(mx,my,position.x,position.y);if (d < mass) {rollover = true;} else {rollover = false;}}void stopDragging() {dragging = false;}void drag() {if (dragging) {position.x = mouseX + dragOffset.x;position.y = mouseY + dragOffset.y;}} 
}

newmover类——被Attractor所吸引。

class newmover {PVector position;PVector velocity;PVector acceleration;float mass;float angle = 0;float aVelocity = 0;float aAcceleration = 0;newmover(float m, float x, float y) {mass = m;position = new PVector(x,y);velocity = new PVector(random(-1,1),random(-1,1));acceleration = new PVector(0,0);}void applyForce(PVector force) {PVector f = PVector.div(force,mass);acceleration.add(f);}void update() {velocity.add(acceleration);position.add(velocity);aAcceleration = acceleration.x / 10.0;aVelocity += aAcceleration;aVelocity = constrain(aVelocity,-0.1,0.1);angle += aVelocity;acceleration.mult(0);}void display() {noStroke();//fill(255,130,171,230);float g = random(255);/* float sd = 20; float mean = 200;*//*g = constrain(g,0,255);*/fill(255,g,50);rectMode(CENTER);pushMatrix();translate(position.x,position.y);rotate(angle);rect(0,0,mass*16,mass*16);popMatrix();}
}

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



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

相关文章

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum

Python多线程实现大文件快速下载的代码实现

《Python多线程实现大文件快速下载的代码实现》在互联网时代,文件下载是日常操作之一,尤其是大文件,然而,网络条件不稳定或带宽有限时,下载速度会变得很慢,本文将介绍如何使用Python实现多线程下载... 目录引言一、多线程下载原理二、python实现多线程下载代码说明:三、实战案例四、注意事项五、总结引

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

IDEA与MyEclipse代码量统计方式

《IDEA与MyEclipse代码量统计方式》文章介绍在项目中不安装第三方工具统计代码行数的方法,分别说明MyEclipse通过正则搜索(排除空行和注释)及IDEA使用Statistic插件或调整搜索... 目录项目场景MyEclipse代码量统计IDEA代码量统计总结项目场景在项目中,有时候我们需要统计

MySQL设置密码复杂度策略的完整步骤(附代码示例)

《MySQL设置密码复杂度策略的完整步骤(附代码示例)》MySQL密码策略还可能包括密码复杂度的检查,如是否要求密码包含大写字母、小写字母、数字和特殊字符等,:本文主要介绍MySQL设置密码复杂度... 目录前言1. 使用 validate_password 插件1.1 启用 validate_passwo

MySQL实现多源复制的示例代码

《MySQL实现多源复制的示例代码》MySQL的多源复制允许一个从服务器从多个主服务器复制数据,这在需要将多个数据源汇聚到一个数据库实例时非常有用,下面就来详细的介绍一下,感兴趣的可以了解一下... 目录一、多源复制原理二、多源复制配置步骤2.1 主服务器配置Master1配置Master2配置2.2 从服