在原点周围旋转矢量三角形

时间:2017-04-11 14:21:55

标签: c vector trigonometry

我有以下代码试图围绕原点旋转三角形,但由于某种原因,在代码执行完之后,三角形变得扭曲,我实在无法理解。我真的不明白我做错了什么,因为我正在使用raidans,而三角形仍在变形。

shipHeading = 120 shipX = 240 shipY = 240

    int shipX = getShipX();
    int shipY = getShipY();
    int shipHeading = getShipHeading();

    float shipHeadingRads = (shipHeading * (PI / 180));

    screen->setCursor(2,40);
    screen->printf("Shipheading: %i, Shipheadingrads: %f", shipHeading, shipHeadingRads);

    int x1, y1, x2, y2, x3, y3;
    int x1r, y1r, x2r, y2r, x3r, y3r;

    x1 = shipX;
    y1 = shipY - 20;
    x2 = shipX - 10;
    y2 = shipY + 10;
    x3 = shipX + 10;
    y3 = shipY + 10;

    screen->drawTriangle(x1, y1, x2, y2, x3, y3, RED);

    //x_rotated = ((x - x_origin) * cos(angle)) - ((y_origin - y) * sin(angle)) + x_origin
  //y_rotated = ((y_origin - y) * cos(angle)) - ((x - x_origin) * sin(angle)) + y_origin

    x1r = ((x1 - shipX) * cos(shipHeadingRads)) - ((shipY - y1) * sin(shipHeadingRads)) + shipX;
    y1r = ((shipY - y1) * cos(shipHeadingRads)) - ((x1 - shipX) * sin(shipHeadingRads)) + shipY;
    x2r = ((x2 - shipX) * cos(shipHeadingRads)) - ((shipY - y2) * sin(shipHeadingRads)) + shipX;
    y2r = ((shipY - y2) * cos(shipHeadingRads)) - ((x2 - shipX) * sin(shipHeadingRads)) + shipY;
    x3r = ((x3 - shipX) * cos(shipHeadingRads)) - ((shipY - y3) * sin(shipHeadingRads)) + shipX;
    y3r = ((shipY - y3) * cos(shipHeadingRads)) - ((x3 - shipX) * sin(shipHeadingRads)) + shipY;

0 个答案:

没有答案
相关问题