第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

相关文章

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义