📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 7371| 回复: 30
跳转到指定楼层
上一主题 下一主题
收起左侧

[CS61B_Spring 2015] HW1 加分讨论帖

全局:
公开课
学校名称: Berkeley
Unit号: 14
开课时间: 2015-05-25
课程全名: CS61B Data Structure
平台: 其他

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
HW1: NBody Simulation

上一篇:Principles of Computing (Part 1)-Week1
下一篇:[CS61B_Spring 2015] Discussion2 - Diagramming, Iterative vs. Recursive
推荐
whosays 2015-6-12 04:21:41 | 只看该作者
全局:

/*
在discussion2 & HW1上付出远超出要求的时间,对这一周的习题训练感想很多,稍微总结:
1. 写好一个robust的test对于初学者是多么的不容易(尤其对初学者),写好一个user-friendly的class同理不易。这点在自己写test测试discussion2时感受最深刻,linked-list的经典NPE处理折磨我好久,好多时候是test本身的问题,反而让我误以为源程序有问题,可见坏test是多么的坑己……在这一点上有了初步认知后再去听SList和sentinel node,理解深入很多

2. 然后,HW1.个人认为官方的作业要求写的不好,有的时候需要参考test代码里的变量名,浪费了一些时间,感觉不是很值。
*/
Planet.java
public class Planet {
        double x;
        double y;
        double xVelocity;
        double yVelocity;
        double mass;
        String img;
        double G = 6.67e-11;
        double xNetForce;
        double yNetForce;
        double xAccel;
        double yAccel;

        public Planet (double xPos, double yPos, double xVel, double yVel, double m, String image) {
                this.x = xPos;
                this.y = yPos;
                this.xVelocity = xVel;
                this.yVelocity = yVel;
                this.mass = m;
                this.img = image;
        } // close constructor

        // Planet planet = new Planet(x, y, xVelocity, yVelocity, mass, img);
        // Planet planet2 = new Planet(x2, y2, xVel2, yVel2, m2, im2);
        public double calcDistance (Planet planetX) {
                double dis;
                dis = Math.sqrt((x - planetX.x) * (x - planetX.x) + (y - planetX.y) * (y - planetX.y));
                // dis = Math.sqrt(dis);
                return dis;
        }

        public double calcPairwiseForce (Planet planetX) {
               
                double f = G * mass * planetX.mass / Math.pow(calcDistance (planetX), 2);
                return f;
        }

        public double calcPairwiseForceX (Planet planetX) {
                double fX = calcPairwiseForce (planetX) * (planetX.x - x) / calcDistance(planetX);
                return fX;
        }

        public double calcPairwiseForceY (Planet planetX) {
                double fY = calcPairwiseForce (planetX) * (planetX.y - y) / calcDistance(planetX);
                return fY;
        }
       
        public void setNetForce (Planet[] planetG) {
                // add the force one by one
                this.xNetForce = 0;
                this.yNetForce = 0;
                for (int i = 0; i < planetG.length; i++) {
                        if (this != planetG[i]) {
                                this.xNetForce += calcPairwiseForceX (planetG[i]);
                                this.yNetForce += calcPairwiseForceY (planetG[i]);
                        } else {
                                this.xNetForce += 0;
                                this.yNetForce += 0;
                        }
                }
        } // close setNetForce method

        /**
        ** Draw the planet itself at its appropriate position
        ** to the specific x, y coordinates.
        **/
        public void draw() {
                StdDraw.picture(this.x, this.y, "images/" + this.img);
        } // close draw method

        /** Update the new positions and velocities of the planets **/
        public void update (double dt) {
                xAccel = this.xNetForce / mass;
                yAccel = this.yNetForce / mass;
                xVelocity = xVelocity + dt * xAccel;
                yVelocity = yVelocity + dt * yAccel;
                x = x + dt * xVelocity;
                y = y + dt * yVelocity;
        } // close update method

}

NBody.java

public class NBody {
        /*
        takes an In object as an argument, and reads from it the information for the next Planet object.
        It will then instantiate that Planet and return it.
        Note that getPlanet will NOT work on an In unless the In has already had its number of planets and universe radius read.
        */
        public static Planet getPlanet (In i) {
                double x = i.readDouble();
                double y = i.readDouble();
                double xvel = i.readDouble();
                double yvel = i.readDouble();
                double m = i.readDouble();
                String img = i.readString();
                Planet p = new Planet(x, y, xvel, yvel, m, img);
                return p;
        } // close getPlanet()

        public static void main(String[] args) {
        // step 1
                double T;
                double time = 0.0;
            T = Double.parseDouble(args[0]);

                double dt = Double.parseDouble(args[1]);
        // step 2
                String filename = args[2];
        // step 3
                In file = new In(filename);
        // step 4
                int planetNum = file.readInt();
                double radius = file.readDouble();
        /***************************************************
        ******   Drawing the Initial Universe State  **********
        *****************************************************/               
        // read all planets into one array
               
                // set the universe scale
                StdDraw.setScale(-radius, radius);
                // draw the initial state of the universe
                StdDraw.picture(0, 0, "images/starfield.jpg");
                // build the array with the size of planet #
                Planet[] uni = new Planet[planetNum];
                // read all planets into array
                for (int j = 0; j < planetNum; j++) {
                        uni[j] = getPlanet(file);
                }
        /*******************************************
        ************* Amimation **********************
        *******************************************/
                StdAudio.play("audio/2001.mid"); // add background audio
                while (time < T) {
                        StdDraw.picture(0, 0, "images/starfield.jpg");
                        for (int k = 0; k < planetNum; k++) {
                                uni[k].setNetForce(uni);
                                uni[k].update(dt);
                                uni[k].draw();
                        }
                        StdDraw.show(10);
                        time += dt;
                }
/**********************************************
        ************** Printing the Universe **********
        ***********************************************/
                StdOut.printf("%d\n", planetNum);
                StdOut.printf("%.2e\n", radius);
                for (int l = 0; l < planetNum; l++) {
                        StdOut.printf("%11.4e %11.4e %11.4e %11.4e %11.4e %12s\n",
                   uni[l].x, uni[l].y, uni[l].xVelocity, uni[l].yVelocity, uni[l].mass, uni[l].img);
                } // close for loop

               
        } // close main method
} // close NBody class
回复

使用道具 举报

推荐
a0106660 2015-6-11 13:48:26 | 只看该作者
全局:
刚刚开始上,之前卡在如何使用Git 这,后来发现用不用这个对我这个**没多大影响。果断先不管。
我也把作业交了吧。
class Planet{
                public static double G = 6.67e-11;
                public double x;
                public double y;
                public double xVelocity;
                public double yVelocity;
                public double mass;
                public String img;
                public double xNetForce;
                public double yNetForce;
                public double xAccel;
                public double yAccel;
        public Planet(double x_position, double y_position, double x_velocity,
                double y_velocity, double mass, String img){
                this.x = x_position;
                this.y = y_position;
                this.xVelocity = x_velocity;
                this.yVelocity = y_velocity;
                this.mass = mass;
                this.img = img;
        }

        public double calcDistance(Planet p){
                return Math.sqrt((this.x-p.x)*(this.x-p.x)+
                        (this.y-p.y)*(this.y-p.y));
        }

        public double calcPairwiseForce(Planet p){
                double distance = this.calcDistance(p);
                return G*this.mass*p.mass/(distance*distance);
        }

        public double calcPairwiseForceX(Planet p){
                double pairwiseForce = this.calcPairwiseForce(p);
                double distance = this.calcDistance(p);
                return (p.x-this.x)/distance*pairwiseForce;
        }

        public double calcPairwiseForceY(Planet p){
                double pairwiseForce = this.calcPairwiseForce(p);
                double distance = this.calcDistance(p);
                return (p.y-this.y)/distance*pairwiseForce;
        }

        public void setNetForce(Planet[] planets){
                this.xNetForce=0;
                this.yNetForce=0;
                for(int i =0;i<planets.length;i++){
                        if(planets[i]!=this){
                                this.xNetForce+=calcPairwiseForceX(planets[i]);
                                this.yNetForce+=calcPairwiseForceY(planets[i]);
                        }
                }
        }

        public void draw(){
                StdDraw.picture(this.x,this.y,"images/"+this.img);
        }
       
        public void update(double dt){
                this.xAccel = this.xNetForce/this.mass;
                this.yAccel = this.yNetForce/this.mass;
                this.xVelocity += this.xAccel*dt;
                this.yVelocity += this.yAccel*dt;
                this.x +=this.xVelocity*dt;
                this.y +=this.yVelocity*dt;

        }
}

class NBody{

        public static void main(String[] args) {
                double T;
                double dt;
                String filename;
                int num;
                double radius;
                Planet[] planets;
                double time =0 ;

                T = Double.parseDouble(args[0]);
                dt =Double.parseDouble(args[1]);
                filename = args[2];
                In in = new In(filename);
                num = in.readInt();
                radius = in.readDouble();
                planets = new Planet[num];
                StdDraw.setScale(-radius,radius);
                StdDraw.picture(0,0,"images/starfield.jpg");
                StdAudio.play("audio/2001.mid");
                for(int i =0;i<num;i++){
                        planets[i] = getPlanet(in);
                }
                while(time<T){
                        StdDraw.picture(0,0,"images/starfield.jpg");
                        for(int j =0;j<num;j++){
                                planets[j].setNetForce(planets);
                                planets[j].update(dt);
                                planets[j].draw();
                        }
                        StdDraw.show(10);
                        time+=dt;
                }
                StdOut.printf("%d\n",num);
                StdOut.printf("%.2e\n",radius);
                for(int k=0;k<num;k++){
                        StdOut.printf("%11.4e %11.4e %11.4e %11.4e %11.4e %12s\n",
                                planets[k].x,planets[k].y,planets[k].xVelocity,
                                planets[k].yVelocity,planets[k].mass,planets[k].img);
                }

        }
        public static Planet getPlanet(In input){
                double x = input.readDouble();
                double y = input.readDouble();
                double xVelocity = input.readDouble();
                double yVelocity = input.readDouble();
                double mass = input.readDouble();
                String img = input.readString();
                Planet p = new Planet(x,y,xVelocity,yVelocity,mass,img);
                return p;
        }
}
回复

使用道具 举报

推荐
Casualet 2015-6-22 15:57:26 | 只看该作者
全局:
本帖最后由 Casualet 于 2015-6-22 16:12 编辑

Planet.java
public class Planet{
   public static final double G=6.67E-11;
   public double x;
   public double y;
   public double xVelocity;
   public double yVelocity;
   public double mass;
   public String img;
   public double xNetForce;   
   public double yNetForce;
   public double xAccel,yAccel;   

    public Planet(double inX,double inY,double inXV,double inYV,
              double inMass,String inImg){
           
             this.x=inX;
             this.y=inY;
             this.xVelocity=inXV;
             this.yVelocity=inYV;
             this.mass=inMass;
             this.img=inImg;     
    }
   
    public double calcDistance(Planet inP){
         double inX=inP.x;
         double inY=inP.y;
         double cX=inX-this.x;
         double cY=inY-this.y;
         return  Math.sqrt(cX*cX+cY*cY);   

    }
    public double calcPairwiseForce(Planet inP){
             double distance=this.calcDistance(inP);
             double result;
             result= G*this.mass*inP.mass/(distance*distance);
             return result;
     }

     public double calcPairwiseForceX(Planet inP){
             double F=this.calcPairwiseForce(inP);
             double r=this.calcDistance(inP);
             double dx=this.x-inP.x;
             dx=(-dx);
             return F*dx/r;
     }        
   
     public double calcPairwiseForceY(Planet inP){
             double F=this.calcPairwiseForce(inP);
             double r=this.calcDistance(inP);
             double dy=this.y-inP.y;
             dy=(-dy);
             return F*dy/r;               

     }
     public void setNetForce(Planet [] inArray){
            int length=inArray.length;
            double Fx,Fy;
            Fx=Fy=0;
            for(int i=0;i<length;i++){
             if(inArray!=this){
                 Fx=Fx+this.calcPairwiseForceX(inArray[i]);
                 Fy=Fy+this.calcPairwiseForceY(inArray[i]);

             }

           }
           this.xNetForce=Fx;
           this.yNetForce=Fy;            
     }
     public void draw(){
        // Draw d=new Draw("planet");   
        StdDraw.picture(x,y,"images/"+img);

     }
     public void update(double dt){
           this.xAccel=this.xNetForce/this.mass;
           this.yAccel=this.yNetForce/this.mass;
           this.xVelocity=this.xVelocity+dt*this.xAccel;
           this.yVelocity=this.yVelocity+dt*this.yAccel;
           this.x=this.x+dt*this.xVelocity;
           this.y=this.y+dt*this.yVelocity;

    }

}


NBody.java
public class NBody{
   public static Planet getPlanet(In in){
      
        double x=in.readDouble();
        double y=in.readDouble();
        double vx=in.readDouble();
        double vy=in.readDouble();
        double mass=in.readDouble();
        String s=in.readString();
   
        return new Planet(x,y,vx,vy,mass,s);

   }
   public static void main(String args[]){
       double T =Double.parseDouble(args[0]);
       double dt=Double.parseDouble(args[1]);
       String filename=args[2];
       In in= new In(filename);
       int numOfPlanets=in.readInt();
       double radius=in.readDouble();      
       Planet [] pArray=new Planet[numOfPlanets];
       for(int i=0;i<numOfPlanets;i++){
          pArray[i]=getPlanet(in);
       //   pArray[i].draw();  
       }  
       StdDraw.setScale(-radius,radius);
       StdDraw.picture(0.0,0.0,"images/starfield.jpg");
       for(int i=0;i<numOfPlanets;i++){
           pArray[i].draw();
       }
       double time =0.0;
       for(;time<T;time+=dt){
          for(int i=0;i<numOfPlanets;i++){
              pArray[i].setNetForce(pArray);
              pArray[i].update(dt);
           }   
          StdDraw.picture(0.0,0.0,"images/starfield.jpg");
          for(int i=0;i<numOfPlanets;i++){
              pArray[i].draw();
          }
          StdDraw.show(10);  
     
       }   
       //After the simulation
       System.out.println(numOfPlanets);
       System.out.println(radius);
       Planet temp;
       for(int i=0;i<numOfPlanets;i++){
          temp=pArray[i];
          System.out.print(temp.x+" "
          +temp.y+" "+temp.xVelocity+" "+
          temp.yVelocity+" "+temp.mass+" "
          +temp.img+"\n");
      
        

      }
   }
}
picture:




最近重装系统,截图环境有点乱,additional部分后面再写。。。。
[/i][/i][/i][/i][/i][/i][/i][/i][/i]
回复

使用道具 举报

🔗
baiery 2015-6-3 00:56:07 | 只看该作者
全局:
楼楼我卡在Update这个方法上了,你做了嘛?我感觉目前只有咱们俩在坚持了。。。
回复

使用道具 举报

🔗
karte_polo 2015-6-3 19:42:34 | 只看该作者
全局:
Planet.java



public class Planet {
        public static double g = 6.67/Math.pow(10,11);
        public double x;
        public double y;
        public double xVelocity;
        public double yVelocity;
        public double mass;
        public String img;
        public double xNetForce;
        public double yNetForce;
        public double xAccel;
        public double yAccel;
        public Planet(double xp,double yp,double xv,double yv, double m, String s){
                this.x = xp;
                this.y = yp;
                this.xVelocity = xv;
                this.yVelocity = yv;
                this.mass = m;
                this.img = s;
        }
        public double calcDistance(Planet p){
                double xdis = this.x - p.x;
                double ydis = this.y - p.y;
                return Math.sqrt(xdis*xdis+ydis*ydis);
        }
        public double calcPairwiseForce(Planet p){
                double dis = this.calcDistance(p);
                return g*this.mass*p.mass/(dis*dis);
        }
        public double calcPairwiseForceX(Planet p){
                double dis = this.calcDistance(p);
                return calcPairwiseForce(p)*(p.x-this.x)/dis;
        }
        public double calcPairwiseForceY(Planet p){
                double dis = this.calcDistance(p);
                return calcPairwiseForce(p)*(p.y-this.y)/dis;
        }
        public void setNetForce(Planet[] ps){
                int l = ps.length;
                this.xNetForce = 0;
                this.yNetForce = 0;
                for (int n=0; n<l;n++){
                        if (this != ps[n]){
                                this.xNetForce+= calcPairwiseForceX(ps[n]);
                                this.yNetForce+= calcPairwiseForceY(ps[n]);
                        }       
                }
        }
        public void draw(){
                StdDraw.picture(this.x,this.y,"images/"+this.img);
        }
        public void update(double dt){
                xAccel = xNetForce/mass;
                yAccel = yNetForce/mass;
                xVelocity +=dt*xAccel;
                yVelocity +=dt*yAccel;
                x +=dt*xVelocity;
                y +=dt*yVelocity;
        }
       

}



NBody.java:

import java.io.*;
import java.util.Scanner;
public class NBody {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                double T,dt,R,time;
                String filename;
                int num;
                Planet p;
                Scanner s = new Scanner(System.in);
                T = s.nextDouble();
                dt =s.nextDouble();
                filename = s.next();
                In in = new In(filename);
                num = in.readInt();
                R = in.readDouble();
                Planet[] ps = new Planet[num];
                StdDraw.setScale(-R,R);
                for (int n=0;n<num;n++){
                        ps[n] = getPlanet(in);
                }
                time= 0.0;
                StdAudio.play("audio/2001.mid");
                while (time<T){
                StdDraw.picture(0.0,0.0,"images/starfield.jpg",R*2.0,R*2.0);
                        for (int n=0;n<num;n++){
                                ps[n].setNetForce(ps);
                                ps[n].update(dt);
                                ps[n].draw();
                        }
                        StdDraw.show(5);
                        time+=dt;
                }
                System.out.println(num);
                System.out.println(R);
                for (int n=0;n<num;n++){
                        p = ps[n];
                        System.out.println(p.x+"  "+p.y+"  "+p.xVelocity+"  "+p.yVelocity+"  "+p.mass+"  "+p.img);
                }

        }
        public static Planet getPlanet(In i){
                double d1 = i.readDouble();
                double d2 = i.readDouble();
                double d3 = i.readDouble();
                double d4 = i.readDouble();
                double d5 = i.readDouble();
                String imgs = i.readString();
                Planet p = new Planet(d1,d2,d3,d4,d5,imgs);
                return p;
        }

}
回复

使用道具 举报

🔗
baiery 2015-6-4 00:34:34 | 只看该作者
全局:
public class Planet{
        public  double x, y, xVelocity, yVelocity, mass;
        public  String img;
        public  double xNetForce, yNetForce, xAccel, yAccel;

        public Planet(double xp, double yp, double xVp, double yVp, double massp, String imgp){
                x = xp;
                y = yp;
                xVelocity = xVp;
                yVelocity = yVp;
                mass = massp;
                img = imgp;
        }

        public  double calcDistance(Planet a){
                double disx = Math.abs(a.x-this.x);
                double disy = Math.abs(a.y-this.x);

                return Math.sqrt(disx*disx+disy*disy);
        }

        public  double calcPairwiseForce(Planet a){
                return (6.67e-11*this.mass*a.mass/(calcDistance(a)*calcDistance(a)));
        }

        public  double calcPairwiseForceX(Planet a){
                return (calcPairwiseForce(a)*(a.x-this.x)/calcDistance(a));
        }

        public double calcPairwiseForceY(Planet a){
                return (calcPairwiseForce(a)*(a.y-this.y)/calcDistance(a));
        }

        public  void setNetForce(Planet[] a){
               
                for (int i = 0; i < a.length;i++){
                        if (!(a[i].x == this.x && a[i].y == this.y) ){
                                xNetForce = calcPairwiseForceX(a[i])+xNetForce ;
                                yNetForce = calcPairwiseForceY(a[i])+yNetForce;
                        }
                }

        }

        public  void draw(){
                StdDraw.setXscale(0.0,10.0);
                StdDraw.setYscale(0.0,10.0);
                StdDraw.point(x,y);
        }

        public  void update(double dt){
                xAccel = this.xNetForce/ mass;

                yAccel = this.yNetForce / mass;
               
                xVelocity = xVelocity + dt*xAccel;
               
                yVelocity = yVelocity + dt*yAccel;
               
                x = x + dt*xVelocity;
               
                y = y + dt*yVelocity;
        }

       
}

先把Planet.java放上来,至于NBody.java实在不太会写,根本不知道该用什么方法。。。。求助
回复

使用道具 举报

🔗
baiery 2015-6-4 12:00:12 | 只看该作者
全局:

可不可以问你一下,如何知道import什么文件,另外从txt里读数据我看到一个Bufferreader的库,说是把每行的一个数存入数组,你是怎么知道用这个scanner的?谢谢!
回复

使用道具 举报

🔗
karte_polo 2015-6-4 16:05:19 | 只看该作者
全局:
baiery 发表于 2015-6-4 12:00
可不可以问你一下,如何知道import什么文件,另外从txt里读数据我看到一个Bufferreader的库,说是把每行 ...

Scanner是我之前在网上找“JAVA如何读入控制台输入”时候找到的,这是JAVA IO库里一个比较简单的类,使用的时候只需要事先import java.util.scanner就可以了。至于使用什么库需要IMPORT什么文件,可以去百度查一下;
BuffetReader和Scanner都可以用来读控制台上的输入,但SCANNER相对更简单些,但也很容易出错- -
回复

使用道具 举报

🔗
baiery 2015-6-4 16:12:22 | 只看该作者
全局:
karte_polo 发表于 2015-6-4 16:05
Scanner是我之前在网上找“JAVA如何读入控制台输入”时候找到的,这是JAVA IO库里一个比较简单的类,使用 ...

好的,看来的确是要自己研究方法,我还以为课程网站上要要用什么方法的提示,谢谢啦!
回复

使用道具 举报

🔗
inversion39 2015-6-6 11:44:21 | 只看该作者
全局:
baiery 发表于 2015-6-4 03:12
好的,看来的确是要自己研究方法,我还以为课程网站上要要用什么方法的提示,谢谢啦!

不需要用Scanner也可以的
args本身就是一个string数组,所以直接读取就可以了

T = Double.parseDouble(args[0]);
dt = Double.parseDouble(args[1]);
filename = args[2];
回复

使用道具 举报

无效楼层,该帖已经被删除
🔗
inversion39 2015-6-6 12:45:33 | 只看该作者
全局:
NBody.java

import java.io.*;
import java.util.Scanner;

public class NBody{
    public static Planet getPlanet(In in){
        double x = in.readDouble();
        double y = in.readDouble();
        double xVelocity = in.readDouble();
        double yVelocity = in.readDouble();
        double mass = in.readDouble();
        String img = in.readString();
        Planet planet = new Planet(x, y, xVelocity, yVelocity, mass, img);
        return planet;
    }

    public static void main(String[] args){
        double T, dt, radius;
        double time = 0.0;
        int num;
        String filename;
        
        T = Double.parseDouble(args[0]);
        dt = Double.parseDouble(args[1]);
        filename = args[2];

        In in  = new In(filename);
        num = in.readInt();
        radius = in.readDouble();
        StdDraw.setScale(-radius, radius);

        Planet[] planets = new Planet[num];
        for (int i = 0; i < num; i++){
            planets[i] = getPlanet(in);
        }

        StdAudio.play("audio/2001.mid");

        while (time < T){
            StdDraw.picture(0.0, 0.0, "images/starfield.jpg");
            for (int i = 0; i < num; i++){
                planets[i].update(dt);
                planets[i].draw();
            }
            StdDraw.show(10);
            time += dt;
        }
StdOut.printf("%d\n", num);
        StdOut.printf("%.2e\n", radius);
        for (int i = 0; i < num; i++) {
            StdOut.printf("%11.4e %11.4e %11.4e %11.4e %11.4e %12s\n",
            planets[i].x, planets[i].y, planets[i].xVelocity, planets[i].yVelocity, planets[i].mass, planets[i].img);
        }
    }
}


Planet.java

/* homework from cs61b spring 2015
* https://berkeley-cs61b.github.io ... als/hw/hw1/hw1.html */

public class Planet{
    double x;
    double y;
    double xVelocity;
    double yVelocity;
    double mass;
    String img;

    double xNetForce = 0.0;
    double yNetForce = 0.0;
   
    double xAccel = 0.0;
    double yAccel = 0.0;

    /* constructor for Planet*/
    public Planet(double x, double y, double xVelocity, double yVelocity, double mass, String img){
        this.x = x;
        this.y = y;
        this.xVelocity = xVelocity;
        this.yVelocity = yVelocity;
        this.mass = mass;
        this.img = img;
    } //close constructor

    /* calculate the distance between two Planets */
    public double calcDistance(Planet planet){
        return (Math.sqrt(Math.pow((x - planet.x), 2) + (Math.pow((y - planet.y), 2))));
    } //close calcDistance

    /* calculate pairwise Force */  
    public double calcPairwiseForce(Planet planet){
        return (6.67*Math.pow(10, -11) * mass * planet.mass / Math.pow(this.calcDistance(planet), 2));
    }// close calcPairwiseForce

    /* calculate the force on x dirction */
    public double calcPairwiseForceX(Planet planet){
        return this.calcPairwiseForce(planet) * Math.abs(planet.x - this.x) / this.calcDistance(planet);
    } // close calcPairwiseForceX

    /* calculate the force on y dirction */
    public double calcPairwiseForceY(Planet planet){
        return this.calcPairwiseForce(planet) * Math.abs(planet.y - this.y) / this.calcDistance(planet);
    } // close calcPairwiseForceY

    /* Net Force: The principle of superposition says that
     * the net force acting on a particle in the x- or y-direction
     * is the sum of the pairwise forces acting on the particle
     * in that direction.*/
    public void setNetForce(Planet[] planets){
        for (int i=0; i < planets.length; i++){
            xNetForce += this.calcPairwiseForceX(planets[i]);
            yNetForce += this.calcPairwiseForceY(planets[i]);
        }
    } // close setNetForce

    /* We also want a planet to be able to draw itself at its appropriate position. */
    public void draw(){
        StdDraw.picture(this.x, this.y, "images/" + this.img);
    } // close draw

    /* update the planet */
    public void update(double dt){
        //this.setNetForce();
         this.xAccel = this.xNetForce/ this.mass;
         this.yAccel = this.yNetForce/ this.mass;

        this.xVelocity = this.xVelocity + this.xAccel * dt;
        this.yVelocity = this.yVelocity + this.yAccel * dt;

        this.x = this.x + dt * this.xVelocity;
        this.y = this.y + dt * this.yVelocity;

    } // close update
}


结果出来是个动画,还有下面的输出
5
2.50e+11
1.4960e+11  4.7024e+12  0.0000e+00  2.9800e+04  5.9740e+24    earth.gif
2.2790e+11  3.8030e+12  0.0000e+00  2.4100e+04  6.4190e+23     mars.gif
5.7900e+10  7.5586e+12  0.0000e+00  4.7900e+04  3.3020e+23  mercury.gif
0.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00  1.9890e+30      sun.gif
1.0820e+11  5.5230e+12  0.0000e+00  3.5000e+04  4.8690e+24    venus.gif
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表