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

[CS61B_Spring 2015] HW1 加分讨论帖

🔗
inversion39 2015-6-11 21:02:20 | 只看该作者
全局:
wowmomsos 发表于 2015-6-10 23:42
好吧,第二个问题,我像你这样写就是动态图,你却是静态的。
应该是你的setNetForce的问题,我 ...

多谢指教!

动画是出来的,但是为何是远离太阳系的,没道理啊
回复

使用道具 举报

🔗
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
回复

使用道具 举报

🔗
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]
回复

使用道具 举报

🔗
pigeyes 2015-7-13 18:43:44 | 只看该作者
全局:

你的g 怎麼是6.67*Math.pow(10,11)而不是6.67*Math.pow(10,-11)?
回复

使用道具 举报

🔗
pigeyes 2015-7-13 21:33:02 | 只看该作者
全局:
zhugejun 发表于 2015-6-11 21:02
多谢指教!

动画是出来的,但是为何是远离太阳系的,没道理啊

我animation出來也是遠離太陽系..... 完全找不出問題
回复

使用道具 举报

🔗
pigeyes 2015-7-13 22:04:15 | 只看该作者
全局:
終於debug完畢......
來交作業


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;
        public double yAccel;

        public Planet(double poX, double poY, double vX,
                                  double vY, double m, String image){
                x = poX;
                y = poY;
                xVelocity = vX;
                yVelocity = vY;
                mass = m;
                img = image;
        }

        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){
                return G*this.mass*p.mass/calcDistance(p)/calcDistance(p);
        }

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

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

        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(x, y, "images/"+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 += xVelocity*dt;
                this.y += yVelocity*dt;
        }

}



public class NBody{

        /* new the next planet by scanning the In it takes in. */
        public static Planet getPlanet(In in){
                double[] planetInfo = new double[5];
                String planetName;
                for (int n=0; n<planetInfo.length; n++){
                        planetInfo[n] = in.readDouble();
                }
                planetName = in.readString();
                Planet p = new Planet(planetInfo[0], planetInfo[1], planetInfo[2],
                                                          planetInfo[3], planetInfo[4], planetName);
                return p;
        }

        public static void main(String[] args){
                double T = Double.parseDouble(args[0]);
                double dt = Double.parseDouble(args[1]);
                String filename = args[2];
                In i = new In(filename);
                int numOfPlanets = i.readInt();
                double universeRadius = i.readDouble();
                Planet[] planets = new Planet[numOfPlanets];
                /* save all planets*/
                for (int n=0; n<planets.length; n++){
                        planets[n] = getPlanet(i);
                }
                StdDraw.setScale(-universeRadius, universeRadius);
                StdDraw.picture(0, 0, "images/starfield.jpg");
                /*start point*/
                for (int n=0;n<planets.length; n++){
                        planets[n].draw();
                }

                /*animation*/
                for (double time=0; time<T; time+=dt){
                        StdDraw.picture(0, 0, "images/starfield.jpg");                       
                        for (int n=0;n<planets.length; n++){
                                planets[n].setNetForce(planets);
                                planets[n].update(dt);
                                planets[n].draw();
                                }
                        StdDraw.show(10);
                }

                /* print out final state*/
                StdOut.printf("%d\n", numOfPlanets);
                StdOut.printf("%.2e\n", universeRadius);
                for (int n = 0; n < numOfPlanets; n++) {
                    StdOut.printf("%11.4e %11.4e %11.4e %11.4e %11.4e %12s\n",
            planets[n].x, planets[n].y, planets[n].xVelocity, planets[n].yVelocity, planets[n].mass, planets[n].img);
                }
        }
}
回复

使用道具 举报

🔗
inversion39 2015-7-14 11:42:51 | 只看该作者
全局:
pigeyes 发表于 2015-7-13 09:04
終於debug完畢......
來交作業

现在这个结果还是远离太阳系吗?
回复

使用道具 举报

🔗
pigeyes 2015-7-14 21:37:35 | 只看该作者
全局:
本帖最后由 pigeyes 于 2015-7-14 21:47 编辑
zhugejun 发表于 2015-7-14 11:42
现在这个结果还是远离太阳系吗?

沒有 我後來發現是我在setNetForce method裡面,忘了把netForce歸零,每次都累加,才會有錯誤。
以上的code結果是正確的
回复

使用道具 举报

🔗
pigeyes 2015-7-14 21:45:12 | 只看该作者
全局:
pigeyes 发表于 2015-7-13 22:04
終於debug完畢......
來交作業

C:\Users\User-pc\Desktop\未命名.png

附上作業截圖

未命名.png (274.2 KB, 下载次数: 0)

未命名.png
回复

使用道具 举报

全局:
对于初学者来说,这个作业好难啊。。。Planet还可以,发现之前用enhanced for循环没有考虑要去除自己的因素。
就是NBody 真的很难写,参考了前面几楼的代码。边看边学。

更多图片 小图 大图
组图打开中,请稍候......
回复

使用道具 举报

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

本版积分规则

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