I am working on C graphics program, where I will ask for Projection Angle from end user and then will use that angle to launch the rocket from earth (circle) surface.
But I am not able to do so.
Here what I found on google:
x1 = x + cos(angle) * distance;
y1 = y + sin(angle) * distance;
where x1 y1 are the new pixel position for object.
I tried this but it doesn't seem like working. Also I want rocket to move constantly till the end of screen, but the above code will directly print the object from position A to position B.
Complete Program Code
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <math.h>
#include <stdlib.h>
#include <iostream.h>
#define cld cleardevice()
int _moonRadius = 20, _earthRadius = 40, _marsRadius = 25;
void mars () {
setfillstyle(9, BROWN);
setcolor(BROWN);
circle(getmaxx() - 25, 50, _marsRadius);
floodfill(getmaxx() - 27, 52, BROWN);
}
void moon () {
setfillstyle(9, WHITE);
setcolor(WHITE);
circle(getmaxx()/2, getmaxy()/2, _moonRadius);
floodfill(getmaxx()/2, getmaxy()/2, WHITE);
// Moon's gravitational area
setfillstyle(SOLID_FILL, DARKGRAY);
setcolor(DARKGRAY);
circle(getmaxx()/2, getmaxy()/2, _moonRadius * 5);
}
void earth () {
setfillstyle(9, GREEN);
setcolor(GREEN);
circle(40, getmaxy() - 100, _earthRadius);
floodfill(42, getmaxy() - 102, GREEN);
}
void rocket (int x, int y) {
setcolor(WHITE);
rectangle(x, y - 105, x + 70, y - 95);
}
void rocket_clear (int x, int y) {
setcolor(BLACK);
rectangle(x, y - 105, x + 70, y - 95);
}
void main () {
clrscr();
int angle, speed;
printf("Please provide input parameters.");
printf("Enter projection angle (range from 5 to 90)\n");
scanf("%d", &angle);
printf("Enter projection speed (range from 10 to 100)\n");
scanf("%d", &speed);
int gd=DETECT, gm, i, j, k;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
// Planets and rocket
mars();
moon();
earth();
rocket(80, 550); // let say initial pixel position x = 80, y = 550
// Moving the rocket
// Right now its only moving towards horizontal line, with speed implementation
// Now here I want to implement the angle of projection
for (i = 81; i < getmaxx() + 100; i++) {
// Also I am not sure about this loop's final range, should it go to getmaxx() or some other range
rocket(i, 550);
rocket_clear(i - 1, 550); // 550 is hard coded right now, so rocket will move only horizontally
delay(500 / speed);
}
getch();
}
Need your help guys, please.
(For reference: you can also think of a moving bullet from killer position to the position of person with some angle)
Thanks :)
sin
andcos
functions in radians? – Gerhartsin
andcos
– Contagiongraphics.h
library – Contagionrocket_clear
. You are drawing a rectangle for rocket and then another rectangle with x position - 1 for rocket clear? What behavior are you expecting? – Amora