我是每个人,
我对学习计算机图形算法很感兴趣,而且我做的很有趣 从这里开始锻炼:
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() 方法。有谁知道发生了什么事?
答案 0 :(得分: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
,因此您将超出分配的空间。