GithubHelp home page GithubHelp logo

dropbox's Introduction

Dropbox

临时收容所

#include <iostream>
#include <cmath>

using namespace std;

void rotatePoint(float& x, float& y, float angle, float x0=0, float y0=0) {
    float ro = angle * M_PI / 180.0f;

    float rx = (x - x0) * cosf(ro) - (y - y0) * sinf(ro);
    float ry = (x - x0) * sinf(ro) + (y - y0) * cosf(ro);

    x = rx;
    y = ry;
}

class Rect {
    public:
        Rect(int w=0, int h=0):width(w),height(h) {}
        void rotate(float angle) {
            float ltx = - width / 2.0f, lty = - height / 2.0f;
            float rtx =   width / 2.0f, rty = - height / 2.0f;
            float rdx =   width / 2.0f, rdy = height / 2.0f;
            float ldx = - width / 2.0f, ldy = height / 2.0f;

            cout << "lt: " << ltx << ", " << lty << endl;
            cout << "rt: " << rtx << ", " << rty << endl;
            cout << "rd: " << rdx << ", " << rdy << endl;
            cout << "ld: " << ldx << ", " << ldy << endl;

            rotatePoint(ltx, lty, angle);
            rotatePoint(rtx, rty, angle);
            rotatePoint(rdx, rdy, angle);
            rotatePoint(ldx, ldy, angle);

            cout << "After" << endl;
            cout << "lt: " << ltx << ", " << lty << endl;
            cout << "rt: " << rtx << ", " << rty << endl;
            cout << "rd: " << rdx << ", " << rdy << endl;
            cout << "ld: " << ldx << ", " << ldy << endl;


            cout << "求出外切矩形" << endl;
            float minx = min(ltx, min(rtx, min(rdx, ldx)));
            float maxx = max(ltx, max(rtx, max(rdx, ldx)));

            float miny = min(lty, min(rty, min(rdy, ldy)));
            float maxy = max(lty, max(rty, max(rdy, ldy)));

            cout << "lt: " << minx << ", " << miny << endl;
            cout << "rt: " << maxx << ", " << miny << endl;
            cout << "rd: " << maxx << ", " << maxy << endl;
            cout << "ld: " << minx << ", " << maxy << endl;

            cout << "移动外切矩形左上角到 0, 0 位置" << endl;
            float dx = -minx, dy = -miny;
            minx += dx; miny += dy;
            maxx += dx; maxy += dy;

            cout << "lt: " << minx << ", " << miny << endl;
            cout << "rt: " << maxx << ", " << miny << endl;
            cout << "rd: " << maxx << ", " << maxy << endl;
            cout << "ld: " << minx << ", " << maxy << endl;

            cout << "移动旋转后的矩形" << endl;
            ltx += dx; lty += dy;
            rtx += dx; rty += dy;
            rdx += dx; rdy += dy;
            ldx += dx; ldy += dy;

            cout << "Final" << endl;
            cout << "lt: " << ltx << ", " << lty << endl;
            cout << "rt: " << rtx << ", " << rty << endl;
            cout << "rd: " << rdx << ", " << rdy << endl;
            cout << "ld: " << ldx << ", " << ldy << endl;

            float ow = (maxx - minx);
            float oh = (maxy - miny);
            float coord[8];
            coord[0] = ltx / ow; coord[1] = lty / oh;
            coord[2] = rtx / ow; coord[3] = rty / oh;
            coord[4] = ldx / ow; coord[5] = ldy / oh;
            coord[6] = rdx / ow; coord[7] = rdy / oh;

            cout << "[1]: " << coord[0] << "," << coord[1] << endl;
            cout << "[2]: " << coord[2] << "," << coord[3] << endl;
            cout << "[3]: " << coord[4] << "," << coord[5] << endl;
            cout << "[4]: " << coord[6] << "," << coord[7] << endl;
        }
        int width, height;
};

int main(void) {
    std::cout << "run test" << std::endl;

    Rect rect(400, 300);
    rect.rotate(45);
    return 0;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.