题目链接
题目描述
求解思路
- 因为伸缩和旋转变化相互独立,因此我们可以分别单独记录各自的操作,最后进行叠加。
- 前缀和:创建一个
Operate
类,记录下每次操作的内容以及前面所有操作的叠加结果。 - 需要注意对
operates[0]
的operates[0].prefixOperate
也要进行初始化,因为后面的计算结果需要借助前面的完成。 - 最后再进行旋转变换时,需要注意设置一个
tempx
变量来记录变化前的x
值。
实现代码
import java.util.Scanner;
class Operate {
double k;
double sita;
Operate prefixOperate;
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n, m;
int type;
double value;
n = in.nextInt();
m = in.nextInt();
Operate[] operates = new Operate[n + 1];
operates[0] = new Operate();
operates[0].k = 1.0;
operates[0].sita = 0.0;
operates[0].prefixOperate = new Operate();
operates[0].prefixOperate.k = 1.0;
operates[0].prefixOperate.sita = 0.0;
for (int i = 1; i <= n; i ++) {
type = in.nextInt();
value = in.nextDouble();
operates[i] = new Operate();
operates[i].prefixOperate = new Operate();
if (type == 1) {
operates[i].k = value;
operates[i].sita = operates[i - 1].sita;
operates[i].prefixOperate.k = operates[i - 1].prefixOperate.k * value;
operates[i].prefixOperate.sita = operates[i - 1].prefixOperate.sita;
} else {
operates[i].k = operates[i - 1].k;
operates[i].sita = operates[i - 1].sita + value;
operates[i].prefixOperate.k = operates[i - 1].prefixOperate.k;
operates[i].prefixOperate.sita = operates[i - 1].prefixOperate.sita + value;
}
}
for (int i = 0; i < m; i ++) {
int begin, end;
double x, y;
begin = in.nextInt();
end = in.nextInt();
x = in.nextDouble();
y = in.nextDouble();
Operate operate = new Operate();
operate.k = operates[end].prefixOperate.k / operates[begin - 1].prefixOperate.k;
operate.sita = operates[end].prefixOperate.sita - operates[begin - 1].prefixOperate.sita;
x *= operate.k;
y *= operate.k;
double tempx = x;
x = x * Math.cos(operate.sita) - y * Math.sin(operate.sita);
y = tempx * Math.sin(operate.sita) + y * Math.cos(operate.sita);
System.out.println(x + " " + y);
}
}
}