帮我看看这个环绕定位计算的问题

如下代码,得到了12个刻度的定位值,但是我不知道怎么加入起始角度,求指点

function calc() {
  const times = 12;
  const posList = [];
  const radius = 200;

  // 角度
  const avd = 360 / times;
  // 弧度
  const ahd = (avd * Math.PI) / 180;

  for (let i = 0; i < times; i++) {
    posList.push({
      left: Math.sin(ahd * i) * radius,
      top: Math.cos(ahd * i) * radius,
    });
  }

  return posList;
}
阅读 1.4k
2 个回答
function calc(start = 0) {
//            ^^^^^^^^^
    const times = 12;
    const posList = [];
    const radius = 200;

    // 角度
    const avd = 360 / times;
    // 弧度
    const ahd = (avd * Math.PI) / 180;

    for (let i = 0; i < times; i++) {
        const x = (start + i) % times;
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        posList.push({
            left: Math.round(Math.sin(ahd * x) * radius * 100) / 100,
//                ^^^^^^^^^^^                           ^^^^^^^^^^^^
            top: Math.round(Math.cos(ahd * x) * radius * 100) / 100,
//               ^^^^^^^^^^^                           ^^^^^^^^^^^^
        });
    }

    return posList;
}

console.log(calc());
console.log(calc(6));
[
  { left: 0, top: 200 },
  { left: 100, top: 173.21 },
  { left: 173.21, top: 100 },
  { left: 200, top: 0 },
  { left: 173.21, top: -100 },
  { left: 100, top: -173.21 },
  { left: 0, top: -200 },
  { left: -100, top: -173.21 },
  { left: -173.21, top: -100 },
  { left: -200, top: -0 },
  { left: -173.21, top: 100 },
  { left: -100, top: 173.21 }
]
[
  { left: 0, top: -200 },
  { left: -100, top: -173.21 },
  { left: -173.21, top: -100 },
  { left: -200, top: -0 },
  { left: -173.21, top: 100 },
  { left: -100, top: 173.21 },
  { left: 0, top: 200 },
  { left: 100, top: 173.21 },
  { left: 173.21, top: 100 },
  { left: 200, top: 0 },
  { left: 173.21, top: -100 },
  { left: 100, top: -173.21 }
]
left: Math.sin((ahd * i + start) % 360) * radius
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题