Advertisement
Guest User

aaaaaaaaaaa

a guest
Feb 28th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.37 KB | None | 0 0
  1. /*  This line allows the compiler to understand the
  2.  
  3. *   graphics functions
  4.  
  5. */
  6. #include <graphics_lib.h>
  7. #include <math.h>
  8.  
  9. #define XINITIALROCKETPOSITION 135
  10. #define YINITIALROCKETPOSITION 450
  11. #define XMARSPOSITION 800
  12. #define YMARSPOSITION 0
  13.  
  14. struct positionVector
  15. {
  16.     double x;
  17.     double y;
  18. };
  19.  
  20. struct velocityVector
  21. {
  22.     double x;
  23.     double y;
  24. };
  25.  
  26.  
  27. void drawBackground();
  28. void drawRocket(struct positionVector *drawPosition);
  29. void calculateInitialVelocity(struct velocityVector *initialVelocity);
  30. void calculateRocketMovement(struct positionVector *position, struct velocityVector *velocity, double rocketTime);
  31. double pythagoras(struct positionVector rocketPosition, struct positionVector objectPosition);
  32.  
  33. double xRocketVelocity, yRocketVelocity;
  34. struct positionVector rocketPosition, marsPosition;
  35. struct velocityVector rocketVelocity;
  36.  
  37.  
  38.  
  39. int main(void)
  40.  
  41. {
  42.     double programTime = 0;
  43.     rocketPosition.x = XINITIALROCKETPOSITION;
  44.     rocketPosition.y = YINITIALROCKETPOSITION;
  45.     marsPosition.x = XMARSPOSITION;
  46.     marsPosition.y = YMARSPOSITION;
  47.     double directDistance = pythagoras(rocketPosition, marsPosition);
  48.     double marsRadius = 240.41;
  49.  
  50.     GFX_InitWindow(800, 600);
  51.     GFX_InitBitmap();
  52.     drawRocket(&rocketPosition);
  53.     calculateInitialVelocity(&rocketVelocity);
  54.     while (directDistance > marsRadius)
  55.     {
  56.         drawRocket(&rocketPosition);
  57.         calculateRocketMovement(&rocketPosition, &rocketVelocity, programTime);
  58.         directDistance = pythagoras(rocketPosition, marsPosition);
  59.         programTime = programTime + 1;
  60.     }
  61.  
  62.  
  63.     /* Wait for a user's signal to exit*/
  64.     printf("Please press enter");
  65.     getchar();
  66.  
  67.  
  68.  
  69.     /* remove the display */
  70.  
  71.     GFX_CloseWindow();
  72.  
  73.  
  74.  
  75.     return 0;
  76.  
  77. }
  78.  
  79. drawBackground()
  80. {
  81.     BITMAP background;
  82.     background = GFX_LoadBitmap("background.bmp");
  83.     GFX_DrawBitmap(background, 400, 300);
  84.     GFX_FreeBitmap(background);
  85. }
  86.  
  87.  
  88. drawRocket(struct positionVector drawPosition)
  89. {
  90.     GFX_ClearWindow();
  91.     drawBackground();
  92.     BITMAP rocket;
  93.     rocket = GFX_LoadBitmap("rocket.bmp");
  94.     GFX_MakeImageBGTransparent(rocket, 255, 255, 255);
  95.     GFX_DrawBitmap(rocket, drawPosition.x, drawPosition.y);
  96.     GFX_FreeBitmap(rocket);
  97.     GFX_UpdateDisplay();
  98. }
  99.  
  100. calculateInitialVelocity(struct velocityVector *initialVelocity)
  101. {
  102.     double velocity, angle;
  103.     printf("Please enter in the velocity to launch the rocket at\n");
  104.     scanf("%lf", &velocity);
  105.     printf("Please enter in the angle to launch the rocket at (in degrees)\n");
  106.     scanf("%lf", &angle);
  107.     angle = angle * M_PI / 180;
  108.     *initialVelocity->x = velocity * cos(angle);
  109.     *initialVelocity->y = velocity * sin(angle);
  110. }
  111.  
  112. calculateRocketMovement(struct positionVector *position, struct velocityVector *velocity, double programTime)
  113. {
  114.     *position->x = XINITIALROCKETPOSITION + *velocity->x * programTime;
  115.     *position->y = YINITIALROCKETPOSITION - *velocity->y * programTime;
  116. }
  117.  
  118. double pythagoras(struct positionVector rocketPosition, struct positionVector objectPosition)
  119. {
  120.     rocketPosition.x = rocketPosition.x - objectPosition.x;
  121.     rocketPosition.x = pow(rocketPosition.x, 2);
  122.     rocketPosition.y = objectPosition.y - rocketPosition.y;
  123.     rocketPosition.y = pow(rocketPosition.y, 2);
  124.     double distance = sqrt(rocketPosition.x + rocketPosition.y);
  125.     return distance;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement