MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
apollony.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file apollony.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief Draw the Apollony Gasket via an ifs.@EOL
7 @keywords apollony fractal gasket
8 @std C++20
9 @see https://www.mitchr.me/SS/AGasket/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 Pick an initial @f$z\in\mathbb{C}@f$, say @f$\frac{1}{10}+\frac{2}{10}i@f$. Then iterate. For each iteration let the next $z$ value be @f$f_n(z)@f$ where
34 @f$n@f$ is selected at random. If we plot the @f$z@f$ points, we obtain the classical Apollony Gasket.
35
36 @f[\begin{array}{rcl}
37 f_1(z) &=& f(z) \\
38 f_2(z) &=& \frac{-1 + i\sqrt{3}}{2f(z)} \\
39 f_3(z) &=& \frac{-1 - i\sqrt{3}}{2f(z)} \\
40 \end{array}@f]
41
42 Where
43
44 @f[ f(z) = \frac{3}{1-z+\sqrt{3}} - \frac{1+\sqrt{3}}{2+\sqrt{3}} @f]
45*/
46/*******************************************************************************************************************************************************.H.E.**/
47/** @cond exj */
48
49//--------------------------------------------------------------------------------------------------------------------------------------------------------------
50#include "ramCanvas.hpp"
51
52//--------------------------------------------------------------------------------------------------------------------------------------------------------------
53int main() {
54 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
55 std::cout << "apollony start" << std::endl;
56 const int CSIZE = 1080*2; // Quad HD
57 const int ITRTOSS = static_cast<int>(std::pow(2, 10)); // Throw away first iterations
58 const long NUMITR = static_cast<int>(std::pow(2, 29)); // Needs to be big
59
60 mjr::ramCanvas3c8b theRamCanvas(CSIZE, CSIZE, -4.0, 4.0, -4.0, 4.0);
61 theRamCanvas.setDrawMode(mjr::ramCanvas3c8b::drawModeType::ADDCLAMP);
62
63#if SUPPORT_DRAWING_MODE
64 mjr::ramCanvas3c8b::colorType aColor[] = { mjr::ramCanvas3c8b::colorType(1, 0, 0),
65 mjr::ramCanvas3c8b::colorType(0, 1, 0),
66 mjr::ramCanvas3c8b::colorType(0, 0, 1) };
67#else
68 mjr::ramCanvas3c8b::colorType aColor[] = { mjr::ramCanvas3c8b::colorType(255, 0, 0),
69 mjr::ramCanvas3c8b::colorType(0, 255, 0),
70 mjr::ramCanvas3c8b::colorType(0, 0, 255) };
71#endif
72
73 std::random_device rd;
74 std::minstd_rand0 rEng(rd()); // Fast is better than high quality for this application.
75
76 const double s = 1.73205080757;
77 const std::complex<double> si(0, s);
78 const std::complex<double> c1 = (1+s)/(2+s);
79 const std::complex<double> c2 = 0.5*(si-1.0);
80 const std::complex<double> c3 = -0.5*(si+1.0);
81 const std::complex<double> c4 = 1.0+s;
82 const std::complex<double> c5(3.0, 0.0);
83 std::complex<double> z(0.1, 0.2);
84 for (long n=0;n<NUMITR;n++) {
85 std::complex<double> zNxt;
86 if ((n % (NUMITR/100)) == 0) {
87 if ((n % (NUMITR/10)) == 0)
88 std::cout << "|" << std::flush;
89 else
90 std::cout << "." << std::flush;
91 }
92 std::complex<double> f = c5/(c4-z)-c1;
93 std::minstd_rand0::result_type rn = rEng()%3;
94 switch (rn) {
95 case 0:
96 zNxt = f; break;
97 case 1:
98 zNxt = c2/f; break;
99 case 2:
100 zNxt = c3/f; break;
101 }
102 z = zNxt;
103 if (n > ITRTOSS)
104 theRamCanvas.drawPoint(z, aColor[rn]);
105 }
106 std::cout << "|" << std::endl;
107 std::cout << "apollony dump" << std::endl;
108 theRamCanvas.applyHomoPixTfrm(&mjr::ramCanvas3c8b::colorType::tfrmStdPow, 1/5.0);
109 theRamCanvas.writeTIFFfile("apollony.tiff");
110 std::cout << "apollony finish" << std::endl;
111 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
112 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
113 return 0;
114}
115/** @endcond */
int main(int argc, char *argv[])