Coder Social home page Coder Social logo

xiaoliangstd / bolckcrash Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yikai-coder/bolckcrash

0.0 0.0 0.0 13 KB

看到3Bule1Brown关于木块碰撞的视频,想到有计算机来算一下试试,于是写了一个这样的程序

C 100.00%

bolckcrash's Introduction

木块碰撞和Π的联系验证

前言

在3Blue1Brown那里看到一个视频,讲的是两个小木块在理想条件下的碰撞次数,竟然非常神奇的和Π联系在了一起,感到非常神奇,于是就想写一个程序来验证一下,结果发现事实果然如此,非常神奇。

引子的视频链接

原理的视频链接

关于程序

1、程序原理:

定义v1作为大木块速度,v2作为小木块速度;

定义一个VECTOR结构体来存放二元组(v1,v2);

设小木块质量为m,大木块质量为nm;

根据高中的动量定理和动能守恒推出(背出😂)碰撞速度的计算公式 $$ v1’ = ((n-1)v_1+2v_2)/(n+1) $$

$$ v2'=((1-n)v_2+2v_1)/(n+1) $$

定义calculate函数来计算每一次碰撞后的速度;

handleCrash用来模拟每个n对应的多次碰撞过程:

  1. 第一次碰撞时,直接调用calculate函数计算即可;

  2. 只有当两个木块都是反向于墙壁运动(v<0)时,且v1<v2时才无法追上,碰撞结束;

  3. 若两个木块均面向墙壁运动(v>0),则除了第一次碰撞外,其他情况下必定是小木块比大木块要快(否则碰撞就没有结束),所以小木块必定要撞一次墙反弹回来再和大木块相碰,所以v2要取相反数。

  4. 当两个木块相向而行时(v1>0,v2<0),直接调用calculate函数;

  5. 当两个木块相背而行(v1<0,v2>0),小木块会撞墙后返回变成上面的情况2

2、在编写这个程序的过程踩了很多坑,在这里和大家分享

  • 编译错误
D:\Program Files (x86)\C\BolckCrash\main.c|85|error: stray '\243' in program|
D:\Program Files (x86)\C\BolckCrash\main.c|85|error: stray '\273' in program|

这个地方是因为一个分号用成了中文格式的,所以会跳出这两个编码上的错误,原理是这个中文分号的ascii码值是243,不在英文的范围内(0~127),所以是这样的报错。

  • 数据溢出的问题

平常写程序很少会用到这么大的数字(100e5等待),所以在写的时候没怎么注意,第一次没有用long long和long double,造成了数据的溢出,修改之后其实还是只能走到100e8,后面的就算不了了,可能得自定义一个数据类型,比较麻烦,以后有空再补吧‘。

  • 原理问题

​ 这个碰撞是包括木块和墙壁的碰撞的,一开始忽略了这点跑出来的程序就成了Π/2了。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.