MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
LevyCurve.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file LevyCurve.cpp
5 @author Mitch Richling http://www.mitchr.me/
6 @brief Draws Levy Curve with the Chaos Game algorithm.@EOL
7 @std C++23
8 @see https://www.mitchr.me/SS/LevyCurveChaosGame/index.html
9 @copyright
10 @parblock
11 Copyright (c) 2024, Mitchell Jay Richling <http://www.mitchr.me/> All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
14
15 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
16
17 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19
20 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
21 without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 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
28 DAMAGE.
29 @endparblock
30 @filedetails
31
32 A chaos game that generates a Levy Curve:
33
34 Start at a random point. Randomly select one of two transformations, and transform the point. Repeat this process with the new point. The two
35 transformations are given by:
36
37 @f[ \begin{array}{ll}
38 T_1(\vec{x}) & = \left[ {\begin{array}{rr}
39 \frac{1}{2} & \frac{1}{2} \\
40 -\frac{1}{2} & \frac{1}{2} \\
41 \end{array} }
42 \right]
43 \cdot\vec{x} \\
44 T_2(\vec{x}) & = \left[ {\begin{array}{rr}
45 \frac{1}{2} & -\frac{1}{2} \\
46 \frac{1}{2} & \frac{1}{2} \\
47 \end{array} }
48 \right]
49 \cdot\vec{x}-
50 \left[ {\begin{array}{rr}
51 \frac{1}{2} \\
52 \frac{1}{2} \\
53 \end{array} }
54 \right]
55 \end{array}
56 @f]
57
58 We can easily expand these transforms into C code with maxima:
59
60 @code{.maxima}
61 A : matrix([.5, .5], [-.5, .5]);
62 B : matrix([.5, -.5], [ .5, .5]);
63 C : matrix([-.5], [-.5]);
64 X : matrix([x], [y]);
65 display2d:false;
66 A.X;
67 matrix([0.5*y+0.5*x],[0.5*y-0.5*x])
68 B.X+C;
69 matrix([(-0.5*y)+0.5*x-0.5],[0.5*y+0.5*x-0.5])
70 @endcode
71*/
72/*******************************************************************************************************************************************************.H.E.**/
73/** @cond exj */
74
75//--------------------------------------------------------------------------------------------------------------------------------------------------------------
76#include "ramCanvas.hpp"
77
78//--------------------------------------------------------------------------------------------------------------------------------------------------------------
79int main() {
80 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
81 mjr::ramCanvas3c8b theRamCanvas(2048, 2048, -1.5, 0.7, -1.6, .6);
82
83 mjr::ramCanvas3c8b::colorType acl;
84 mjr::point2d<double> cpt = {0.0, 0.0};
85 for(int n=0;n<100000;n++) {
86 if (rand()%2) {
87 cpt = { 0.5*cpt.y+0.5*cpt.x, 0.5*cpt.y-0.5*cpt.x };
88 acl = mjr::ramCanvas3c8b::colorType::cornerColorEnum::RED;
89 } else {
90 cpt = { (-0.5*cpt.y)+0.5*cpt.x-0.5, 0.5*cpt.y+0.5*cpt.x-0.5 };
91 acl = mjr::ramCanvas3c8b::colorType::cornerColorEnum::GREEN;
92 }
93 if(n > 100)
94 theRamCanvas.drawPoint(cpt, acl);
95 }
96
97 theRamCanvas.writeTIFFfile("LevyCurve.tiff");
98
99 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
100 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
101
102 return 0;
103}
104/** @endcond */
int main(int argc, char *argv[])