MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
newton_max_back.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_max_back.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#max_back
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 iterations that make a step AWAY from the root to which the orbit eventually converges. Then
33 we color according to the largest such back-step. Near the origin, some backsteps are quite large.
34*/
35/*******************************************************************************************************************************************************.H.E.**/
36/** @cond exj */
37
38//--------------------------------------------------------------------------------------------------------------------------------------------------------------
39#include "ramCanvas.hpp"
40
41//--------------------------------------------------------------------------------------------------------------------------------------------------------------
42typedef mjr::ramCanvas3c8b rcT; // The Ram Canvas type we will use
43typedef rcT::colorChanType rcccT; // Color Channel Type
44
45//--------------------------------------------------------------------------------------------------------------------------------------------------------------
46int main(void) {
47 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
48 constexpr int MAXITR = 255;
49 constexpr double ZROEPS = .0001;
50 constexpr int IMGSIZ = 7680;
51 std::complex<double> r1( 1.0, 0.0);
52 std::complex<double> r2(-0.5, sin(2*std::numbers::pi/3));
53 std::complex<double> r3(-0.5, -sin(2*std::numbers::pi/3));
54 rcT theRamCanvas(IMGSIZ, IMGSIZ, -2.15, 1.85, -2.0, 2.0);
55
56# pragma omp parallel for schedule(static,1)
57 for(int y=0;y<theRamCanvas.getNumPixY();y++) {
58 for(int x=0;x<theRamCanvas.getNumPixX();x++) {
59 std::complex<double> z = theRamCanvas.int2real(x, y);
60 std::complex<double> zL = z;
61 double maxStepBack1 = 0.0;
62 double maxStepBack2 = 0.0;
63 double maxStepBack3 = 0.0;
64 double curRootMod1 = 1.0;
65 double curRootMod2 = 1.0;
66 double curRootMod3 = 1.0;
67 for(int count=0; count<MAXITR; count++) {
68 double modz = std::abs(z);
69 if (modz<ZROEPS)
70 break;
71 z = z-(z*z*z-1.0)/(z*z*3.0);
72 curRootMod1 = std::abs(z-r1);
73 curRootMod2 = std::abs(z-r2);
74 curRootMod3 = std::abs(z-r3);
75 double curStepBack1 = curRootMod1 - std::abs(zL-r1);
76 double curStepBack2 = curRootMod2 - std::abs(zL-r2);
77 double curStepBack3 = curRootMod3 - std::abs(zL-r3);
78 if(curStepBack1 > maxStepBack1) maxStepBack1 = curStepBack1;
79 if(curStepBack2 > maxStepBack2) maxStepBack2 = curStepBack2;
80 if(curStepBack3 > maxStepBack3) maxStepBack3 = curStepBack3;
81 if ((modz<ZROEPS) || (curRootMod1<ZROEPS) || (curRootMod2<ZROEPS) || (curRootMod3<ZROEPS))
82 break;
83 }
84
85 rcccT r = static_cast<rcccT>(( ((curRootMod1 < 2*ZROEPS) && (maxStepBack1 > 0)) ? std::log(1+maxStepBack1)*18 : 0));
86 rcccT g = static_cast<rcccT>(( ((curRootMod2 < 2*ZROEPS) && (maxStepBack2 > 0)) ? std::log(1+maxStepBack2)*18 : 0));
87 rcccT b = static_cast<rcccT>(( ((curRootMod3 < 2*ZROEPS) && (maxStepBack3 > 0)) ? std::log(1+maxStepBack3)*18 : 0));
88 theRamCanvas.drawPoint(x, y, rcT::colorType(r, g, b));
89 }
90 }
91 theRamCanvas.autoMaxHistStrechRGB();
92 theRamCanvas.applyHomoPixTfrm(&rcT::colorType::tfrmPow, 0.4);
93 theRamCanvas.writeTIFFfile("newton_max_back.tiff");
94 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
95 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
96}
97/** @endcond */
int main(int argc, char *argv[])