You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过纯数学方法将压扁生成的椭圆按指定角度旋转

实现方案

核心思路是直接使用二维旋转变换矩阵对椭圆上的每个点做坐标转换,完全不需要依赖任何绘图框架的内置旋转能力。

基础原理

平面上任意一点 (x, y) 绕原点逆时针旋转α弧度后的坐标(x', y')计算公式为:

x' = x * cos(α) - y * sin(α)
y' = x * sin(α) + y * cos(α)

如果需要顺时针旋转,直接把α取负值代入即可。

完整实现代码

首先注意你原有代码中circle_y行缺少右括号,修复后结合旋转逻辑的完整实现如下:

// 基础参数配置
const circSize = 15;
const aspectAdd = 10;
const rotateRad = Math.PI / 4; // 旋转角度(弧度制,示例为逆时针旋转45度)
const centerX = 0; // 椭圆中心X坐标,可按需修改
const centerY = 0; // 椭圆中心Y坐标,可按需修改

// 传入参数t(0~2π范围)即可获取旋转后椭圆路径上的对应点
function getEllipsePoint(t) {
    // 计算原始未旋转的椭圆点坐标
    const originX = (circSize + aspectAdd) * Math.cos(t);
    const originY = circSize * Math.sin(t);
    // 预计算三角函数避免重复计算
    const cosA = Math.cos(rotateRad);
    const sinA = Math.sin(rotateRad);
    // 应用旋转变换
    const rotX = originX * cosA - originY * sinA;
    const rotY = originX * sinA + originY * cosA;
    // 偏移到实际中心位置返回
    return {
        x: rotX + centerX,
        y: rotY + centerY
    }
}

// 替换你原有获取circle_x、circle_y的逻辑即可
const t = 2 * Math.PI; // 对应你原代码中的circArc参数
const {x: circle_x, y: circle_y} = getEllipsePoint(t);

扩展说明

如果你的椭圆中心不是默认原点,只需要修改centerX、centerY两个参数即可,逻辑内部已经自动处理了平移、旋转、复位的完整流程。


内容的提问来源于stack exchange,提问作者Ludo Timp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 06:54:05