Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This line allows the compiler to understand the
- * graphics functions
- */
- #include <graphics_lib.h>
- #include <math.h>
- #define XINITIALROCKETPOSITION 135
- #define YINITIALROCKETPOSITION 450
- #define XMARSPOSITION 800
- #define YMARSPOSITION 0
- struct positionVector
- {
- double x;
- double y;
- };
- struct velocityVector
- {
- double x;
- double y;
- };
- void drawBackground();
- void drawRocket(struct positionVector *drawPosition);
- void calculateInitialVelocity(struct velocityVector *initialVelocity);
- void calculateRocketMovement(struct positionVector *position, struct velocityVector *velocity, double rocketTime);
- double pythagoras(struct positionVector rocketPosition, struct positionVector objectPosition);
- double xRocketVelocity, yRocketVelocity;
- struct positionVector rocketPosition, marsPosition;
- struct velocityVector rocketVelocity;
- int main(void)
- {
- double programTime = 0;
- rocketPosition.x = XINITIALROCKETPOSITION;
- rocketPosition.y = YINITIALROCKETPOSITION;
- marsPosition.x = XMARSPOSITION;
- marsPosition.y = YMARSPOSITION;
- double directDistance = pythagoras(rocketPosition, marsPosition);
- double marsRadius = 240.41;
- GFX_InitWindow(800, 600);
- GFX_InitBitmap();
- drawRocket(&rocketPosition);
- calculateInitialVelocity(&rocketVelocity);
- while (directDistance > marsRadius)
- {
- drawRocket(&rocketPosition);
- calculateRocketMovement(&rocketPosition, &rocketVelocity, programTime);
- directDistance = pythagoras(rocketPosition, marsPosition);
- programTime = programTime + 1;
- }
- /* Wait for a user's signal to exit*/
- printf("Please press enter");
- getchar();
- /* remove the display */
- GFX_CloseWindow();
- return 0;
- }
- drawBackground()
- {
- BITMAP background;
- background = GFX_LoadBitmap("background.bmp");
- GFX_DrawBitmap(background, 400, 300);
- GFX_FreeBitmap(background);
- }
- drawRocket(struct positionVector drawPosition)
- {
- GFX_ClearWindow();
- drawBackground();
- BITMAP rocket;
- rocket = GFX_LoadBitmap("rocket.bmp");
- GFX_MakeImageBGTransparent(rocket, 255, 255, 255);
- GFX_DrawBitmap(rocket, drawPosition.x, drawPosition.y);
- GFX_FreeBitmap(rocket);
- GFX_UpdateDisplay();
- }
- calculateInitialVelocity(struct velocityVector *initialVelocity)
- {
- double velocity, angle;
- printf("Please enter in the velocity to launch the rocket at\n");
- scanf("%lf", &velocity);
- printf("Please enter in the angle to launch the rocket at (in degrees)\n");
- scanf("%lf", &angle);
- angle = angle * M_PI / 180;
- *initialVelocity->x = velocity * cos(angle);
- *initialVelocity->y = velocity * sin(angle);
- }
- calculateRocketMovement(struct positionVector *position, struct velocityVector *velocity, double programTime)
- {
- *position->x = XINITIALROCKETPOSITION + *velocity->x * programTime;
- *position->y = YINITIALROCKETPOSITION - *velocity->y * programTime;
- }
- double pythagoras(struct positionVector rocketPosition, struct positionVector objectPosition)
- {
- rocketPosition.x = rocketPosition.x - objectPosition.x;
- rocketPosition.x = pow(rocketPosition.x, 2);
- rocketPosition.y = objectPosition.y - rocketPosition.y;
- rocketPosition.y = pow(rocketPosition.y, 2);
- double distance = sqrt(rocketPosition.x + rocketPosition.y);
- return distance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement