MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
chaos_game.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file chaos_game.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief Play the chaos Game.@EOL
7 @keywords fractal ifs
8 @std C++20
9 @see https://www.mitchr.me/SS/ChaosGame/index.html
10 @copyright
11 @parblock
12 Copyright (c) 1988-2015,2017, Mitchell Jay Richling <https://www.mitchr.me> All rights reserved.
13
14 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
15
16 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
17
18 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
19 and/or other materials provided with the distribution.
20
21 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software
22 without specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 DAMAGE.
30 @endparblock
31 @filedetails
32
33 See: https://en.wikipedia.org/wiki/Chaos_game
34*/
35/*******************************************************************************************************************************************************.H.E.**/
36/** @cond exj */
37
38//--------------------------------------------------------------------------------------------------------------------------------------------------------------
39#include "ramCanvas.hpp"
40
41//--------------------------------------------------------------------------------------------------------------------------------------------------------------
42struct ifs {
43 long itrMax;
44 long itrToss;
45 int numPts;
46 int lastAvoidForward;
47 int lastAvoidBackward;
48 int lastAvoidLast;
49 double factor;
50 double radius;
51};
52
53//--------------------------------------------------------------------------------------------------------------------------------------------------------------
54std::vector<ifs> ifsList {
55 { 10000000, 1000, 3, -1, -1, -1, 2.0, 1.10},
56 {100000000, 1000, 4, -1, 1, -1, 2.0, 1.10},
57 {100000000, 1000, 4, -1, 2, -1, 2.0, 1.10},
58 {100000000, 1000, 4, -1, -1, 1, 2.0, 1.10},
59 {100000000, 1000, 4, -1, -1, -1, 2.0, 1.10},
60 {100000000, 1000, 4, -1, -1, -1, 2.3, 1.10},
61 {100000000, 1000, 5, 1, 1, -1, 2.0, 1.10},
62 {100000000, 1000, 5, 2, 2, -1, 2.0, 1.10},
63 {100000000, 1000, 5, -1, 1, 1, 2.0, 0.75},
64 {100000000, 1000, 5, -1, 1, -1, 2.0, 1.10},
65 {100000000, 1000, 5, -1, 2, -1, 2.0, 1.10},
66 {100000000, 1000, 5, -1, -1, 1, 2.0, 0.84},
67 {100000000, 1000, 5, -1, -1, 1, 2.0, 1.10},
68 {100000000, 1000, 5, -1, -1, 1, 2.3, 0.70},
69 {100000000, 1000, 5, -1, -1, -1, 2.0, 1.10},
70 {100000000, 1000, 5, -1, -1, -1, 2.3, 1.10},
71 {100000000, 1000, 6, 2, 2, -1, 2.0, 1.10},
72 {100000000, 1000, 6, 3, 1, -1, 2.0, 1.10},
73 {100000000, 1000, 7, 1, -1, 1, 2.5, 0.65},
74 {100000000, 1000, 7, 2, -1, 1, 2.5, 0.65},
75 {100000000, 1000, 7, 3, 3, 1, 2.5, 0.65},
76 {100000000, 1000, 7, 3, -1, 1, 2.5, 0.65},
77 {100000000, 1000, 7, 5, 5, 1, 2.5, 0.65},
78 {100000000, 1000, 7, 5, -1, 1, 2.5, 0.65},
79 {100000000, 1000, 8, 1, 1, 1, 2.0, 0.75},
80 {100000000, 1000, 8, 1, 1, -1, 2.0, 1.01},
81 {100000000, 1000, 8, 2, 2, 1, 2.0, 0.94},
82 {100000000, 1000, 8, 3, 3, 1, 2.0, 0.94},
83 {100000000, 1000, 8, 5, 1, 1, 2.0, 0.94},
84 {100000000, 1000, 8, 5, 2, 1, 2.0, 0.94},
85 {100000000, 1000, 8, 5, 5, 1, 2.0, 0.94},
86 {100000000, 1000, 10, 1, 1, 1, 2.5, 0.57},
87 {100000000, 1000, 10, 1, 9, 1, 2.5, 0.65},
88 {100000000, 1000, 10, 2, 2, 1, 2.5, 0.65},
89 {100000000, 1000, 10, 3, 3, 1, 2.5, 0.65},
90 {100000000, 1000, 10, 4, 4, 1, 2.5, 0.65},
91 {100000000, 1000, 10, 5, 5, 1, 2.5, 0.65},
92 {100000000, 1000, 10, 6, 1, 1, 2.5, 0.65},
93 {100000000, 1000, 10, 6, 6, 1, 2.5, 0.65},
94 {100000000, 1000, 10, 7, 7, 1, 2.5, 0.65},
95 {100000000, 1000, 10, 8, 8, 1, 2.1, 0.87},
96 {100000000, 1000, 10, 8, 8, 1, 2.3, 0.74},
97 {100000000, 1000, 10, 8, 8, 1, 2.5, 0.65},
98 {100000000, 1000, 15, 5, 5, 1, 3.8, 0.36}
99};
100
101//--------------------------------------------------------------------------------------------------------------------------------------------------------------
102int main() {
103 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
104 mjr::ramCanvas3c8b theRamCanvas(2160, 2160);
105 theRamCanvas.setDrawMode(mjr::ramCanvas3c8b::drawModeType::ADDCLAMP);
106 std::vector<mjr::ramCanvas3c8b::colorType> aColor{ mjr::ramCanvas3c8b::colorType(1, 1, 1)};
107 auto numCol = aColor.size();
108
109 std::random_device rd;
110 std::minstd_rand0 rEng(rd());
111
112 for(auto &theIFS :ifsList) {
113 theRamCanvas.newRealCoords(-theIFS.radius, theIFS.radius, -theIFS.radius, theIFS.radius);
114 theRamCanvas.clrCanvasToBlack();
115
116 std::vector<std::complex<double>> points;
117 double curAngle = std::numbers::pi/2;
118 for(int i=0; i<theIFS.numPts; i++) {
119 points.push_back(std::complex<double>{cos(curAngle), sin(curAngle)});
120 curAngle += 2*std::numbers::pi/theIFS.numPts;
121 }
122
123 std::complex<double> z{0.1, 0.2};
124 int lastPoint = -1;
125 double maxMag = 0;
126 for(int n=0;n<theIFS.itrMax;n++) {
127 std::minstd_rand0::result_type rn = rEng();
128 int rpi = static_cast<int>(rn % theIFS.numPts);
129 while(((lastPoint !=-1) && (theIFS.lastAvoidLast !=-1) && (rpi==lastPoint)) ||
130 ((lastPoint !=-1) && (theIFS.lastAvoidBackward!=-1) && (rpi==(lastPoint+theIFS.lastAvoidBackward)%theIFS.numPts)) ||
131 ((lastPoint !=-1) && (theIFS.lastAvoidForward !=-1) && ((rpi+theIFS.lastAvoidForward)%theIFS.numPts==lastPoint))) {
132 rn = rEng();
133 rpi = static_cast<int>(rn % theIFS.numPts);
134 }
135 lastPoint = rpi;
136 z = (z+points[rpi])/theIFS.factor;
137 if(n > theIFS.itrToss)
138 theRamCanvas.drawPoint(z, aColor[rn%numCol]);
139 double curMag = std::abs(z);
140 if(maxMag < curMag)
141 maxMag = curMag;
142 }
143 std::cout << "max mag: " << maxMag << std::endl;
144
145 theRamCanvas.applyHomoPixTfrm(&mjr::ramCanvas3c8b::colorType::tfrmStdPow, 1.0/5.0);
146
147 std::ostringstream stringStream;
148 stringStream << "chaos_game";
149 stringStream << "," << theIFS.numPts;
150 stringStream << "," << theIFS.lastAvoidForward;
151 stringStream << "," << theIFS.lastAvoidBackward;
152 stringStream << "," << theIFS.lastAvoidLast;
153 stringStream << "," << theIFS.factor;
154 stringStream << ".tiff";
155 theRamCanvas.writeTIFFfile(stringStream.str());
156 }
157
158 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
159 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
160
161 return 0;
162}
163/** @endcond */
int main(int argc, char *argv[])