dimanche 1 mars 2015

How to write a C++ constructor that rotates a rectangle?


I am trying to write a constructor that takes in a point, width and height of the rectangle, and an angle. But I couldn't figure our a way of doing it. I tried several ways but they don't work. Here's what I've got so far:



Rectangle::Rectangle(){
corner1 = Point(0,0);
corner2 = Point(0,-1);
corner3 = Point(1,-1);
corner4 = Point(1,0);
} //default constructor

Rectangle::Rectangle(Point c, double w, double h){
corner1 = c;
corner2 = Point(c.get_x(),c.get_y()-h);
corner3 = Point(c.get_x()+w,c.get_y()-h);
corner4 = Point(c.get_x()+w,c.get_y());
}

Rectangle::Rectangle(Point c, double w, double h, double a){
} //the part that's troubling me


I've also written these, which could be helpful (but I'm not sure how to use them):



void Rectangle::rotate(double a){ //rotate a rectangle
double xmove = get_center().get_x();
double ymove = get_center().get_y();
move(-xmove,-ymove);

rotatePoint(corner1,a);
rotatePoint(corner2,a);
rotatePoint(corner3,a);
rotatePoint(corner4,a);

move(xmove,ymove);
return;
} //if somehow I can use this it would be great

void Rectangle::rotatePoint(Point& p, double a){
double new_x = p.get_x()*cos(a*PI/180)-p.get_y()*sin(a*PI/180);
double new_y = p.get_x()*sin(a*PI/180)+p.get_y()*cos(a*PI/180);
p = Point(new_x,new_y);
return;
}


Thank you very much.




Aucun commentaire:

Enregistrer un commentaire