2013-07-01 97 views
-1

我大家,浮動值改變

我感興趣的學習計算機圖形算法,我在這裏做一個有趣的小 鍛鍊:

http://groups.csail.mit.edu/graphics/classes/6.837/F03/assignments/assignment0/index.html

不過,我有麻煩我IFS類正確地取浮點值 。下面是接口:

#ifndef _IFS_H_ 
#define _IFS_H_ 
#include "matrix.hpp" 
#include "Image.hpp" 

class IFS 
{ 
public: 
    IFS(const char* filename); 
    ~IFS(); 

    void render(Image& img, int numPoint, int numIterations); 
    void printOut(); 

private: 
    int num_transforms;   // number of transformations 
    Matrix* matrices; 
    float* probablility; 

}; 

#endif 

這裏是實現:

#include "IFS.h" 
#include <iostream> 

IFS::IFS(const char* filename){ 
    FILE* input = fopen(filename,"r"); 
    assert(input != NULL); 

    fscanf(input,"%d",&num_transforms); 

    matrices = new Matrix[num_transforms-1];  
    probablility = new float[num_transforms-1]; 

    float temp; 
    for (int i = 0; i < num_transforms; i++) { 
     fscanf (input,"%f",&temp); 
     std::cout << temp << std::endl; 
     probablility[i] = temp; 
     std::cout << probablility[i] << std::endl; 
     matrices[i].Read3x3(input); 
    } 

    fclose(input); 
} 

IFS::~IFS(){ 
    delete matrices; 
    delete probablility; 
} 

void IFS::printOut(){ 
    std::cout << "Number of transformsations: " << num_transforms << std::endl; 
    std::cout << probablility[0] << std::endl; 

    for(int i = 0; i < num_transforms; i++){ 
     std::cout << "Number of iterations: " << i << std::endl; 
     std::cout << probablility[i] << std::endl; 
     matrices[i].Write3x3(); 
    } 
} 

void IFS::render(Image& img, int numPoint, int numIterations){ 
    std::cout << "In Progress" << std::endl; 
} 

這裏是我的主要功能:

#include <cstdio> 
#include <cstdlib> 
#include <iostream> 

#define HEIGHT 480 
#define WIDTH 640 

#include "image.hpp" 
#include "matrix.hpp" 
#include "vectors.hpp" 
#include "utils.h" 
#include "IFS.h" 


int main(void){ 
    IFS myIFS("dragon.txt"); 
    myIFS.printOut(); 

    std::cout << "Hello world" << std::endl; 
    getchar(); 
    return 0; 
} 

這裏是dragon.txt:

2 
0.5 
0.500124 0.499725 -0.250062 
-0.499725 0.500124 0.249863 
0.000000 0.000000 1.000000 
0.5 
-0.499327 0.500521 0.749664 
-0.500521 -0.499327 0.750261 
0.000000 0.000000 1.000000 

這裏是輸出:

myImage

我還沒有更改網站上的任何啓動代碼。 我找不出這個問題。構造函數似乎正確地取得了 的概率值。值從那裏改變到IFS :: printOut() 方法。有誰知道發生了什麼事?

+0

請勿使用TAB。並請格式化和簡化您發佈的代碼:拋棄'_IFS_H_'的'#ifndef'&'#define',刪除無用的空行,...然後,更詳細地描述您的問題,因爲我不清楚您的意思(並且不要指望別人分析你的代碼來尋找意義)。 –

+1

-1用於附加** text **的屏幕截圖。 –

+1

這個問題沒有顯示足夠的努力來縮小問題的範圍,並且簡潔。 –

回答

2

您沒有在這些陣列中分配足夠的空間。

matrices = new Matrix[num_transforms-1];  
probablility = new float[num_transforms-1]; 

您已經分配的空間num_transforms-1元素,但你有這樣的代碼:

for (int i = 0; i < num_transforms; i++) { 
    fscanf (input,"%f",&temp); 
    std::cout << temp << std::endl; 
    probablility[i] = temp; 
    std::cout << probablility[i] << std::endl; 
    matrices[i].Read3x3(input); 
} 

你在哪裏初始化num_transforms,所以你將要超越分配的空間。