Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1.  
  2. import java.io.File;
  3.  
  4. public class Planet {
  5. double myXPos;
  6. double myYPos;
  7. double myXVel;
  8. double myYVel;
  9. double myMass;
  10. String myFileName;
  11.  
  12. double gConst;
  13.  
  14. public Planet(double xp, double yp, double xv, double yv, double mass, String filename) {
  15. this.myXPos = xp;
  16. this.myYPos = yp;
  17. this.myXVel = xv;
  18. this.myYVel = yv;
  19. this.myMass = mass;
  20. this.myFileName = filename;
  21.  
  22. this.gConst = 6.67 * Math.pow(10, -11);
  23. }
  24.  
  25. public Planet(Planet p) {
  26. this.myXPos = p.myXPos;
  27. this.myYPos = p.myYPos;
  28. this.myXVel = p.myXVel;
  29. this.myYVel = p.myYVel;
  30. this.myMass = p.myMass;
  31. this.myFileName = p.myFileName;
  32. }
  33.  
  34. public double calcDistance(Planet otherPlanet) {
  35. double xres = Math.pow(otherPlanet.myXPos - this.myXPos, 2);
  36. double yres = Math.pow(otherPlanet.myYPos - this.myYPos, 2);
  37. return Math.sqrt(xres + yres);
  38. }
  39.  
  40. public double calcForceExertedBy(Planet otherPlanet) {
  41. double force = (gConst * otherPlanet.myMass * this.myMass) / Math.pow(calcDistance(otherPlanet), 2);
  42. return force;
  43. }
  44.  
  45. public double calcForceExertedByX(Planet otherPlanet) {
  46. double dx = otherPlanet.myXPos - this.myXPos;
  47. double dy = otherPlanet.myYPos - this.myYPos;
  48. double radius = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
  49. double force = calcForceExertedBy(otherPlanet);
  50.  
  51. return (force * dx) / radius;
  52. }
  53.  
  54. public double calcForceExertedByY(Planet otherPlanet) {
  55. double dx = otherPlanet.myXPos - this.myXPos;
  56. double dy = otherPlanet.myYPos - this.myYPos;
  57. double radius = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
  58. double force = calcForceExertedBy(otherPlanet);
  59.  
  60. return (force * dy) / radius;
  61. }
  62.  
  63. public double calcNetForceExertedByX(Planet[] planets) {
  64. double total = 0.0;
  65. for (Planet planet : planets) {
  66. if (!this.equals(planet)) {
  67. total += calcForceExertedByX(planet);
  68. }
  69. }
  70. return total;
  71. }
  72.  
  73. public double calcNetForceExertedByY(Planet[] planets) {
  74. double total = 0.0;
  75. for (Planet planet : planets) {
  76. if (!this.equals(planet)) {
  77. total += calcForceExertedByY(planet);
  78. }
  79. }
  80. return total;
  81. }
  82.  
  83. public void update(double seconds, double xforce, double yforce) {
  84. double accelerationx = xforce / myMass;
  85. double accelerationy = yforce / myMass;
  86.  
  87. this.myXVel = myXVel + seconds * accelerationx;
  88. this.myYVel = myYVel + seconds * accelerationy;
  89. this.myXPos = myXPos + seconds * myXVel;
  90. this.myYPos = myYPos + seconds * myYVel;
  91. }
  92.  
  93. public void draw() {
  94. StdDraw.picture(myXPos, myYPos, "images/" + myFileName);
  95. }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement