Hi guys,
Im pretty dam stuck at the moment on an assignment Im meant to do. Its my last question so everything is pretty laid out, but the question (to me is not useful) so if you could just help me out it'd be much appreciated
Ok so what i had to do was "create" a rectangle where 4 co-ordinates were stored mapping out where, on an x, y grid this rectangle is. Then i had to make a way for the user to see where on the grid they are by allowing them to see the current x, y co-ordinate.
Finally they had to be able to move the co-ordinate "absolute" and "relative" in order to be able to figure out where this rectangle lies.
Down below is the code for this and works 100%. It should be fairly easy to understand.
// Point2D_OO.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Point2D.h"
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
Point2D aPoint;
int menu = 0;
while ( menu != 4 )
{
system("cls");
if (aPoint.contains(8, 6, 4, 2) == 1)
{
cout << endl << "You are currently inside the rectangle" << endl << endl;
}
else if ( aPoint.contains(8, 6, 4, 2) == 2)
{
cout << endl << "X is currently inside the rectangle" << endl << endl;
}
else if ( aPoint.contains(8, 6, 4, 2) == 3)
{
cout << endl << "Y is currently inside the rectangle" << endl << endl;
}
else
{
cout << endl << "You are not currently inside the rectangle" << endl << endl;
}
cout << "======Menu======" << endl;
cout << "1. Display X and Y co ordinates" << endl;
cout << "2. Move to a co-ordinate" << endl;
cout << "3. Increment or decrement the X or Y co-ordinate" << endl;
cout << "4. Exit" << endl;
cin >> menu;
if ( menu == 1 )
{
cout << endl << "-----" << endl;
cout <<"x = " << aPoint.printX() << endl;
cout << "y = " << aPoint.printY() << endl;
getch();
}
else if ( menu == 2 )
{
aPoint.moveAbsolute();
}
else if ( menu == 3 )
{
aPoint.moveRelative();
}
}
return 0;
}
//---------------------------------------------------------------------------
now that works how ever what i have to do now is create 2 more classes and move the original one along with these 2 into a header file which is fairly easy and i have done
and the rest of the instructions are as follows:
The previous question is a messy mixture of OO and non-OO code. Tidy up the cody by:
1) implement class Offset and use an offset object as an argument to the member function MoveRelative(). An offset object has two values that are added to the x and y vlaue of a point.
2) Implement class aRectangle and use an aRectangle object as an arguement to the contains method
So I've figured that "2" means that where
if (aPoint.contains(8, 6, 4, 2) == 1)
{
cout << endl << "You are currently inside the rectangle" << endl << endl;
}
else if ( aPoint.contains(8, 6, 4, 2) == 2)
{
cout << endl << "X is currently inside the rectangle" << endl << endl;
}
else if ( aPoint.contains(8, 6, 4, 2) == 3)
{
cout << endl << "Y is currently inside the rectangle" << endl << endl;
}
else
{
cout << endl << "You are not currently inside the rectangle" << endl << endl;
}
is placed something of similar value should be placed in the rectangle class however I am unsure of how this should be done and it might behelpful if some one can point out a path to me.
as for "1" I got NO effing clue on what is going on there.
for simplisity sakes we can use the .h file which i have created for the class to minimize code if you need to change anything in the cpp file just identify the change maybe in bold
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Point2D{
public:
Point2D() {x = 0; y = 0;}
int printX() { return x; }
int printY() { return y; }
int moveRelative()
{
char answer;
int newXY;
cout << endl;
cout << "Do you wish to modify x? (y or n): ";
cin >> answer;
if (answer == 'y')
{
cout << "Please increment or decrement the x co-ordinate: ";
cin >> newXY;
x = x + newXY;
}
else if (answer == 'n')
{
cout << endl;
cout << "Do you wish to modify y? (y or n): ";
cin >> answer;
if (answer == 'y')
{
cout << "Please increment or decrement the y co-ordinate: ";
cin >> newXY;
y = y + newXY;
}
}
else
{
cout << "Unknown answer" << endl;
cout << "Do you wish to modify x? (y or n): ";
cin >> answer;
}
return 0;
}
int moveAbsolute()
{
char answer;
cout << "Do you wish to move to a new starting point? (y or n): ";
cin >> answer;
if (answer == 'y')
{
cout << "Position of x: ";
cin >> x;
cout << "Position of y: ";
cin >> y;
}
return 0;
}
int contains(int topX, int topY, int bottomX, int bottomY)
{
if (x <= topX && x >= bottomX)
{
if (y <= topY && y >= bottomY)
{
return 1;
}
else
{
return 2;
}
}
else if (y <= topY && y >= bottomY)
{
if (x <= topX && x >= bottomX)
{
return 1;
}
else
{
return 3;
}
}
else
{
return 0;
}
}
private:
int x, y;
};
class Offset{
};
class aRectangle {
public:
private:
};