MRaster examples 22.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
juliaM2.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file juliaM2.cpp
5 @author Mitch Richling <https://www.mitchr.me>
6 @brief Draw julia sets on a Mandelbrot Curve.@EOL
7 @std C++20
8 @see https://www.mitchr.me/SS/julia/index.html
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 Moves $c$ around the Mandelbrot set along the 10th Mandelbrot Curve. A GIF may be rendered from the frames like this:
33
34 time convert -delay 1 -loop 0 -dispose previous juliaM2_???.tiff juliaM2.gif
35*/
36/*******************************************************************************************************************************************************.H.E.**/
37/** @cond exj */
38
39//--------------------------------------------------------------------------------------------------------------------------------------------------------------
40#include "ramCanvas.hpp"
41#include "MRMathSTR.hpp"
42
43//--------------------------------------------------------------------------------------------------------------------------------------------------------------
44typedef mjr::ramCanvas3c8b::colorType ct;
45
46//--------------------------------------------------------------------------------------------------------------------------------------------------------------
47int main(void) {
48 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
49 const int JULIA_MAXITR = 1024;
50 const double JULIA_MAXZSQ = 4.0;
51 const int IMGSIZ = 7680/8;
52 const int MANDL_MAXITR = 1024;
53 const double MANDL_MAXZSQ = 4.0;
54 mjr::ramCanvas3c8b manRamCanvas(IMGSIZ, IMGSIZ, -2.2, 0.8, -1.5, 1.5);
55
56 // Read in our curve -- not much error checking here...
57 std::vector<double> mcX;
58 std::vector<double> mcY;
59 char strBuf[256];
60 std::ifstream iStream("../data/mandelbrot_curve_10.txt", std::ios_base::binary);
61 if (!(iStream.good()))
62 return 1;
63 while(iStream.good()) {
64 iStream.getline(strBuf, 100, '\n');
65 std::string strBufS(strBuf);
66 if (strBufS.length() > 0) {
67 std::string::size_type pos;
68 std::string::size_type tagIdx = strBufS.find_first_of("\t ,");
69 mcX.push_back(std::stod(strBufS.substr(0, tagIdx), &pos));
70 mcY.push_back(std::stod(strBufS.substr(tagIdx+1), &pos));
71 }
72 }
73 int numPts = static_cast<int>(mcX.size());
74
75 // Draw a refrence mandelbrot set
76 for(int y=0;y<manRamCanvas.getNumPixY();y++) {
77 for(int x=0;x<manRamCanvas.getNumPixX();x++) {
78 std::complex<double> c = manRamCanvas.int2real(x, y);
79 std::complex<double> z(0.0, 0.0);
80 int count = 0;
81 while((std::norm(z)<MANDL_MAXZSQ) && (count<=MANDL_MAXITR)) {
82 z=std::pow(z, 2) + c;
83 count++;
84 }
85 if(count < MANDL_MAXITR)
86 manRamCanvas.drawPoint(x, y, ct::csCColdeFireRamp::c(static_cast<ct::csIntType>(count*20)));
87 }
88 }
89 // Draw our curve on the refrence mandelbrot set image
90 for(int frame=0; frame<numPts; frame++)
91 manRamCanvas.drawLine(mcX[frame], mcY[frame], mcX[(frame+1)%numPts], mcY[(frame+1)%numPts], "white");
92
93 // Draw frames
94 for(int frame=0; frame<numPts; frame++) {
95 // Draw julia curve frames
96 mjr::ramCanvas3c8b julRamCanvas(IMGSIZ, IMGSIZ, -2.2, 2.2, -2.2, 2.2);
97 std::cout << "Frame: " << frame << " of " << numPts << std::endl;
98 std::complex<double> c = std::complex<double>(mcX[frame], mcY[frame]);
99 for(int y=0;y<julRamCanvas.getNumPixY();y++) {
100 for(int x=0;x<julRamCanvas.getNumPixX();x++) {
101 std::complex<double> z(julRamCanvas.int2realX(x), julRamCanvas.int2realY(y));
102 int count = 0;
103 while((std::norm(z)<JULIA_MAXZSQ) && (count<=JULIA_MAXITR)) {
104 z=std::pow(z, 2) + c;
105 count++;
106 }
107 if(count < JULIA_MAXITR)
108 julRamCanvas.drawPoint(x, y, ct::csCColdeFireRamp::c(static_cast<ct::csIntType>(count*20)));
109 }
110 }
111 // Draw inset image
112 mjr::ramCanvas3c8b setRamCanvas(manRamCanvas);
113 setRamCanvas.drawFillCircle(c, 0.04, "white");
114 setRamCanvas.scaleDownMean(4);
115 julRamCanvas.insertCanvas(setRamCanvas, 0, 0);
116 julRamCanvas.drawRectangle(0, 0, IMGSIZ/4, IMGSIZ/4, "white");
117 // Label the frame
118 julRamCanvas.drawString("MJR 2022", mjr::hershey::font::ROMAN_SL_SANSERIF, IMGSIZ-IMGSIZ/6, 20, "white", 1, 20);
119 // Dump out frame
120 julRamCanvas.writeTIFFfile("juliaM2_" + mjr::math::str::fmt_int(frame, 3, '0') + ".tiff");
121 }
122
123 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
124 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
125}
126/** @endcond */
int main(int argc, char *argv[])