MIT 6.837 assignment0代码与笔记

IFS.h

#ifndef _IFS_H_					//宏定义
#define _IFS_H_
#include <stdio.h>
#include "matrix.h"
#include "image.h"


class IFS
{

private:
    char *input_file = NULL;
    FILE *input = NULL;
    int num_transforms = 0;
    Matrix *pm = NULL;
    float *pprobability = NULL;

public:
    IFS(char *i)
    {
        input_file = i;
    }

    ~IFS()
    {
        delete[] pm;									//delete[]
        delete[] pprobability;
    }

    void read_description();								//函数声明和定义

    void render(Image &image, int points, int iterations);		//引用传参
};

#endif

IFS.cpp

#include "IFS.h"
#include<random>
#define N 99

void IFS::read_description()
{
    // open the file
    input = fopen(input_file, "r");
    assert(input != NULL);													//assert

    // read the number of transforms
    fscanf(input, "%d", &num_transforms);

    // < DO SOMETHING WITH num_transforms >

    // read in the transforms
    pprobability = new float[num_transforms];							//new type []
    pm = new Matrix[num_transforms];
    for (int i = 0; i < num_transforms; i++)
    {
        fscanf(input, "%f", &pprobability[i]);
        pm[i].Read3x3(input);
        // < DO SOMETHING WITH probability and m >
    }
    // close the file
    fclose(input);
}

void IFS::render(Image &image, int points, int iterations){
    int width = image.Width();
    int height = image.Height();
    for (int i = 0;i < points;i++){
        float x0 = (float)(rand() % width) / width;                                             // 不强制类型转换就是整型除法了,结果为0
        float y0 = (float)(rand() % height) / height;
        Vec2f vect(x0, y0);
        for(int k = 0;k < iterations; k++){
            float random_float = rand() % (N + 1) / (float)(N + 1);                 
            //有关概率的算法:假设有3个矩阵:1、三个矩阵分别掷一次骰子判断是否执行和2、掷一次骰子判断执行哪个 是不一样的:1有可能执行三个,也可能都不执行,2必执行且必仅执行一个
            float sum_probility = 0.0;
            for(int m = 0;m < num_transforms;m++){
                sum_probility += pprobability[m];
                if(random_float <= sum_probility){
                    pm[m].Transform(vect);
                    break;
                }
            }
        }
        vect.Get(x0, y0);
        x0 *= width;
        y0 *= height;
        x0 = (x0>width||x0<0) ? 0 : x0;
        y0 = (y0>height||y0<0) ? 0 : y0;
        int x1 = x0;
        int y1 = y0;
        Vec3f black_color(0.0, 0.0, 0.0);
        Vec3f green_color(0, 0, 0);                                                   //三个值都不为0的时候为什么显示不出来     改变值的大小也不会有明暗效果
        image.SetPixel(x1, y1, green_color);
    }
}

main.cpp

#include<assert.h>
#include<stdio.h>
#include<string.h>
#include"image.h"
#include"IFS.h"
#include"IFS.cpp"
#include"image.c"
#include"matrix.h"
#include"matrix.c"

int main(int argc, char *argv[]){
    char *input_file = NULL;
    int num_points = 10000;
    int num_iters = 10;
    int size = 100;
    char *output_file = NULL;
    //读命令行参数
    for (int i = 1; i < argc; i++){
        if (!strcmp(argv[i], "-input")){
            i++;
            assert(i < argc);
            input_file = argv[i];
        }
        else if (!strcmp(argv[i], "-points")){
            i++;
            assert(i < argc);
            num_points = atoi(argv[i]);
        }
        else if (!strcmp(argv[i], "-iters")){
            i++;
            assert(i < argc);
            num_iters = atoi(argv[i]);
        }
        else if (!strcmp(argv[i], "-size")){
            i++;
            assert(i<argc);
            size = atoi(argv[i]);
        }
        else if (!strcmp(argv[i], "-output")){
            i++;
            assert(i<argc);
            output_file=argv[i];
        }
        else {
            printf("whoops error with command line argument %d: '%s'\n", i, argv[i]);
            assert(0);
        }
    }
    
    //创建Image实例
    Image myimage(size, size);
    Vec3f ground_color(255.0, 255.0, 255.0);
    myimage.SetAllPixels(ground_color);

    //读IFS描述
    IFS myifs(input_file);
    myifs.read_description();

    //render the IFS to the image
    myifs.render(myimage, num_points, num_iters);

    //save the image
    myimage.SaveTGA(output_file);

    return 0;
}

配置vscode和用vscode实现debug和传参数:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值