cs61b2018sp WEEK2 课后练习

2022.3.20

网站:
NBody Simulation | CS 61B Spring 2018 (datastructur.es)

WEEK2 课后练习

LAB

1.生词

prerequisites——先决条件
prompts——提示 (n.)
Plugin——插入
Interactive——交互的
retrieve——取回,恢复(v.)
repository——存储库
hover over——悬停在
in beta——处于测试阶段
subtleties——微妙之处
mutually——相互
tugging on——拉扯
explicitly——明确的
typos——错别字
hassle——麻烦(n.)
parameter——范围;参数
consternation——惊愕
pedagogical——教学法
simulation——模拟
particle——粒子;微粒
curvature——曲率
superposition——叠加(n.)
pairwise——成对的
celestial——天上的;太空的
coordinate——坐标
exert——施加
magnitude——大小;量纲
Neptune——海王星
Saturn——土星
constant——常量;恒量
notation——符号;注释;表示法
snippet——片段
verbosity——冗长(n.)
verbose——冗长的;累赘的
squirrel——松鼠
corresponding to——对应于
intimidating——吓人的
detour——绕行(v.)
discretize——离散化(v.)
flicker——闪烁(v.)
tweak——调整(v.)
subtle——微妙的
Acknowledgements——致谢(n.)

Project0

1.The Planet Class and Its Constructor

按照要求,编写一个Planet类

public class Planet {
    double xxPos,yyPos,xxVel,yyVel,mass;
    String imgFileName;
    public  Planet(double xP,double yP,double xV,double yV,double m,String img){
        xxPos = xP;
        yyPos = yP;
        xxVel = xV;
        yyVel = yV;
        mass = m;
        imgFileName = img;
    }
    public  Planet(Planet p){
        xxPos = p.xxPos;
        yyPos = p.yyPos;
        xxVel = p.xxVel;
        yyVel = p.yyVel;
        mass = p.mass;
        imgFileName = p.imgFileName;
    }

}

再按照要求测试


测试结果

2.Understanding the Physics

列出了一些物理公式,基本都是初中知识,简单看一下就好



这里的dx,dy是两个坐标的x,y差值,不是微分号

3.Writing the Planet Class

calcDistance

    public double calcDistance(Planet p){//calculates the distance between two Planets
        return Math.sqrt(Math.pow(p.xxPos - xxPos,2)+Math.pow(p.yyPos - yyPos,2));
    }
运行结果

calcForceExertedBy

    public double calcForceExertedBy(Planet p){
        return (G*p.mass*mass)/Math.pow(this.calcDistance(p),2);
    }
运行结果

calcForceExertedByX and calcForceExertedByY

    public double calcForceExertedByX(Planet p){
        return (this.calcForceExertedBy(p)*(p.xxPos-xxPos))/this.calcDistance(p);
    }

    public double calcForceExertedByY(Planet p){
        return (this.calcForceExertedBy(p)*(p.yyPos-yyPos))/this.calcDistance(p);
    }
运行结果

calcNetForceExertedByX and calcNetForceExertedByY

    public double calcNetForceExertedByX(Planet[] ps){
        double Fxsum = 0;
        for(Planet p:ps){
            if (p.equals(this)){
                continue;
            }else {
                Fxsum += this.calcForceExertedByX(p);
            }
        }
        return Fxsum;
    }

    public double calcNetForceExertedByY(Planet[] ps){
        double Fysum = 0;
        for(Planet p:ps){
            if (p.equals(this)){
                continue;
            }else {
                Fysum += this.calcForceExertedByY(p);
            }
        }
        return Fysum;
    }
运行结果

update

    public void update(double dt,double fX,double fY){
        double ax = fX/mass;
        double ay = fY/mass;
        xxVel = xxVel + ax*dt;
        yyVel = yyVel + ay*dt;
        xxPos = xxPos + dt*xxVel;
        yyPos = yyPos + dt*yyVel;
    }
运行结果

NBody类、readRadius和readPlanets方法

public class NBody {
    public static double readRadius(String Str){
        In in = new In(Str);
        int firstindemo = in.readInt();
        double radiusIn = in.readDouble();
        return radiusIn;
    }

    public static Planet[] readPlanets(String Str){
        In in = new In(Str);
        int totalN = in.readInt();
        double radiusIn = in.readDouble();
        Planet[] ps = new Planet[totalN];
        for(int i = 0;i < totalN;i++){
            Planet p = new Planet(in.readDouble(),in.readDouble(),in.readDouble(),in.readDouble(),in.readDouble(),in.readString());
            ps[i] = p;
        }
        return ps;
    }
}
Hoorah!

Drawing the Initial Universe State (main)

public class NBody {
    public static double readRadius(String Str){
        In in = new In(Str);
        int firstindemo = in.readInt();
        double radiusIn = in.readDouble();
        return radiusIn;
    }

    public static Planet[] readPlanets(String Str){
        In in = new In(Str);
        int totalN = in.readInt();
        double radiusIn = in.readDouble();
        Planet[] ps = new Planet[totalN];
        for(int i = 0;i < totalN;i++){
            Planet p = new Planet(in.readDouble(),in.readDouble(),in.readDouble(),in.readDouble(),in.readDouble(),in.readString());
            ps[i] = p;
        }
        return ps;
    }

    public static void main(String[] args){
        String TStr = args[0];
        String dtStr = args[1];
        double T = Double.parseDouble(TStr);
        double dt = Double.parseDouble((dtStr));
        String filename = args[2];
        Planet[] ps = NBody.readPlanets(filename);
        double Radius = NBody.readRadius(filename);
        StdDraw.setCanvasSize((int)((Radius)/5e8),(int)((Radius)/5e8));
        StdDraw.setScale(-Radius/5e8,Radius/5e8);
        StdDraw.picture(Radius/35/5e8,Radius/35/5e8,"C:/Users/OMEN/Desktop/offclass/cs61b/Proj0/images/starfield.jpg");
        for(Planet p:ps){
            p.draw();
        }

    }
}
运行结果

Creating an Animation

更新后的main方法如下所示

    public static void main(String[] args){
        String TStr = args[0];
        String dtStr = args[1];
        double T = Double.parseDouble(TStr);
        double dt = Double.parseDouble((dtStr));
        String filename = args[2];
        Planet[] ps = NBody.readPlanets(filename);
        double Radius = NBody.readRadius(filename);
        StdDraw.setCanvasSize((int)((Radius)/5e8),(int)((Radius)/5e8));
        StdDraw.setScale(-Radius/5e8,Radius/5e8);
        StdDraw.picture(Radius/35/5e8,Radius/35/5e8,"C:/Users/OMEN/Desktop/offclass/cs61b/Proj0/images/starfield.jpg");
        for(Planet p:ps){
            p.draw();
        }
        StdDraw.enableDoubleBuffering();
        double time = 0;
        for(;time <= T ; time += dt){
            double[] xForces = new double[5];
            double[] yForces = new double[5];
            for(int i = 0;i < 5;i++){
                xForces[i] = ps[i].calcNetForceExertedByX(ps);
                yForces[i] = ps[i].calcNetForceExertedByY(ps);
            }
            for(int i = 0;i < 5;i++){
                ps[i].update(dt,xForces[i],yForces[i]);
            }
            StdDraw.picture(Radius/35/5e8,Radius/35/5e8,"C:/Users/OMEN/Desktop/offclass/cs61b/Proj0/images/starfield.jpg");
            for(Planet p:ps){
                p.draw();
            }
            StdDraw.show();
            StdDraw.pause(1);
        }
    }

}
这里应该是动图,但截的图是静止的,大概就是其他的星球绕着太阳转

完结语

这次的项目还是很有意思的,跟着做其实不难,每一步老师都给有基本思路,但这一整个写项目的过程,能给我们这些学生很大的收获。当最后行星都绕着太阳转时,我笑得合不拢嘴,这种收获的乐趣促使着我不断学习下去,去创作出更cool的项目。总之,感谢Josh Hug老师的项目指导,我真真正正地感受到了编程的乐趣。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,902评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,037评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,978评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,867评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,763评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,104评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,565评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,236评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,379评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,313评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,363评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,034评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,637评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,719评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,952评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,371评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,948评论 2 341