Homework assignment #1
Develop a new class Location that stores a location of an object in plane coordinates (x and
y). These coordinates should both be floating point numbers. This class should have:
- default constructor;
- copy constructor;
- a constructor that allows to assign initial values of coordinates;
- methods getX() and getY() which allow to get the current position;
- methods setX(), setY, and moveTo() that allow to change
x, y, or both coordinates;
- method moveBy() that takes two variables: the direction and distance to move and changes
the current coordinates accordingly. This method should be overloaded; that is you should implement two different
methods with this name. The first one, takes direction as an integer angle in degrees (from 0 to 360), and the second
one takes one of eight possible values: North, NorthWest, West, SouthWest, South, SouthEast, East, NorthEast.
Please define a special type that can take only these values.
Please also write a function Distance() which is not a member of the class Location, but takes
two arguments of the type const Location& and computes the distance between two given points. Let the function
access the private data members of the class.
You should submit three files:
- location.h that contains definition of the class
- location.cpp that contains implementations of all not inline methods and function Distance()
- loc_main.cpp that contains a small program that tests all the methods of the class.
Needed analytical geometry:
- For two given points (x1, y1) and (x2, y2)
the distance between them is:
D = sqrt( (x1-x2)2 + (y1-y2)2)
- For given point (x, y), angle α and distance d (as shown on the
picture)
the new coordinates (xnew, ynew) are to be computed as
xnew = x + d * cos(α)
ynew = y + d * sin(α)
Please note that your function takes the angle parameter in degrees, but the trigonometric functions
in C++ takes angle in radians. Thus, you need to convert degrees to radians. In order to do that, we need to recall
that 180o is exactly π. Thus
α = angle/180 * π