美文网首页
cs61b2018sp WEEK2 课后练习

cs61b2018sp WEEK2 课后练习

作者: 且乐一杯酒 | 来源:发表于2022-03-19 14:49 被阅读0次

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老师的项目指导,我真真正正地感受到了编程的乐趣。

相关文章

网友评论

      本文标题:cs61b2018sp WEEK2 课后练习

      本文链接:https://www.haomeiwen.com/subject/fkbpdrtx.html