-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 

这是输出:

我的图像

我没有更改网站上的任何启动代码。我无法弄清楚问题所在。构造函数似乎正确地接受了概率值。值从那里更改为 IFS::printOut() 方法。有谁知道发生了什么?

4

1 回答 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,所以你将超出分配的空间。

于 2013-07-01T19:49:14.693 回答