第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

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识