代码本色第2章——力学

编程入门 行业动态 更新时间:2024-10-12 10:20:45

代码本色第2章——<a href=https://www.elefans.com/category/jswz/34/1764303.html style=力学"/>

代码本色第2章——力学

序言

此博文基于《代码本色》第三章所写,我们先来回顾一下牛顿力学三大定律

牛顿第一定律

内容:一切物体在没有受到力或合力为零的作用时,总保持静止状态或匀速直线运动状态。
说明:物体都有维持静止和作匀速直线运动的趋势,因此物体的运动状态是由它的运动速度决定的,没有外力,它的运动状态是不会改变的。物体的这种性质称为惯性。所以牛顿第一定律也称为惯性定律。第一定律也阐明了力的概念。明确了力是物体间的相互作用,指出了是力改变了物体的运动状态。因为加速度是描写物体运动状态的变化,所以力是和加速度相联系的,而不是和速度相联系的。在日常生活中不注意这点,往往容易产生错觉。
注意:牛顿第一定律并不是在所有的参照系里都成立,实际上它只在惯性参照系里才成立。因此常常把牛顿第一定律是否成立,作为一个参照系是否惯性参照系的判据。

牛顿第二定律

内容:物体在受到合外力的作用会产生加速度,加速度的方向和合外力的方向相同,加速度的大小与合外力的大小成正比,与物体的惯性质量成反比。
公式:F=ma,F为合外力
牛顿第二定律定量描述了力作用的效果,定量地量度了物体的惯性大小。它是矢量式,并且是瞬时关系。
要强调的是,物体受到的不为零合外力,会产生加速度,使物体的运动状态或速度发生改变,但是这种改变是和物体本身的运动状态有关的。
局限:该定律只适用于宏观物体的低速运动。

牛顿第三定律

内容:两个物体之间的作用力和反作用力,在同一条直线上,大小相等,方向相反。
需要注意:
(1)作用力和反作用力是没有主次、先后之分。同时产生、同时消失。
(2)这一对力是作用在不同物体上,不可能抵消。
(3)作用力和反作用力必须是同一性质的力。
(4)与参照系无

说白了,力就是一个带有方向的标量,也就是说它也是一个向量,此时可以参考前一篇关于向量的博客代码本色第1章——向量

此博客实现的功能与向量中相同,并在其基础上增加了重力。

可见小球与鼠标距离小于一定范围之后,小球就会弹回,但是仍然有接近的趋势

此作品的力学知识有:
1.牛顿第二定律
2.力的累加
3.重力

实验效果

Mover代码

class Mover {      
PVector position;    
PVector velocity;    
PVector acceleration;    
float mass;    
float g ;        
Mover(float m, float x, float y) {      
mass =m;      
g=1;      
position = new PVector(x, y);      
velocity = new PVector(0, 0);      
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);      acceleration.mult(0);    }      void display() {      stroke(0);      strokeWeight(1);            float r=random(255);      float g=random(255);      float b=random(255);      fill(200,r,g,b);       if(position.x>width||position.x<0)  {        velocity.x=velocity.x*(-1);              }       if(position.y>height||position.y<0)      {      velocity.y=velocity.y*(-1);          }      float xx=mouseX-position.x;      float yy=mouseY-position.y;      double xy=Math.sqrt(xx*xx+yy*yy);      if(xy<80)      {        velocity.x=velocity.x*(-1);velocity.y=velocity.y*(-1);          }    ellipse(position.x, position.y, mass*25, mass*25);  }      PVector attract(Mover m) {      PVector force = PVector.sub(position, m.position);             // Calculate direction of force      float distance = force.mag();                                 // Distance between objects      distance = constrain(distance, 5.0, 25.0);                             // Limiting the distance to eliminate "extreme" results for very close or very far objects      force.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 magnitude      force.mult(strength);                                         // Get force vector --> magnitude * direction      return force;  }   }  

主函数代码


Mover[] movers = new Mover[20];        
void setup() {    
size(640,400);    
for (int i = 0; i < movers.length; i++) {    movers[i] = new Mover(random(0.1,2),random(width),random(height)); 
}        
}    
void draw() 
{    
background(0);    
for (int i = 0; i < movers.length; i++) 
{      
for (int j = 0; j < movers.length; j++) 
{        
if (i != j) 
{          
PVector force = movers[j].attract(movers[i]);           float xx=mouseX-movers[i].position.x;           
float yy=mouseY-movers[i].position.y;           
double xy=Math.sqrt(xx*xx+yy*yy);      
if(xy<50)      
{                
force.x=-force.x;        
force.y=-force.y;      
}            
movers[i].applyForce(force);        
}             
}       
float m=0.1*movers[i].mass;      
PVector g=new PVector(0,m);      movers[i].applyForce(g);        
movers[i].update();      
movers[i].display();               
}   
}  

其中,与前一篇的向量不同之处是

if(xy<50)      
{                
force.x=-force.x;        
force.y=-force.y;      
}            

此代码段是为了让圆形所受到的力反向,所以直接乘以-1即可

float m=0.1*movers[i].mass;      
PVector g=new PVector(0,m);      
movers[i].applyForce(g); 

此代码段主要作用是加了重力,这样就会让球有一种从空中掉落下来的感觉。

更多推荐

代码本色第2章——力学

本文发布于:2024-02-10 18:42:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1676737.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:力学   本色   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!