MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
newton_min_root.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file newton_min_root.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief Draw a Newton Fractical -- color by orbit distance from roots.@EOL
7 @std C++20
8 @see https://www.mitchr.me/SS/newton/index.html#min_root_d
9 @copyright
10 @parblock
11 Copyright (c) 1988-2015, Mitchell Jay Richling <https://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 We do the standard Newton iterations, but we keep track of the minimum distance from the roots by any iterate of the orbit. Then we use the two largest
33 minimum distances to color the pixel. The distances are used to set the r, g, b channels (i.e. distance to root 1 is r, root 2 is g, and root 3 is b).
34 Closer is a brighter color. By using the two largest and omitting the third distance, the color represents how close the orbit got to the roots to which it
35 didn't converge.
36*/
37/*******************************************************************************************************************************************************.H.E.**/
38/** @cond exj */
39
40//--------------------------------------------------------------------------------------------------------------------------------------------------------------
41#include "ramCanvas.hpp"
42
43//--------------------------------------------------------------------------------------------------------------------------------------------------------------
44typedef mjr::ramCanvas3c8b rcT; // The Ram Canvas type we will use
45typedef rcT::colorChanType rcccT; // Color Channel Type
46
47//--------------------------------------------------------------------------------------------------------------------------------------------------------------
48int main(void) {
49 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
50 int MAXITR = 255;
51 double ZROEPS = .0001;
52 double MAXZ = 10;
53 const int IMGSIZ = 7680;
54 std::complex<double> r1( 1.0, 0.0);
55 std::complex<double> r2(-0.5, sin(2*std::numbers::pi/3));
56 std::complex<double> r3(-0.5, -sin(2*std::numbers::pi/3));
57 rcT theRamCanvas(IMGSIZ, IMGSIZ, -2.15, 1.85, -2.0, 2.0);
58
59# pragma omp parallel for schedule(static,1)
60 for(int y=0;y<theRamCanvas.getNumPixY();y++) {
61 for(int x=0;x<theRamCanvas.getNumPixX();x++) {
62 std::complex<double> z = theRamCanvas.int2real(x, y);
63 double minMod1 = std::abs(z-r1);
64 double minMod2 = std::abs(z-r2);
65 double minMod3 = std::abs(z-r3);
66 for(int count=0; count<MAXITR; count++) {
67 double modz = std::abs(z);
68 if ((modz>MAXZ) || (modz<ZROEPS))
69 break;
70 z = z-(z*z*z-1.0)/(z*z*3.0);
71 if(std::abs(z-r1) < minMod1) minMod1 = std::abs(z-r1);
72 if(std::abs(z-r2) < minMod2) minMod2 = std::abs(z-r2);
73 if(std::abs(z-r3) < minMod3) minMod3 = std::abs(z-r3);
74 if ((modz<ZROEPS) || (minMod1<ZROEPS) || (minMod2<ZROEPS) || (minMod3<ZROEPS))
75 break;
76 }
77
78 rcccT r = static_cast<rcccT>((minMod1 < std::min(minMod2, minMod3) ? 100.0 : 720.0*std::log(4.73-minMod1)));
79 rcccT g = static_cast<rcccT>((minMod2 < std::min(minMod1, minMod3) ? 100.0 : 720.0*std::log(4.73-minMod2)));
80 rcccT b = static_cast<rcccT>((minMod3 < std::min(minMod1, minMod2) ? 100.0 : 720.0*std::log(4.73-minMod3)));
81 theRamCanvas.drawPoint(x, y, rcT::colorType(r, g, b));
82 }
83 }
84
85 theRamCanvas.writeTIFFfile("newton_min_root.tiff");
86 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
87 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
88}
89/** @endcond */
int main(int argc, char *argv[])