小车运动在二维平面直角坐标系问题(c语言)

问题遇到的现象和发生背景 有一辆智能小车,最初(时间为0)的位置为(0,0),我们想知道它最后的位置。小车以每小时10公里的速度向北移动(以北为y轴正向,以东为x轴正向)。小车会受到一系列依照时间戳记排序的命令,1表示“向左转”,2表示“向右转”,3表“停止”。每个命令的前面有一个时间戳记,所以我们知道该命令是何时发出的。最后一个命令一定是“停止”。我们另外假设,这辆小车非常灵活,它可以在瞬间转弯。
以下列输入为例。小车在时间为5的时候收到一个“向左转”的命令1,在时间10收到一个“向右转”的命令2,在时间15收到一个“停止”的命令3。那么在最后时间15的时候,小车的位置将在(-50,100)。程序只要求输出小车最后的位置,第一个整数是x坐标,第二个整数是y坐标。
此题该用什么方法求解

用向量计算

初始化速度v(0,10),位置p(0,0)

接到一个命令,计算一下运动时间t,运动p = p + v * t,更新速度。

如果向左转,给v乘上旋转矩阵,theta = pi/2,如果右转,theta= -pi/2。

img

#include <iostream>
#include <math.h>
#define PI 3.14159
using namespace std;

struct Vector2d
{
    int x;
    int y;

    Vector2d(int a,int b):x(a),y(b){}

    Vector2d operator+(const Vector2d& v) {
        return Vector2d(x + v.x, y + v.y);
    }
    Vector2d operator*(float a) {
        return Vector2d(a * x, a * y);
    }
};

void rotate(Vector2d& v,bool isLeft) {
    float theta = PI / 2;
    if (!isLeft)            //右转
        theta = -theta;
    float m11, m12, m21, m22;    //旋转矩阵
    m11 = cos(theta);
    m12 = -sin(theta);
    m21 = sin(theta);
    m22 = cos(theta);

    int x, y;

    x = m11 * v.x + m12 * v.y;
    y = m21 * v.x + m22 * v.y;

    v.x = x;
    v.y = y;
}

int main() {
    Vector2d velocity(0, 10);
    Vector2d position(0, 0);

    int t = 5;
    position = position + velocity * 5;

    rotate(velocity, 1);
    position = position + velocity * 5;

    rotate(velocity, 0);
    position = position + velocity * 5;

    cout << position.x << " " << position.y << endl;    //输出-50 100
}