1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include <iostream>
#include <cmath>
#include "graph1.h"
using namespace std;
//function prototype
double computeLength(int x1, int y1, int x2, int y2);
void rotateLine(int obj_no, int x1, int y1, double length);
bool moveCannon(int obj_no, int new_x, int new_y);
int main()
{
//variables initialization/declaration
int obj_no = 0;
int x1 = 0;
int y1 = 0;
double length = 0;
double angle = 0.0;
int new_x = 0;
int new_y = 0;
int x2 = 0;
int y2 = 0;
int velocity = 0;
char repeat = 'y';
bool finish = false;
//displayGraphics
displayGraphics();
do
{
displayBMP("cannon_base1.bmp", 0, 454);
//draw barrel
obj_no = drawLine(26, 464, 26, 427, 5);
//Compute length of line
length = computeLength(26, 464, 26, 427);
//Rotate the line
rotateLine(obj_no, 26, 464, length);
//Ask user to repeat
cout << "Repeat Program? (y/n) ";
cin >> repeat;
//Clear the graphics
clearGraphics();
}while ( (repeat == 'y') || (repeat == 'Y') );
return 0;
}
void rotateLine(int obj_no, int x1, int y1, double length)
{
//Rotate one degree cw if left arrow
//Rotate one degree ccw if right arrow
//Press up arrow to quit
const double PI = 3.1415926;
int i = 0;
int new_x = 0;
int new_y = 0;
double angle = 0;
cout << "Enter Angle For Cannon <Between 0 and 90 inclusive>: ";
cin >> angle;
new_x = (cos( (90.0-angle)*PI/180)*length)+26;
new_y = (cos( (90.0-angle)*PI/180)*40)+427;
//Rotate the line
moveObject(obj_no, new_x, new_y);
drawCircle(5, new_x+3, new_y);
do
{
if(right() )
{
angle = 90 - (i);
//Compute x/y coordinate for clock's hand
new_x = cos(angle*PI/180.0)*length;
new_y = sin(angle*PI/180.0)*length;
//Rotate cw
moveObject(obj_no,x1+new_x,y1-new_y);
i++;
}
else if (left())
{
//Rotate ccw
angle = 90 - (i);
//Compute x/y coordinate for clock's hand
new_x = cos(angle*PI/180.0)*length;
new_y = sin(angle*PI/180.0)*length;
//obj_no = drawLine(x1,y1,x2+new_x,y1-new_y,1);
moveObject(obj_no,x1+new_x,y1-new_y);
i--;
}
else if (up())
{
break;
}
}while(true);
}
//Compute length of line
double computeLength(int x1,int y1,int x2,int y2)
{
double len = 0.0;
//Compute length
len = sqrt( pow(x1-x2,2.0) + pow(y1-y2,2.0) );
return (len);
}
|