Write a definition of a class named Point that might be used to store and manipu
ID: 2990530 • Letter: W
Question
Write a definition of a class named Point that might be used to store and manipulate the location of a point in the plane. This program should present a menu with the following options. It should accept both upper and lower case letters for the options, and if what is entered is not one of the options it should print 'That is not a valid option' and display the menu again. The options are as follows:
s: set the coordinated of point
m: move a point
r: rotate a point
q: quit
PLEASE read the entire instructions. This program is different than the others posted on here.
Explanation / Answer
DO RATE if satisfied
/*
* Point_test.cpp
*
* Created on: Oct 31, 2014
* Author: Sivanag
*/
#include <iostream>
using namespace std;
#include <math.h>
/*
*
*/
class Point {
private:
float x, y;
public:
Point(){
x = 0;
y = 0;
}
void set(float x, float y){
this->x = x;
this->y = y;
}
void move(float x, float y){
this->x += x;
this->y += y;
}
void rotate(float degree){
float tx = x, ty = y;
float angle = 3.141592654*degree/180.0;
x = tx*cos(-angle) - ty*sin(-angle);
y = tx*sin(-angle) + ty*cos(-angle);
}
float getX() const {
return x;
}
void setX(float x) {
this->x = x;
}
float getY() const {
return y;
}
void setY(float y) {
this->y = y;
}
};
int requestPoint(){
int ch = 0;
do{
cout << "Which point would you like to select (1-3): " << endl;
cin >> ch;
if(ch >=1 && ch<=3)
return ch;
else
cout<<"Invalid selection of point." <<endl;
}while(1);
return 0;
}
int main(){
Point points[3];
bool exit = false;
char choice;
float x, y, angle;
int pi;
while( !exit ){
cout << "s: set the coordinate of point" << endl;
cout << "m: move point" << endl;
cout << "r: rotate point" << endl;
cout << "q: quit" << endl;
cout << "Select an option: " << endl;
cin >> choice;
switch( choice ){
case 's' :
case 'S' :
pi = requestPoint();
cout << "Enter X, Y seperated by space: ";
cin >> x >> y;
points[pi].set(x, y);
cout << "Point " << pi << " set to ("
<< points[pi].getX() << ", "
<< points[pi].getY() << ")" << endl;
break;
case 'm' :
case 'M' :
pi = requestPoint();
cout << "Point " << pi << " current value = ("
<< points[pi].getX() << ", "
<< points[pi].getY() << ")" << endl;
cout << "Enter X, Y values of movement "
"seperated by space: ";
cin >> x >> y;
points[pi].move(x, y);
cout << "Point " << pi << " moved to ("
<< points[pi].getX() << ", "
<< points[pi].getY() << ")" << endl;
break;
case 'r' :
case 'R' :
pi = requestPoint();
cout << "Point " << pi << " current value = ("
<< points[pi].getX() << ", "
<< points[pi].getY() << ")" << endl;
cout << "Enter angle of rotation:";
cin >> angle;
points[pi].rotate(angle);
cout << "Point " << pi << " moved to ("
<< points[pi].getX() << ", "
<< points[pi].getY() << ")" << endl;
break;
case 'q' :
case 'Q' :
exit = true;
break;
default : cout << "This is not a valid option"<<endl;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.