乘法在for循环中不能很好地工作

时间:2015-12-19 10:32:14

标签: c for-loop operators

它不起作用,我做了一切来解决它,但每次我得到相同的答案..我的功能正如我所见的那样好。但是当我试图将它们乘以for循环时,它不起作用,我得到了大量的零..

我一直在努力修复这个超过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;
}

1 个答案:

答案 0 :(得分:2)

您必须将bezierx和朋友定义为返回float,并在main函数之前声明它们,或在main函数之前移动它们的定义。

请注意,您可以简化代码,并且应该使用double精度算术而不是float来免费提高精度。写x*xx*x*x而不是使用pow来表示这些简单的整数幂也更好,以避免精度损失。

仔细阅读表明bezierxbezierybezierz之间没有区别。使用相同的功能。

以下是修改后的版本:

#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个用于最终结果。