类声明:
#pragma once #ifndef __NROOKS_HEADER__ #define __NROOKS_HEADER__ #include "sampler.h" class NRooks :public Sampler { public: NRooks(); ~NRooks(); NRooks(const integer samps); NRooks(const integer samps, const integer sets); NRooks(const NRooks& nr); NRooks& operator=(const NRooks& nr); virtual Sampler* clone() const; virtual void generate_samples(); private: void shuffled_x_coordinates(); void shuffled_y_coordinates(); }; #endif
类实现:
#include "pch.h" #include "nrooks.h" NRooks::NRooks() :Sampler() { generate_samples(); } NRooks::~NRooks() {} NRooks::NRooks(const integer samps) :Sampler(samps) { generate_samples(); } NRooks::NRooks(const integer samps, const integer sets) : Sampler(samps, sets) { generate_samples(); } NRooks::NRooks(const NRooks& nr) : Sampler(nr) { generate_samples(); } NRooks& NRooks::operator=(const NRooks& nr) { if (this == &nr) return *this; Sampler::operator=(nr); return *this; } Sampler* NRooks::clone() const { return new NRooks(*this); } void NRooks::generate_samples() { for (integer i = 0; i < nsets; i++) for (integer j = 0; j < nsamples; j++) { Point2 sp((j + random_ldouble()) / nsamples, (j + random_ldouble()) / nsamples); samples.push_back(sp); } shuffled_x_coordinates(); shuffled_y_coordinates(); } void NRooks::shuffled_x_coordinates() { for (integer i = 0; i < nsets; i++) for (integer j = 0; j < nsamples - 1; j++) { integer k = random_integer() % nsamples + i * nsamples; std::swap(samples[i * nsamples + j + 1].x, samples[k].x); } } void NRooks::shuffled_y_coordinates() { for (integer i = 0; i < nsets; i++) for (integer j = 0; j < nsamples - 1; j++) { integer k = random_integer() % nsamples + i * nsamples; std::swap(samples[i * nsamples + j + 1].y, samples[k].y); } }
测试结果图: