Processing+代码本色 chap3 振荡

2023-11-26 13:20

本文主要是介绍Processing+代码本色 chap3 振荡,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

弹力

介绍

在这里插入图片描述
弹簧的弹力可以根据胡克定律计算得到,胡克定律以英国物理学家罗伯特·胡克命名,他在1660年发明了这个公式。胡克最初是用拉丁文描述这个公式的——“Ut tensio,sic vis”, 这句话的意思是“力如伸长(那样变化)”。我们可以这么理解它:弹簧的弹力与弹簧的伸长量成正比。

先用胡克定律计算弹力的大小。我们需要知道k和x的值:k很简单,它只是一个常量,我们可以随意选择一个数。

float k = 0.1;

x可能会更复杂,我们需要知道“当前长度和静止长度的差”。可以用restLength表示静止长度,

PVector dir = PVector.sub(bob,anchor); 由枢轴点指向摆锤的向量,它告诉我们弹簧的当前长度
float currentLength = dir.mag();
float x = restLength - currentLength;

方向计算
在这里插入图片描述

float k = 0.1; 按照胡克定律计算得到的弹力
PVector force = PVector.sub(bob,anchor);
float currentLength = dir.mag();
float x = restLength - currentLength;
force.normalize(); 弹力的方向(单位向量)
force.mult(-1 k x); 把方向和大小放在一起!

下面两个类不做过多介绍,可以参考书本第三章的介绍

Spring类的实现

class Spring { PVector anchor;// 静止长度和弹簧常数float len;float k = 0.2;Bob a;Bob b;// Spring(Bob a_, Bob b_, int l) {a = a_;b = b_;len = l;} // Calculate spring forcevoid 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;// Calculate force according to Hooke's Lawforce.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);}
}

控制小球的类(Bob类)

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(0);strokeWeight(2);fill(175);if (dragging) {fill(50);}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;}}
}

网格设计

在这里插入图片描述
根据上面两个类对网进行实现

int Bobnum = 10;
Bob[][] b = new Bob[Bobnum][Bobnum];
Spring[] s = new Spring[2*Bobnum*(Bobnum-1)];

Bobnum:网的长度(即每一行的点的个数,宽度和长度相等)
b:存储网的点的数组
s:存储点之间连线的数组

对数组进行初始化,连线方式参考上图

void setup() {size(1000, 800);// 在起始位置创建对象//请注意,Spring构造函数中的第三个参数是"rest length"int Springnum = 0;for(int i=0;i<Bobnum;i++)for(int j=0;j<Bobnum;j++)b[i][j] = new Bob(width*j/(Bobnum-1),height*i/((Bobnum-1)));for(int i=0;i<Bobnum;i++)for(int j=0;j<Bobnum;j++){if(j<Bobnum-1&&i<Bobnum-1){s[Springnum] = new Spring(b[i][j],b[i][j+1],int(width/(Bobnum-1)));s[Springnum+1] = new Spring(b[i][j],b[i+1][j],int(height/((Bobnum-1))));Springnum +=2;}else{if(j==Bobnum-1 &&i<Bobnum-1){s[Springnum] = new Spring(b[i][j],b[i+1][j],int(height/((Bobnum-1))));Springnum+=1;}else if(j<Bobnum-1&&i==Bobnum-1){s[Springnum] = new Spring(b[i][j],b[i][j+1],int(width/(Bobnum-1)));Springnum +=1;}else{}}}
}

在draw()函数中进行实现

void draw() {background(255); for(int a=0;a<2*Bobnum*(Bobnum-1);a++)s[a].update();for(int a=0;a<2*Bobnum*(Bobnum-1);a++)s[a].display();for(int i=0;i<Bobnum;i++)for(int j=0;j<Bobnum;j++){b[i][j].update();b[i][j].display();}b[int(Bobnum/2)][int(Bobnum/2)].drag(mouseX, mouseY);fill(255,0,0);ellipse(b[int(Bobnum/2)][int(Bobnum/2)].position.x,b[int(Bobnum/2)][int(Bobnum/2)].position.y,b[int(Bobnum/2)][int(Bobnum/2)].mass*2,b[int(Bobnum/2)][int(Bobnum/2)].mass*2);fill(0);text("点击鼠标右键重置", 10, 30);
}

点击鼠标右键进行网的初始位置重现,鼠标左键点击红色的球可以进行拖动。

void mousePressed() {if (mouseButton == RIGHT){setup();}b[int(Bobnum/2)][int(Bobnum/2)].clicked(mouseX, mouseY);
}void mouseReleased() {b[int(Bobnum/2)][int(Bobnum/2)].stopDragging();
}

运行效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

这篇关于Processing+代码本色 chap3 振荡的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La