Im using a custom library and so far all I have is the triangle going in circles when you press left or right.
I want it to go the direction its pointing when up is pressed and reverse when down is pressed. Any ideas?
// System header files
#include <iostream>
#include <ctime>
#include <cmath>
#include <iomanip>
using namespace std;
// Project header files
#include "gwin.h"
// Constants
//const double PI = 3.14159265; // inaccurate
//const double PI = acos(0.0)*2.0; // Janet Poliakoff method
const double PI = 3.1415926535897931;
const double DEGREE = 2.0*PI/360.0;
// Classes
class Coord
{
private:
double mX;
double mY;
public:
Coord();
Coord( double pX, double pY );
void clear();
void rotate( double pAngle ); // around 0 by pAngle radians
void rotate( double pAngle, double pX, double pY ); // around (pX, pY) by pAngle radians
void rotate( double pAngle, Coord pPoint ); // around coordinate by pAngle radians
void setXY( double pX, double pY );
double distance( Coord & pCoord ); // between this coordinate and pCoord
double distance( double pX, double pY ); // between this coordinate and (pX, pY)
double getX();
double getY();
int getIntX(); // So that Gwin functions can have an integer
int getIntY(); // So that Gwin function can have an integer
void translate( double pXTrans, double pYTrans ); // by the amount pXTrans and pYTrans
void move( double pDelta, double pAngle ); // move by pDelta in the direction pAngle (in radians)
void display();
};
class Line
{
private:
// member data
Coord mStart;
Coord mEnd;
Colour mColour;
double mAngle; // current angle in degrees
double mDeltaAngle; // change in angle in degrees
public:
// member functions
Line();
Line( double pStartX, double pStartY, double pEndX, double pEndY, Colour pColour );
void set( double pStartX, double pStartY, double pEndX, double pEndY, Colour pColour );
void draw();
void erase();
void display();
Coord startPoint();
Coord endPoint();
double angle(); // returns the angle of the line
void translate( double pXTrans, double pYTrans );
void rotate( double pAngle, Coord pPoint ); // rotate about Coord point pPoint
void rotateClockwise(); // rotate around the start point
void rotateAnticlockwise(); // rotate around the start point
void rotate( double pAngle );
void move( double pDelta ); // move by pDelta amount in the direction given by mAngle
};
class Shape
{
private:
// the graphical object
Line mLine;
Colour mColour;
Coord mCirclePos;
int mRadius;
// the angle of the object
int mAngle; // in degrees
int mDeltaAngle; // in degrees
// the layer information
int mLayerID;
int mLayerWidth;
Coord mLayerPos;
Coord mLayerCentre;
public:
Shape();
void draw();
void rotateClockwise(); // rotate around the centre
void rotateAnticlockwise(); // rotate around the centre
void move( int pDelta ); // move in the direction of mAngle
void show(); // make the layer visible
void hide(); // hide the layer
};
// Function prototypes
int round(double x);
int main()
{
bool required = true;
Key keyValue = ' ';
DWORD start = 0;
int timeInterval= 100; // time interval to check for Blob movement
Shape shape;
Line line1( 100, 100, 300, 300, RED );
// Set up the console and graphics window
Gwin.show(); // display the graphics window
Gwin.clear(); // clear the window - defaults to WHITE
Gwin.showConsole(); // display the console window
// Draw the shape and the line
shape.draw();
line1.draw();
Gwin.refresh(); // make the images appear first time
// Seed the random-number generator with current time so that
// the numbers will be different every time we run.
srand( (int)time( NULL ) );
start = GetTickCount(); // remember the start of this timing period
while ( required )
{
if ((GetTickCount () - start) > DWORD(timeInterval) )
{// the difference between the start of timing and now is greater than the value of timeInterval
start = GetTickCount(); // restart the timing
}
if (Keyboard.kbhit())
{// then they have kit a key so read it
keyValue = Keyboard.getch();
switch( keyValue)
{
case GwinKeyNames::ESCAPE:
// then set the variable to leave the loop
cout << "ESCAPE key pressed." << endl;
required = false;
break;
case 'H':
case 'h':
// Hide the layer
cout << "Hide the layer" << endl;
shape.hide();
break;
case 'S':
case 's':
// Show the layer
cout << "Show the layer" << endl;
shape.show();
break;
case GwinKeyNames::CURSOR_DOWN:
// Move the shape backward
shape.move( 5 );
break;
case GwinKeyNames::CURSOR_UP:
// Move the line forward
shape.move( -5 );
break;
case GwinKeyNames::CURSOR_RIGHT :
// Rotate right
shape.rotateClockwise();
break;
case GwinKeyNames::CURSOR_LEFT:
// Move the layer left
shape.rotateAnticlockwise();
break;
default:
cout << "Unrecognised command: " << keyValue << endl;
break;
}
}
}
// These last lines will just hold the screen until a key is pressed
cout << "Press a key to end ..." << endl << endl;
Keyboard.getch();
return 0;
}
// Coord member functions. These should be in their own .cpp file.
Coord::Coord()
{
clear();
}
Coord::Coord( double pX, double pY )
{
setXY( pX, pY );
}
void Coord::clear()
{
setXY( 0.0, 0.0 );
}
void Coord::rotate( double pAngle )
{// this rotates around the point (0,0) by pAngle radians.
double x;
double y;
// Calculate the new value of x,y
x = (mX * cos( pAngle )) - (mY * sin( pAngle ));
y = (mX * sin( pAngle )) + (mY * cos (pAngle ));
mX = x;
mY = y;
}
void Coord::rotate( double pAngle, double pX, double pY )
{// rotate around the point pX, pY
translate( -pX, -pY );
rotate( pAngle );
translate( pX, pY );
}
void Coord::rotate( double pAngle, Coord pPoint )
{// rotate around the point pPoint
rotate( pAngle, pPoint.getX(), pPoint.getY() );
}
void Coord::setXY( double pX, double pY )
{
mX = pX;
mY = pY;
}
double Coord::getX()
{
return mX;
}
double Coord::getY()
{
return mY;
}
int Coord::getIntX()
{// Gwin requires the coordinates as integers, so this function returns mX as an
// integer. The round function is defined below.
return round( mX );
}
int Coord::getIntY()
{// Gwin requires the coordinates as integers, so this function returns mY as an
// integer. The round function is defined below.
return round( mY );
}
void Coord::translate( double pXTrans, double pYTrans )
{
mX = mX + pXTrans;
mY = mY + pYTrans;
}
void Coord::display()
{
cout << "[" << mX << ", " << mY << "]";
}
void Coord::move( double pDelta, double pAngle )
{// treating pDelta as the length of the hypotenuse.
double deltaX;
double deltaY;
deltaX = pDelta*cos( pAngle );
deltaY = pDelta*sin( pAngle );
translate( deltaX, deltaY );
}
double Coord::distance( Coord & pCoord )
{// Pythagoras' theorom
double dX = mX-pCoord.mX;
double dY = mY-pCoord.mY;
return ( sqrt( (dX*dX) + (dY*dY) ) );
}
double Coord::distance( double pX, double pY )
{// Use the real distance function
Coord coord( pX, pY );
return ( distance( coord ) );
}
// Line member functions
Line::Line()
{
mDeltaAngle = 5.0*DEGREE;
set( 0.0, 0.0, 0.0, 0.0, BLACK );
}
Line::Line( double pStartX, double pStartY, double pEndX, double pEndY, Colour pColour )
{
mDeltaAngle = 5.0*DEGREE;
set( pStartX, pStartY, pEndX, pEndY, pColour );
}
void Line::set( double pStartX, double pStartY, double pEndX, double pEndY, Colour pColour )
{
mStart.setXY( pStartX, pStartY );
mEnd.setXY( pEndX, pEndY );
mColour = pColour;
// Angle of the line this is worked out from the coordinates
mAngle = (90.0*DEGREE)+atan( (pStartX-pEndX)/(pStartY-pEndY) );
}
void Line::draw()
{
Gwin.setPenColour( mColour );
Gwin.moveTo( mStart.getIntX(), mStart.getIntY() );
Gwin.lineTo( mEnd.getIntX(), mEnd.getIntY() );
}
void Line::erase()
{
Gwin.setPenColour( WHITE );
Gwin.moveTo( mStart.getIntX(), mStart.getIntY() );
Gwin.lineTo( mEnd.getIntX(), mEnd.getIntY() );
}
void Line::display()
{
cout << "Start: ";
mStart.display();
cout << " End: ";
mEnd.display();
cout << " Angle: " << mAngle;
}
Coord Line::startPoint()
{
return mStart;
}
Coord Line::endPoint()
{
return mEnd;
}
void Line::translate(double pXTrans, double pYTrans)
{
erase();
mStart.translate( pXTrans, pYTrans );
mEnd.translate( pXTrans, pYTrans );
draw();
}
void Line::rotate( double pAngle, Coord pPodouble )
{// rotate about Coord podouble pPodouble
erase();
mStart.rotate( pAngle, pPodouble );
mEnd.rotate( pAngle, pPodouble );
mAngle = mAngle+pAngle;
}
void Line::rotateClockwise( )
{// rotate clockwise about the line start point by the amount set in mDeltaAngle
rotate( mDeltaAngle );
}
void Line::rotateAnticlockwise( )
{// rotate anticlockwise about the line start point by the amount set in mDeltaAngle
rotate( -mDeltaAngle );
}
void Line::rotate( double pAngle )
{// rotate about the line start point by the amount set in pAngle
erase();
mEnd.rotate( pAngle, mStart );
mAngle = mAngle+pAngle;
}
void Line::move( double pDelta )
{// move by pDelta amount in the direction given by mAngle
erase();
mStart.move( pDelta, mAngle );
mEnd.move( pDelta, mAngle );
}
Shape::Shape()
{
// Set up the layer
mLayerWidth = 51;
mLayerCentre.setXY( 26, 26 );
mLayerPos.setXY( 100, 100 );
// Create the layer
mLayerID = Gwin.layers.newLayer( mLayerWidth, mLayerWidth );
// Move the layer to its position
Gwin.layers(mLayerID).move( mLayerPos.getIntX(), mLayerPos.getIntY() );
// Set up the shape
mDeltaAngle = 5; // how much it rotates in degrees
mAngle = 90; // the starting angle in degrees
mLine.set( mLayerCentre.getIntX(), 5, mLayerCentre.getIntX(), mLayerWidth-5, RED );
mColour = RED;
mRadius = 2;
draw();
}
void Shape::draw()
{
// clear the layer
Gwin.layers(mLayerID).clear();
Gwin.layers(mLayerID).setPenColour( mColour );
Gwin.layers(mLayerID).setFillColour( mColour );
// draw the line
Gwin.layers(mLayerID).moveTo( mLine.startPoint().getIntX(), mLine.startPoint().getIntY());
Gwin.layers(mLayerID).lineTo( mLine.endPoint().getIntX(), mLine.endPoint().getIntY());
// draw the circle
Gwin.layers(mLayerID).circle( mLine.startPoint().getIntX(), mLine.startPoint().getIntY(), mRadius );
Gwin.refresh();
}
void Shape::rotateClockwise( )
{// rotate clockwise about the layer centre point by the amount set in mDeltaAngle
mLine.rotate( mDeltaAngle*DEGREE, mLayerCentre );
mAngle = (mAngle + mDeltaAngle)%360; // don't let it get bigger than 360 degrees
draw();
}
void Shape::rotateAnticlockwise( )
{// rotate anticlockwise about the layer centre point by the amount set in mDeltaAngle
mLine.rotate( -mDeltaAngle*DEGREE, mLayerCentre );
mAngle = (mAngle - mDeltaAngle)%360;
draw();
}
void Shape::move( int pDelta )
{
// recalculate the layer position
mLayerPos.move( pDelta, mAngle*DEGREE );
// Now move the layer
Gwin.layers(mLayerID).move( mLayerPos.getIntX(), mLayerPos.getIntY() );
Gwin.refresh();
}
void Shape::show()
{
Gwin.layers(mLayerID).show();
Gwin.refresh();
}
void Shape::hide()
{
Gwin.layers(mLayerID).hide();
Gwin.refresh();
}
inline int round(double x)
{// A function to round a double floating point number and return it as an int.
if ( x>0.0 )
{
return int( x + 0.5 );
}
else
{
return int( x - 0.5 );
}
}