JavaScript 计算旋转前坐标:基于中心点和角度
要求出旋转 30 度之前的坐标,我们需要知道旋转的中心点坐标和旋转的角度。
假设中心点坐标为 (x0, y0),我们要求的坐标为 (x, y),旋转角度为θ。
旋转前的坐标可以通过下面的公式计算:
x = (x - x0) * cos(θ) - (y - y0) * sin(θ) + x0
y = (x - x0) * sin(θ) + (y - y0) * cos(θ) + y0
其中,cos(θ) 和 sin(θ) 分别代表旋转角度的余弦和正弦值。
在本例中,旋转角度为 30 度,可以使用 JavaScript 中的 Math.cos() 和 Math.sin() 函数来计算:
let x0 = 0; // 中心点 x 坐标
let y0 = 0; // 中心点 y 坐标
let x = 1; // 原始点 x 坐标
let y = 0; // 原始点 y 坐标
let angle = 30; // 旋转角度,单位为度
let radian = angle * Math.PI / 180; // 角度转弧度
let cos = Math.cos(radian); // 旋转角度的余弦值
let sin = Math.sin(radian); // 旋转角度的正弦值
let newX = (x - x0) * cos - (y - y0) * sin + x0; // 旋转前的 x 坐标
let newY = (x - x0) * sin + (y - y0) * cos + y0; // 旋转前的 y 坐标
console.log(newX, newY); // 输出旋转前的坐标
原文地址: https://www.cveoy.top/t/topic/bjXM 著作权归作者所有。请勿转载和采集!