【问题标题】:How to transform degrees (angle) to facing number如何将度数(角度)转换为面数
【发布时间】:2012-01-26 10:09:19
【问题描述】:

我有一个包含 40 个旋转图像的图像。

图像索引实际上从 0. 0-39 开始。

这是将 0-39 转换为度数的代码

int image_direction = 0; //Can be 0-39
int facing_degrees = (int)(360.0 * (-(image_direction- 10.0))/40.0);
while(facing_degrees < 0)
    facing_degrees += 360;
while (facing_degrees > 360)
    facing_degrees -= 360;

所以是的,它也可以给出负度数以及超过 360 度数。这就是为什么有 2 个 while 循环。

现在我想扭转这个过程说我指定 90 度我想回到 0..

我正在考虑做类似的事情

 if(degrees == 90 || degrees >= 80 && degrees <= 99)
    image_direction = 0;
 elseif(degrees == 100 || degrees >= 91 && degrees <= 109)
    image_direction = 39;
//etc.......

好吧,我数学不好,我忘了这种东西。

我想知道如何反转从 image_direction 给出度数的函数以在一个简单的方程中向后运行,以避免 if 语句的巨大情况。

这是一些结果

Image Index = 0 Image Degree = 90
Image Index = 1 Image Degree = 81
Image Index = 2 Image Degree = 72
Image Index = 3 Image Degree = 63
Image Index = 4 Image Degree = 54
Image Index = 5 Image Degree = 45
Image Index = 6 Image Degree = 36
Image Index = 7 Image Degree = 27
Image Index = 8 Image Degree = 18
Image Index = 9 Image Degree = 9
Image Index = 10 Image Degree = 0
Image Index = 11 Image Degree = 351
Image Index = 12 Image Degree = 342
Image Index = 13 Image Degree = 333
Image Index = 14 Image Degree = 324
Image Index = 15 Image Degree = 315
Image Index = 16 Image Degree = 306
Image Index = 17 Image Degree = 297
Image Index = 18 Image Degree = 288
Image Index = 19 Image Degree = 279
Image Index = 20 Image Degree = 270
Image Index = 21 Image Degree = 261
Image Index = 22 Image Degree = 252
Image Index = 23 Image Degree = 243
Image Index = 24 Image Degree = 234
Image Index = 25 Image Degree = 225
Image Index = 26 Image Degree = 216
Image Index = 27 Image Degree = 207
Image Index = 28 Image Degree = 198
Image Index = 29 Image Degree = 189
Image Index = 30 Image Degree = 180
Image Index = 31 Image Degree = 171
Image Index = 32 Image Degree = 162
Image Index = 33 Image Degree = 153
Image Index = 34 Image Degree = 144
Image Index = 35 Image Degree = 135
Image Index = 36 Image Degree = 126
Image Index = 37 Image Degree = 117
Image Index = 38 Image Degree = 108
Image Index = 39 Image Degree = 99
Image Index = 0 Image Degree = 90
Image Index = 1 Image Degree = 81
Image Index = 2 Image Degree = 72
Image Index = 3 Image Degree = 63
Image Index = 4 Image Degree = 54
Image Index = 5 Image Degree = 45
Image Index = 6 Image Degree = 36
Image Index = 7 Image Degree = 27
Image Index = 8 Image Degree = 18

【问题讨论】:

  • 您能否提供一个包含一些示例映射的列表,例如0 映射到 x 度,1 映射到 y ... 39 映射到 z
  • 我只能说上面的功能完美无缺。我有 40 张图像.. 旋转 360 度。图像 0 处于 90 度,图像 1 可能减少 85 度,图像 2 再次减少 75-70?我不知道.. 但它做了一个完整的圆圈.. 图像 39 是 100 度。顺时针
  • 这不仅对问题很有帮助,而且对您自己的调试将上述代码包装在从 i = 0 到 39 的外观中,并将输入索引和输出度数打印到控制台都会很有帮助。我建议您在继续之前这样做,就好像您不确定代码输出的是什么一样,很难对其进行逆向工程。
  • 不不.. 上述函数产生的值很好,但如果你是这个意思,我不能在数组中使用它们,因为度数是实时变化的。有时它是 90.. 有时是 91.. 我希望它是近似的.. 所以 90 和 91 仍然等于图像 0。现在我考虑到这一点.. 我认为 if 语句是这样做的唯一方法。我将发布所有角度的列表到图像索引。
  • +1 用于输入/输出映射!

标签: c++ math rotation image-rotation degrees


【解决方案1】:

根据您输入到输出的映射,我将它们粘贴到 Excel 中,计算出度数的计算公式如下:

RawDegrees = 90 - (index * 9)

然后将其裁剪为 0...360(使用 while 循环环绕),您将获得最终的度数输出。通过重新排列上述以使 Index 成为主题,可以找到反转此公式的公式

RawDegrees - 90 = - (指数 * 9) --- 1 RawDegrees/9.0 - 10 = -index --- 2 所以 指数 = 10 - 度/9.0

这将为您提供以下映射

学位发现指数 90 0 81 1 72 2 63 3 54 4 45 5 36 6 27 7 18 8 9 9 0 10 351 -29 342 -28 333 -27 324 -26 315 -25 306 -24 297 -23 288 -22 279 -21 270 -20 261 -19 252 -18 243 -17 234 -16 225 -15 216 -14 207 -13 198 -12 189 -11 180 -10 171 -9 162 -8 153 -7 144 -6 135 -5 126 -4 117 -3 108 -2 99 -1 90 0 81 1 72 2 63 3 54 4 45 5 36 6 27 7 18 8

您需要做的就是执行环绕以将答案剪裁为 0..39,例如

index = index < 0 ? index + 40 : index;

获得正确的输出。

顺便说一句,您的度数索引代码可以按如下方式分解以获得相同的输出(对输入 0..39 有效)

int facing_degrees = (int)(90.0 - (image_direction * 9.0));    
facing_degrees = facing_degrees < 0 ? facing_degrees + 360 : facing_degrees;

【讨论】:

  • 不敢相信这是这么简单int index = 10 - Degrees/9.0; 哇,这是一个很好的逻辑,你可以在那里计算出 9.0.. 我确实注意到所有学位都有第一个数字 9,8,7,6,5,4,3,2,1 和第二个数字0,1,2,3,4,5,6,7,8,9 这就是我记忆 9 表乘法的方式。哈哈完全错过了..你似乎从索引 9 到 0 它正在做 9 的乘法。
  • @SSpoke - 对于此类问题,有时查看 Excel 中的数字可以让您大致了解正在发生的事情。我通过观察数字来计算它,注意到每个索引之间的“-9”关系:度映射并使用公式RawDegrees = 90 - (index * 9) 创建一个列来检查它(反复试验)。从那里它只是代数重新排列到解决方案中。 excel中的另一列您可以对此进行测试。 while 循环部分通常用于处理度数以剪辑到 0..360。这不是计算的一部分,而只是标准化 sin/cos/ Cheers 输出的一部分!
  • 非常感谢你,我希望你也一切顺利。
【解决方案2】:

嗯,为什么不使用类似于“百分比公式”的东西稍加调整:

image_direction = (int)(((float)(degrees-90) / 360) * 40) % 40;
if (image_direction < 0)
    image_direction += 40; // for negative integers

分解公式;我们将度数除以 360 并乘以 40 以获得给定度数的正确图像,然后我们使用模运算符将度数 > +360 或

希望这会有所帮助。

编辑:哦,抱歉,我似乎错过了大约 90 度 == 图像索引为 0 的部分,在这里添加很容易,只需从实际度数中减去 90(上面编辑的代码)

【讨论】:

  • 抱歉更多测试结果是逆时针方向?索引 0 的 90 是正确的.. 但是在 100 度时它应该是 39
  • 博士。 Andrew Burnett-Thom 在 1 行中写了解决方案 int image_direction = 10 - degrees/9.0; 我将尝试测试所有案例,看看是否有任何问题。
  • 好吧,你可以添加这个:image_direction = (40 - image_direction) % 40;在上面的代码 sn-p 下方,可以纠正它但似乎有点奇怪,是否没有办法像以前一样重新排列图像以适应算法?如果不只是这样做。
  • 无法重新排列图像我实际上正在使这个模组成为一个游戏,既可以生成图像,也可以读取精灵集的图像。
猜你喜欢
  • 2021-12-03
  • 2011-04-19
  • 2021-01-02
  • 1970-01-01
  • 2020-08-23
  • 1970-01-01
  • 2015-11-28
  • 2015-05-24
相关资源
最近更新 更多