Answers for "draw a rectangle and rotate it to a specific angle c++ pdf"

C++
0

draw a rectangle and rotate it to a specific angle c++ pdf

#include <iostream>
#include <cmath>
#include <iomanip>

struct Point{
    double x = 0;
    double y = 0;
    
    Point(double aX = 0, double aY = 0){
        x = aX;
        y = aY;
    }
    
    void translate( double aX, double aY ){
        x += aX;
        y += aY;
    }
    // ANTI-CLOCKWISE ROTATION OF Point ABOUT ORIGIN
    void rotate( double aAngle ){
        double xTemp = x;
        double yTemp = y;
        
        double theta = aAngle * M_PI / 180;
        double c = cos( theta );
        double s = sin( theta );
        
        x = xTemp * c - yTemp * s;
        y = xTemp * s + yTemp * c;
    }
};

std::ostream & operator<<(std::ostream & out, const Point & aPoint){
    out << std::fixed << std::setprecision(2) << "(" << aPoint.x << ", " << aPoint.y << ")";
    return out;
}

int main(){
    
    Point triangle[] = {Point(2,1), Point(3,3), Point(4,2)};
    Point geometric_center(0,0);
    
    for(auto i:triangle){
        geometric_center.x += i.x/3;
        geometric_center.y += i.y/3;
    }
    std::cout << "Geometric center: " << geometric_center << '\n';
    
    for(auto i:triangle){
        std::cout << i << " -> ";
        i.translate(-geometric_center.x, -geometric_center.y);
        i.rotate(90);
        i.translate(geometric_center.x, geometric_center.y);
        
        std::cout << i << '\n';
    }
    return 0;
}
Posted by: Guest on September-08-2021

Code answers related to "draw a rectangle and rotate it to a specific angle c++ pdf"

Browse Popular Code Answers by Language