gpt4 book ai didi

c - 自定义角度的旋转点矩阵

转载 作者:太空宇宙 更新时间:2023-11-04 04:35:46 27 4
gpt4 key购买 nike

我想制作点矩阵并将它们旋转自定义角度。
当我输入所需的角度时,我得到了一堆 0,不知道为什么。
可能是一个非常简单的错误,我是 C 语言编程的新手,正在尝试学习。

这是我的代码:

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <math.h>

int main(){
double pi = 3.14159265;
double rot[2][2], obj[2][10], obj2[2][10];
int a, c, d, i, j;
double x,y, sum=0;
double element = 0;

printf("Enter the number of columns of the matrix:\n");
scanf("%d", &a);

printf("Enter the elements of the matrix:\n");

for (c = 0; c < 2; c++){
for (d = 0; d < a; d++){
scanf("%lf", &obj[c][d]);
}
}

printf("Enter the desired clockwise rotation angle:\n");
scanf("%lf", &x);
if ((int)x > 360){
x = (int)x % 360;
}
if ((int)x < 0){
x = 360 + x;
}

y = (pi / 180) * x;

rot[0][0] = cos(y);
rot[0][1] = sin(y);
rot[1][0] = -sin(y);
rot[1][1] = cos(y);


for (c = 0; c < 2; c++){
for (d = 0; d < a; d++){
for (i = 0; i < 2; i++){
sum += rot[c][i] * obj[i][d];
}
obj2[c][d] = sum;
sum = 0;
}
}

printf("Rotated object in matrix form:\n");
for (c = 0; c < 2; c++){
for (d = 0; d < a; d++){
printf("%f\t", obj2[c][d]);
}
printf("\n");
}

system("PAUSE");
return 0;
}

在将值接受到矩阵字段时遇到问题,几乎所有值都是困惑的。 printf 函数中也有 & 所以它打印了错误的东西。现在一切都已解决,调整了一些输出以使其看起来更好,但它现在是一个正常运行的代码。

编辑:修复 printf 后,我得到 http://prntscr.com/7fgq7m调试后。
EDIT2:输入值可以正常工作,现在将使用 sin() 和 cos() 部分来查看是否同样有效。

最终编辑:一切都按照我计划的值顺利运行,将与 friend 进行更多测试,但这看起来不错。
感谢所有提供帮助的人,非常感谢。

最佳答案

scanf("%.lf", &obj[c][d]);

这是错误的。 "%.lf"没有这样的说明符。对于 double 类型值,它必须是 "%lf"。

您还使用 fflush(stdin); 刷新标准输入缓冲区是不正确的,并且根据标准 (C11 7.21.5.2) 这是未定义的行为。 (okey 编译器支持它,但错误)。

你可以使用你自己的函数;

void FlushStdin(void)
{
int ch;
while((ch = getchar()) != '\n' && ch != EOF )
;
}

关于c - 自定义角度的旋转点矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30765146/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com