它不起作用,我尽一切努力去修复它,但每次我得到相同的答案。。我的功能很好。但是当我尝试在循环中乘以它们的时候,它就不起作用了,我得到很多的零。。
我一直致力于修复这个超过5天,我不想改变我的数组在函数中,但如果它不修复,那么我删除数组,并添加更多的功能
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main(void) {
int noktalar[4][4][3] = {
{ {30, 50, 1}, {130, 40, 1}, {200, 40, 1}, {240, 45, 1} },
{ {35, 90, 1}, {100, 95, 1}, {220, 95, 1}, {245, 90, 1} },
{ {25, 160, 1}, {80, 170, 1}, {240, 150, 1}, {260, 140, 1} },
{ {30, 250, 1}, {130, 200, 1}, {220, 220, 1}, {300, 300, 1} }};
int i, j, a, b, k = 1;
float X, Y, Z;
float tX = 0, tY = 0, tZ = 0;
for (a = 0; a <= 10; a++) {
for (b = 0; b <= 10; b++) {
for (i = 0; i <= 3; i++) {
for (j = 0; j <= 3; j++) {
X = bezierx(a, b, i, j) * noktalar[i][j][2];
Y = beziery(a, b, i, j) * noktalar[i][j][2]; // this part doesnt work well...
Z = bezierz(a, b, i, j) * noktalar[i][j][2];
}//--j for--
}//--i for--
printf("%d::::: %f -- %f -- %f \n", k, X, Y, Z);
k++;
}// --b for--
}// --a for--
getch();
return 0;
}
// -----------------main-------------------
int bezierx(int a, int b, int i, int j) {
float Xx;
float u = (float)a / 10, w = (float)b / 10;
float Uu = 1 - u, Ww = 1 - w;
float uu[4] = { (float)pow(Uu, 3), (float)(3 * pow(Uu, 2) * u), (float)(3 * Uu * pow(u, 2)), (float)pow(u, 3) };
float ww[4] = { (float)pow(Ww, 3), (float)(3 * pow(Ww, 2) * w), (float)(3 * Ww * pow(w, 2)), (float)pow(w, 3) };
Xx = uu[i] * ww[j];
// printf(" x : %f -- \n ", Xx);
return (float)Xx;
}
int beziery(int a, int b, int i, int j) {
float Yx;
float u = (float)a / 10, w = (float)b / 10;
float Uu = 1 - u, Ww = 1 - w;
float uu[4] = { (float)pow(Uu, 3), (float)(3 * pow(Uu, 2) * u), (float)(3 * Uu * pow(u, 2)), (float)pow(u, 3) };
float ww[4] = { (float)pow(Ww, 3), (float)(3 * pow(Ww, 2) * w), (float)(3 * Ww * pow(w, 2)), (float)pow(w, 3) };
Yx = uu[i] * ww[j];
// printf("y : %f -- ", Yx);
return (float)Yx;
}
int bezierz(int a, int b, int i, int j) {
float Zx;
float u = (float)a / 10, w = (float)b / 10;
float Uu = 1 - u, Ww = 1 - w;
float uu[4] = { (float)pow(Uu, 3), (float)(3 * pow(Uu, 2) * u), (float)(3 * Uu * pow(u, 2)), (float)pow(u, 3) };
float ww[4] = { (float)pow(Ww, 3), (float)(3 * pow(Ww, 2) * w), (float)(3 * Ww * pow(w, 2)), (float)pow(w, 3) };
Zx = uu[i] * ww[j];
// printf("z : %f \n", Zx);
return (float)Zx;
}
最佳答案
必须将bezierx
和friends定义为返回float
并在main
函数之前声明它们,或者在main
函数之前移动它们的定义。
请注意,您可以简化代码,并且应该使用double
精度算法而不是float
来提高精度,而无需任何成本。对于这些简单的整数幂,最好写入x*x
和x*x*x
,而不是使用pow
,以避免精度损失。
仔细阅读表明bezierx
、beziery
和bezierz
之间没有区别。使用相同的功能。
以下是修改版本:
#include <stdio.h>
#include <conio.h>
#include <math.h>
double bezier(int a, int b, int i, int j);
int main(void) {
int noktalar[4][4][3] = {
{ {30, 50, 1}, {130, 40, 1}, {200, 40, 1}, {240, 45, 1} },
{ {35, 90, 1}, {100, 95, 1}, {220, 95, 1}, {245, 90, 1} },
{ {25, 160, 1}, {80, 170, 1}, {240, 150, 1}, {260, 140, 1} },
{ {30, 250, 1}, {130, 200, 1}, {220, 220, 1}, {300, 300, 1} }};
int i, j, a, b, k = 1;
double X, Y, Z;
double tX = 0, tY = 0, tZ = 0;
for (a = 0; a <= 10; a++) {
for (b = 0; b <= 10; b++) {
for (i = 0; i <= 3; i++) {
for (j = 0; j <= 3; j++) {
X = bezier(a, b, i, j) * noktalar[i][j][0];
Y = bezier(a, b, i, j) * noktalar[i][j][1];
Z = bezier(a, b, i, j) * noktalar[i][j][2];
}
}
printf("%d::::: %f -- %f -- %f \n", k, X, Y, Z);
k++;
}
}
getch();
return 0;
}
double bezier(int a, int b, int i, int j) {
double Xx;
double u = a / 10.0, w = b / 10.0;
double Uu = 1 - u, Ww = 1 - w;
double uu[4] = { Uu * Uu * Uu, 3 * Uu * Uu * u, 3 * Uu * u * u, u * u * u };
double ww[4] = { Ww * Ww * Ww, 3 * Ww* Ww * w, 3 * Ww * w * w, w * w * w };
Xx = uu[i] * ww[j];
//printf(" bezier : %f -- \n ", Xx);
return Xx;
}
代码应该进一步简化:
bezier
计算8个系数,其中只有2个用于最终结果。