MRaster examples 21.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
42//--------------------------------------------------------------------------------------------------------------------------------------------------------------
43typedef mjr::ramCanvas3c8b::colorType ct;
44
45//--------------------------------------------------------------------------------------------------------------------------------------------------------------
46int main(void) {
47 std::chrono::time_point<std::chrono::system_clock> startTime = std::chrono::system_clock::now();
48 const int JULIA_MAXITR = 1024;
49 const double JULIA_MAXZSQ = 4.0;
50 const int IMGSIZ = 7680/8;
51 const int MANDL_MAXITR = 1024;
52 const double MANDL_MAXZSQ = 4.0;
53 mjr::ramCanvas3c8b manRamCanvas(IMGSIZ, IMGSIZ, -2.2, 0.8, -1.5, 1.5);
54
55 // Read in our curve -- not much error checking here...
56 std::vector<double> mcX;
57 std::vector<double> mcY;
58 char strBuf[256];
59 std::ifstream iStream("../data/mandelbrot_curve_10.txt", std::ios_base::binary);
60 if (!(iStream.good()))
61 return 1;
62 while(iStream.good()) {
63 iStream.getline(strBuf, 100, '\n');
64 std::string strBufS(strBuf);
65 if (strBufS.length() > 0) {
66 std::string::size_type pos;
67 std::string::size_type tagIdx = strBufS.find_first_of("\t ,");
68 mcX.push_back(std::stod(strBufS.substr(0, tagIdx), &pos));
69 mcY.push_back(std::stod(strBufS.substr(tagIdx+1), &pos));
70 }
71 }
72 int numPts = static_cast<int>(mcX.size());
73
74 // Draw a refrence mandelbrot set
75 for(int y=0;y<manRamCanvas.getNumPixY();y++) {
76 for(int x=0;x<manRamCanvas.getNumPixX();x++) {
77 std::complex<double> c = manRamCanvas.int2real(x, y);
78 std::complex<double> z(0.0, 0.0);
79 int count = 0;
80 while((std::norm(z)<MANDL_MAXZSQ) && (count<=MANDL_MAXITR)) {
81 z=std::pow(z, 2) + c;
82 count++;
83 }
84 if(count < MANDL_MAXITR)
85 manRamCanvas.drawPoint(x, y, ct::csCColdeFireRamp::c(static_cast<ct::csIntType>(count*20)));
86 }
87 }
88 // Draw our curve on the refrence mandelbrot set image
89 for(int frame=0; frame<numPts; frame++)
90 manRamCanvas.drawLine(mcX[frame], mcY[frame], mcX[(frame+1)%numPts], mcY[(frame+1)%numPts], "white");
91
92 // Draw frames
93 for(int frame=0; frame<numPts; frame++) {
94 // Draw julia curve frames
95 mjr::ramCanvas3c8b julRamCanvas(IMGSIZ, IMGSIZ, -2.2, 2.2, -2.2, 2.2);
96 std::cout << "Frame: " << frame << " of " << numPts << std::endl;
97 std::complex<double> c = std::complex<double>(mcX[frame], mcY[frame]);
98 for(int y=0;y<julRamCanvas.getNumPixY();y++) {
99 for(int x=0;x<julRamCanvas.getNumPixX();x++) {
100 std::complex<double> z(julRamCanvas.int2realX(x), julRamCanvas.int2realY(y));
101 int count = 0;
102 while((std::norm(z)<JULIA_MAXZSQ) && (count<=JULIA_MAXITR)) {
103 z=std::pow(z, 2) + c;
104 count++;
105 }
106 if(count < JULIA_MAXITR)
107 julRamCanvas.drawPoint(x, y, ct::csCColdeFireRamp::c(static_cast<ct::csIntType>(count*20)));
108 }
109 }
110 // Draw inset image
111 mjr::ramCanvas3c8b setRamCanvas(manRamCanvas);
112 setRamCanvas.drawFillCircle(c, 0.04, "white");
113 setRamCanvas.scaleDownMean(4);
114 julRamCanvas.insertCanvas(setRamCanvas, 0, 0);
115 julRamCanvas.drawRectangle(0, 0, IMGSIZ/4, IMGSIZ/4, "white");
116 // Label the frame
117 julRamCanvas.drawString("MJR 2022", mjr::hershey::font::ROMAN_SL_SANSERIF, IMGSIZ-IMGSIZ/6, 20, "white", 1, 20);
118 // Dump out frame
119 julRamCanvas.writeTIFFfile("juliaM2_" + mjr::math::str::fmt_int(frame, 3, '0') + ".tiff");
120 }
121
122 std::chrono::duration<double> runTime = std::chrono::system_clock::now() - startTime;
123 std::cout << "Total Runtime " << runTime.count() << " sec" << std::endl;
124}
125/** @endcond */
int main(int argc, char *argv[])