MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
newton_half.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_half.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief Draw various Newton-like Fracticals.@EOL
7 @std C++20
8 @see https://www.mitchr.me/SS/newton/index.html#nonpoly
9 @copyright
10 @parblock
11 Copyright (c) 1988-2015, 2017, 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*/
31/*******************************************************************************************************************************************************.H.E.**/
32/** @cond exj */
33
34//--------------------------------------------------------------------------------------------------------------------------------------------------------------
35#include "ramCanvas.hpp"
36
37//--------------------------------------------------------------------------------------------------------------------------------------------------------------
38typedef mjr::ramCanvas3c8b rcT; // The Ram Canvas type we will use
39
40//--------------------------------------------------------------------------------------------------------------------------------------------------------------
41/** Enum identifying why iteration stopped */
42enum class whyStopNH { DIVZERO, //!< Divide by zero (zeroTol)
43 TOOBIG, //!< Iterate got too big (> escapeMod)
44 CONVERGEU, //!< Converged in the upper half plane
45 CONVERGEL, //!< Converged in the lower half plane
46 TOOLONG //!< Too many iterations (> MaxCount)
47 };
48
49//--------------------------------------------------------------------------------------------------------------------------------------------------------------
50int main(void) {
51 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
52 constexpr rcT::coordIntType IMXSIZ = 7680;
53 constexpr rcT::coordIntType IMYSIZ = 4320;
54 constexpr double MAXZ = -32.0;
55 constexpr int MAXITR = 64;
56 constexpr double ZROEPS = 0.0001;
57 constexpr int numToKeep = 5;
58 whyStopNH why;
59 rcT::colorType aColor(255, 255, 255);
60 // rcT theRamCanvas(IMXSIZ, IMYSIZ, 4.0, 8.0, -2.0, 2.0);
61 // rcT theRamCanvas(IMXSIZ, IMYSIZ, -0.16, 0.36, -0.22, 0.23);
62 // rcT theRamCanvas(IMXSIZ, IMYSIZ, -0.42, 0.42, -0.2, 0.2);
63 // rcT theRamCanvas(IMXSIZ, IMYSIZ, -4.72, -0.5, -1.0, 1.0);
64 // rcT theRamCanvas(IMXSIZ, IMYSIZ, -3.0, 3.0, -3.0, 3.0);
65 rcT theRamCanvas(std::max(IMXSIZ, IMYSIZ), std::max(IMXSIZ, IMYSIZ), 0.6, 1.1, -0.4, 0.4); // this one is square!
66
67# pragma omp parallel for schedule(static,1)
68 for(int y=0;y<theRamCanvas.getNumPixY();y++) {
69# pragma omp critical
70 std::cout << "Line: " << y << " of " << theRamCanvas.getNumPixY() << std::endl;
71 for(int x=0;x<theRamCanvas.getNumPixX();x++) {
72 std::complex<double> z = theRamCanvas.int2real(x, y);
73
74 std::vector<std::complex<double>> lastZs(numToKeep);
75 rcT::csIntType count = 0;
76 double maxMod = 0.0;
77 while(1) {
78 if (count >= MAXITR) {
79 why = whyStopNH::TOOLONG;
80 break;
81 }
82 // std::complex<double> cz = ((-2.0*sin(2.0*z))-sin(z)+1.0);
83 // std::complex<double> cz = (exp(z)-1.0);
84 // std::complex<double> cz = sin(z);
85 // std::complex<double> cz = cos(z);
86 // std::complex<double> cz = ((2.0*z*cos(z)-z*z*sin(z))*cos(z*z*cos(z)));
87 std::complex<double> cz = ((cos(z)-z*sin(z))*cos(z*cos(z)));
88 if(std::abs(cz) < ZROEPS) {
89 why = whyStopNH::DIVZERO;
90 break;
91 }
92 // z = z-(cos(2.0*z)+cos(z)+z)/cz;
93 // z = z-(exp(z)-z)/cz;
94 // z = z+cos(z)/cz;
95 // z = z-(sin(z)-0.9)/cz;
96 // z=z-sin(z*z*cos(z))/cz;
97 z=z-sin(z*cos(z))/cz;
98 double modz = std::abs(z);
99 lastZs[count%numToKeep] = z;
100 if(modz>maxMod) {
101 maxMod = modz;
102 }
103 if(count >= numToKeep) { // Criteria for convergeance: Last numToKeep z values must be within ZROEPS of each other.
104 bool allEqual = true;
105 for(int i=1; i<numToKeep; i++)
106 if(std::abs(lastZs[0]-lastZs[i])>ZROEPS) {
107 allEqual = false;
108 break;
109 }
110 if(allEqual) {
111 if(std::imag(z) > 0) {
112 //if(std::real(z) > 0) {
113 why = whyStopNH::CONVERGEU;
114 } else {
115 why = whyStopNH::CONVERGEL;
116 }
117 break;
118 }
119 }
120 if((MAXZ>0) && (modz>MAXZ)) {
121 why = whyStopNH::TOOBIG;
122 break;
123 }
124 count++;
125 }
126
127 rcT::csIntType ccol = (2*4*count);
128 rcT::csIntType mcol = ((rcT::csIntType)(2*8*maxMod));
129 switch(why) {
130 case whyStopNH::TOOLONG : theRamCanvas.drawPoint(x, y, rcT::colorType::csCCwicM::c(mcol)); break;
131 case whyStopNH::CONVERGEU : theRamCanvas.drawPoint(x, y, rcT::colorType::csCCwicB::c(mcol+ccol)); break;
132 case whyStopNH::CONVERGEL : theRamCanvas.drawPoint(x, y, rcT::colorType::csCCwicR::c(mcol+ccol)); break;
133 case whyStopNH::TOOBIG : theRamCanvas.drawPoint(x, y, rcT::colorType("black")); break;
134 case whyStopNH::DIVZERO : theRamCanvas.drawPoint(x, y, rcT::colorType::csCCwicC::c(mcol+ccol)); break;
135 }
136 }
137 }
138 theRamCanvas.writeTIFFfile("newton_half.tiff");
139 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
140 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
141}
142/** @endcond */
int main(int argc, char *argv[])