【问题标题】:JS triangle rotating around midpointJS三角形绕中点旋转
【发布时间】:2014-09-29 01:02:22
【问题描述】:

我正在尝试围绕它的中点旋转一个三角形...计算中点并以这种格式存储在一个数组中:
中点 = [midpointX,midpointY];
点的存储方式与 points.a = [x,y] 和 points.b 和 points.b 等的存储方式相同...

这是我用于旋转点 A 的公式例如,由于某种原因返回的值是 null? (附注:rotationAngle 以度为单位)

新 X = (midpoint[0] + (points.a[0]-midpoint[0])*Math.cos(Math.PI*(trig.rotation)/180) - (points.a[1]-midpoint[1])*Math.sin(Math.PI*(trig.rotation)/180))

新 Y = (midpoint[1] + (points.a[1]-midpoint[1])*Math.cos(Math.PI*(rotationAngle)/180) + (points.a[0]-midpoint[0])*Math.sin(Math.PI*(rotationAngle)/180))

编辑:忘了说这是在 ng-attr 中

【问题讨论】:

  • @gandaliter 抱歉忘记了代码标签
  • 奇怪。除非计算中使用的值之一为 null,否则它不应返回 null,并且 Math.sinMath.cos 不能返回 null。您可以尝试将计算与中间变量分开并在每一行测试 null,以查看问题出在哪里。
  • 中点指的是centroid?还是midpoint therom

标签: javascript math svg rotation trigonometry


【解决方案1】:

我发现您的 sn-p 存在一些问题:

  • new 应该是 var:您将计算值存储到 X 和 Y 中。
  • trig.rotation = rotationAngle:如果它们是同一个变量,就选一个。你有X 使用trig.rotationY 使用rotationAngle

我刚刚用var 尝试过这个,当一切都被定义时,它对我来说很好。您应该尝试记录变量以查看是否所有变量都是使用console.log 函数定义的。

console.log(midpoint,trig.rotation,rotationAngle,points.a);
var X = (midpoint[0] + (points.a[0]-midpoint[0])*Math.cos(Math.PI*(trig.rotation)/180) - (points.a[1]-midpoint[1])*Math.sin(Math.PI*(trig.rotation)/180));
var Y = (midpoint[1] + (points.a[1]-midpoint[1])*Math.cos(Math.PI*(rotationAngle)/180) + (points.a[0]-midpoint[0])*Math.sin(Math.PI*(rotationAngle)/180));

【讨论】:

    【解决方案2】:

    问题不在于公式...我使用 ng-attr 进行计算,而这并不需要 Math.cos 或 Math.PI。抱歉没有详细说明=/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-08
      • 2021-05-04
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2019-03-03
      相关资源
      最近更新 更多