00001 00006 /* 00007 Copyright (C) 2004 Anders Hedstrom 00008 00009 This program is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU General Public License 00011 as published by the Free Software Foundation; either version 2 00012 of the License, or (at your option) any later version. 00013 00014 This program is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU General Public License for more details. 00018 00019 You should have received a copy of the GNU General Public License 00020 along with this program; if not, write to the Free Software 00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00022 */ 00023 #include <stdincl.h> 00024 00025 #include "Globals.h" 00026 #include "mandel.h" 00027 00028 00029 int mandel_color(double x,double y) 00030 { 00031 double cx,cy; 00032 double limit; 00033 double ax,ay; 00034 double a1,b1; 00035 double lp; 00036 double a2,b2; 00037 00038 cx = -0.3 + Globals::m_zx; 00039 cy = -0.66 + Globals::m_zy; 00040 // Globals::m_scale = 0.002; 00041 limit = 4; 00042 00043 // {What is the * mathematical * value of this point?} 00044 ax = cx + (x) * Globals::m_scale; 00045 ay = cy + (y) * Globals::m_scale; 00046 00047 // {And now for the magic formula!} 00048 a1 = ax; 00049 b1 = ay; 00050 lp = 0; 00051 #define STOPLIM 40 00052 do 00053 { 00054 // {Do one iteration.} 00055 lp = lp + 1; 00056 a2 = a1 * a1 - b1 * b1 + ax; 00057 b2 = 2 * a1 * b1 + ay; 00058 // {This is indeed the square of a + b1, done component-wise.} 00059 a1 = a2; 00060 b1 = b2; 00061 } while (!(lp > STOPLIM || ((a1 * a1) + (b1 * b1) > limit))); 00062 // {The first condition is satisfied if we have convergence. 00063 // The second is satisfied if we have divergence.} 00064 00065 // {Define colour and draw pixel.} 00066 if (lp > STOPLIM) 00067 { 00068 lp = STOPLIM; 00069 } 00070 // {If the point converges, it is part of the brot and we 00071 // draw it with colour 0, or black.} 00072 // pixel(x + 160,y + 100,lp); 00073 return (int)lp; 00074 } 00075 00076 00077 void mandel_set() 00078 { 00079 double x,y; 00080 double cx,cy; 00081 double limit; 00082 double ax,ay; 00083 double a1,b1; 00084 double lp; 00085 double a2,b2; 00086 00087 // {Set up initial values for drawing. Try compiling the program 00088 // with different values here if you like!} 00089 cx = 0; 00090 00091 cy = 0; 00092 00093 // Globals::m_scale = 0.02; 00094 limit = 4; 00095 00096 // {Loop through all pixels on screen. For reasons that will become 00097 // clear, I am counting not from (0,0) but from (-160,-100).} 00098 for (x = -160; x < 160; x++) 00099 { 00100 for (y = -100; y <= 100; y++) 00101 { 00102 // {What is the * mathematical * value of this point?} 00103 ax = cx + x * Globals::m_scale; 00104 00105 ay = cy + y * Globals::m_scale; 00106 00107 // {And now for the magic formula!} 00108 a1 = ax; 00109 00110 b1 = ay; 00111 00112 lp = 0; 00113 do 00114 { 00115 // {Do one iteration.} 00116 lp = lp + 1; 00117 a2 = a1 * a1 - b1 * b1 + ax; 00118 b2 = 2 * a1 * b1 + ay; 00119 // {This is indeed the square of a + bi, done component-wise.} 00120 a1 = a2; 00121 00122 b1 = b2; 00123 } while (lp > 255 || ((a1 * a1) + (b1 * b1) > limit)); 00124 // {The first condition is satisfied if we have convergence. 00125 // The second is satisfied if we have divergence.} 00126 00127 // {Define colour and draw pixel.} 00128 if (lp > 255) 00129 { 00130 lp = 0; 00131 } 00132 // {If the point converges, it is part of the brot and we 00133 // draw it with colour 0, or black.} 00134 // pixel(x + 160,y + 100,lp); 00135 } 00136 00137 } // for (x) 00138 }