/* Cheap mandelbrot v.5 The source code is in a bit of a mess, I'm sure many parts of it aren't needed. Feel free to mess around with it. If you think you have made an improvement to it(admittedly not hard), please send me a copy. If you have any questions about the code(or anything) please email me: flibble@esatclear.ie. http://www.esatclear.ie/~flibble/chaos/index.html */ /* includes */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include long long double log(long long double x); /* Constants */ #define BobSize 10 // Text size - of coords #define limit 20 // Upper number of pixels ball can jump - upper random number #define EDGE1 0 //gap of window to left/right of screen HORIZONTRAL #define EDGE2 0 //gap of window to top/bottom of screen VERTICAL #define LIMIT 2 long int newLeft=512, //starting point x newTop=384, //starting point y newLeftA; int Yinc=0,Xinc=0; // temp - used in Text() complex x(0,0),c(0,0),c1(0,0); long long double xr,xi,cr,ci, crinitial = -2, crfinal = 1, ciinitial = -1, cifinal = 1, mandelPointCount, OutPointCount, MoverO, crincrement,ciincrement,Xvalue[2000],Yvalue[2000],ph,pv,counter, Xaxistop; int textcounterX=0,textcounterY=0,MagCount,smuge,cvinc,InOrOutInc,row,col, BallWidth = 4, BallHeight = 4, pictureSize=1, // the smaller this is the large the picture drawn is(>0) colourRateA=1000, // change look of the set colourRateB=1, colourRateC=1, a, ACCURACY=200; // number of iteraiions to decide if inside mandelbrot set int ProgStart=0; typedef complex dcomp; /* Globals */ Rect windRect; // Some more global variables WindowPtr gWindowPtr; RGBColor gWhiteColour = { 0xFFFF, 0xFFFF, 0xFFFF }; RGBColor gBlackColour = { 0x0000, 0x0000, 0x0000 }; RGBColor gRedColour = { 0xAAAA, 0x0000, 0x0000 }; RGBColor gRedColourD1 = { 0x7777, 0x0000, 0x0000 }; RGBColor gRedColourB1 = { 0xFFFF, 0x0000, 0x0000 }; RGBColor gYellowColour = { 0xFFFF, 0xCCCC, 0x0000 }; RGBColor gYellowColourD1 = { 0xFFFF, 0x7777, 0x0000 }; RGBColor gGreenColour = { 0x0000, 0x9999, 0x0000 }; RGBColor gBlueColour = { 0x6666, 0x6666, 0x9999 }; RGBColor gBlueColourB1 = { 0x0000, 0x0000, 0xFFFF }; RGBColor gBlueColourD1 = { 0x0000, 0x0000, 0x9999 }; /* Prototypes */ void Initialize(void); float NewBall(float cr,float ci); void ColorBG(void); float TextX(float cr,float ci); // to bring cr and ci to TextX and write the coords on screen float TextY(float cr,float ci); // to bring cr and ci to TextY and write the coords on screen void Equation(void); void OutPix(int counter); void Grid(void); // draw grid on screen void MouseExit(void); void Completed(void); void MouseEnd(void); void RubberBand(void); void comm(void); void escape(void); void endofprogramcredits(void); void startOptions(void); void niceLilRectangle(int,int, int, int,char,int); Point p; UInt16 doRandomNumber(UInt16,UInt16); //MW specified argument and return type. void main(void) { Initialize(); if(ProgStart==0){ startOptions(); } ProgStart=1; ColorBG(); Equation(); comm(); MoverO = mandelPointCount/OutPointCount; do { EventRecord event; WaitNextEvent(everyEvent, &event, 30,0); }while(!Button()); RubberBand(); //Grid(); do { EventRecord event; WaitNextEvent(everyEvent, &event, 30,0); }while(Button()); } // // Initialize everything for the program, make sure we can run // //MW specified argument and return type. void Initialize(void) { //MW added to support new Carbon routines #if TARGET_API_MAC_CARBON BitMap theScreenBits; #endif WindowPtr mainPtr; unsigned long theDateTime; OSErr error; SysEnvRec theWorld; // // Test the computer to be sure we can do color. // If not we would crash, which would be bad. // If we can't run, just beep and exit. // // //MW added !TARGET_API_MAC_CARBON for Carbon version #if TARGET_API_MAC_CARBON //MW do nada now #else error = SysEnvirons(1, &theWorld); if (theWorld.hasColorQD == false) { SysBeep(50); ExitToShell(); /* If no color QD, we must leave. */ } #endif /* Initialize all the needed managers for Classic version only! */ // //MW added !TARGET_API_MAC_CARBON for Carbon version #if TARGET_API_MAC_CARBON //MW don't need to init any of the managers below for Carbon #else InitGraf(&qd.thePort); InitFonts(); InitWindows(); InitMenus(); TEInit(); InitDialogs(nil); #endif InitCursor(); //MW added !TARGET_API_MAC_CARBON for Carbon version #if TARGET_API_MAC_CARBON GetDateTime( &theDateTime ); SetQDGlobalsRandomSeed( theDateTime ); /* carbon accessor */ #else GetDateTime((unsigned long*) &qd.randSeed); //qd.randSeed = theDateTime; #endif // // Make a new window for drawing in, and it must be a color window. // The window is full screen size, made smaller to make it more visible. // //MW added !TARGET_API_MAC_CARBON for Carbon version #if TARGET_API_MAC_CARBON GetQDGlobalsScreenBits( &theScreenBits ); /* carbon accessor */ windRect = theScreenBits.bounds; #else windRect = qd.screenBits.bounds; #endif InsetRect(&windRect, 50, 50); mainPtr = NewCWindow(nil, &windRect, "\pBob Land - Carbon!", true, documentProc, (WindowPtr) -1, false, 0); //MW added for Carbon version #if TARGET_API_MAC_CARBON SetPortWindowPort( mainPtr ); /* carbon accessor */ #else SetPort( mainPtr ); /* set window to current graf port */ #endif TextSize(BobSize); /* smaller font for drawing. */ } // Algorithm to find which pixels are inside the mandelbrot set // calls OutPix when point is found. void Equation(void) { #define StartPtX 65 newLeft = StartPtX; newTop = 50; cr = crinitial; ci = ciinitial; crincrement = -(crinitial - crfinal)/((windRect.right-100-EDGE1)/pictureSize);//***change this number(4) to chnage size of picture // numericial increment for each calculation Real axis - ending point ciincrement = -(ciinitial - cifinal)/((windRect.bottom-100-EDGE2)/pictureSize);//***change this number(4) to chnage size of picture // numericial increment for each calculation Imaginary axis - ending point do { c = dcomp(c.real(), ci); // imag part of c goes to ci2 do { c = dcomp (cr, c.imag()); // real part of c goes to cr2 do { x = (x*x) + c; xr=x.real(); xi=x.imag(); counter++; //if ((xr < .001)&&(xi < .001)){counter=ACCURACY;} }while ((fabs(xr) inside mandelbrot set. draw pixel ballColor.green = 0xFFFF; ballColor.blue = 0xFFFF; BallWidth = 1; BallHeight = 1; mandelPointCount++; } InvertColor(&ballColor); // Set that color as the new color to use in drawing. RGBForeColor (&ballColor); // Move pen to the new location, and colour the pixel. // MoveTo(newLeft, newTop); SetRect(&ballRect, newLeft, newTop, newLeft+BallWidth, newTop+BallHeight); PaintOval (&ballRect); //SetCPixel(newLeft,newTop, &ballColor); if ((newTop==60)&&(newLeft == 100)) { comm(); } } // Write X value along top of screen float TextX(float cr, float ci) // takes cr and ci from NewBall { RGBColor ballColor; // Rect ballRect; ballColor.red = 0xFFFF;//Random(); ballColor.green = 0;//Random(); ballColor.blue = 0;//Random(); //SetRect(&ballRect, 100, 122, 122+22, 122+22); //PaintOval (&ballRect); //DrawString("\pConor"); MoveTo(newLeft, newTop - 3); char Xchar[5]; sprintf(Xchar,"%.4f",cr); // int to char InvertColor(&ballColor); RGBForeColor(&ballColor); DrawText(Xchar,short (0),short (7)); } // Write Y value along side of screen float TextY(float cr, float ci) // takes cr and ci from NewBall { TextSize(BobSize); MoveTo(5, newTop); char Xchar[8],Ychar[8]; RGBColor ballColor; Rect ballRect; ballColor.red = 0xFFFF;//Random(); ballColor.green = 0;//Random(); ballColor.blue = 0;//Random(); sprintf(Ychar,"%.4fi ",ci); // int to char InvertColor(&ballColor); RGBForeColor(&ballColor); DrawText(Ychar,short (0),short (9)); } void Grid(void) { int x=55,y=55; // coords used for lines RGBColor ballColor; Rect ballRect; ballColor.red = 0; ballColor.green = 0xFFFF; ballColor.blue = 0xFFFF; RGBForeColor (&ballColor); do{ MoveTo(x+20,y); LineTo(x+20,1000); x = x + 20; }while(x<1100); x=55; do{ MoveTo(x,y+20); LineTo(1100,y+20); y = y + 20; }while (y<800); } void ColorBG(void) { RGBColor ballColor; Rect ballRect; long int newLeft, newTop; RGBForeColor (&gBlackColour); SetRect(&ballRect, 0, 0, windRect.right, windRect.bottom); PaintRect (&ballRect); } void Completed(void) { RGBColor ballColor; Rect ballRect; ballColor.red = 0xFFFF;//Random(); ballColor.green = 0;//Random(); ballColor.blue = 0;//Random(); TextSize(30); SetRect(&ballRect, 60, 100, 160, 200); MoveTo(ballRect.left, ballRect.top); // InvertColor(&ballColor); RGBForeColor(&ballColor); DrawString("\p Finished"); comm(); escape(); } void MouseExit(void) // quits program is p.h = 0 { GetMouse (&p); GlobalToLocal(&p); if (p.h == 0){ExitToShell();} } void RubberBand(void) { escape(); long long double SRItmp,SIItmp; // temp varbs - make sure srealfinal > sreal init etc PixPatHandle pixpatHdl=0; Point initialMouse, previousMouse, currentMouse; Rect drawRect; UInt16 randomNumber; do { RGBBackColor(&gWhiteColour); #if TARGET_API_MAC_CARBON //conor - seems to work in classic mode //nutin here #else FillRect(&gWindowPtr->portRect,&qd.white); #endif PenPixPat(pixpatHdl); PenSize(2,2); PenMode(patXor); GetMouse(&initialMouse); drawRect.left = drawRect.right = initialMouse.h; drawRect.top = drawRect.bottom = initialMouse.v; crinitial = Xvalue[initialMouse.h]; ciinitial = Yvalue[initialMouse.v]; GetMouse(&previousMouse); while(StillDown()) { GetMouse(¤tMouse); if(currentMouse.v != previousMouse.v || currentMouse.h != previousMouse.h) { FrameRect(&drawRect); if(currentMouse.h >= initialMouse.h) drawRect.right = currentMouse.h; if(currentMouse.v >= initialMouse.v) drawRect.bottom = currentMouse.v; if(currentMouse.h <= initialMouse.h) drawRect.left = currentMouse.h; if(currentMouse.v <= initialMouse.v) drawRect.top = currentMouse.v; FrameRect(&drawRect); } previousMouse.v = currentMouse.v; previousMouse.h = currentMouse.h; } FrameRect(&drawRect); MouseExit(); EventRecord event; WaitNextEvent(everyEvent, &event, 30,0); }while(Button()); crfinal = Xvalue[currentMouse.h]; cifinal = Yvalue[currentMouse.v]; if(crinitial>crfinal) { SRItmp = crinitial; crinitial = crfinal; crfinal = SRItmp; } if(ciinitial>cifinal) { SIItmp = ciinitial; ciinitial = cifinal; cifinal = SIItmp; } if((initialMouse.h==currentMouse.h)||(initialMouse.v==currentMouse.v)) // check pointer moves { crinitial = -2; crfinal = 1; ciinitial = -1; cifinal = 1; } main(); } void comm(void) // draws this on screen while picture is being drawn { RGBColor ballColor; Rect ballRect; RGBForeColor (&gRedColourD1); niceLilRectangle(windRect.right-200+2-EDGE1,windRect.right-50+2-EDGE2, windRect.bottom-100+2-EDGE1,windRect.bottom-70+2-EDGE2,'r',-3); TextFace(bold); TextSize(14); RGBForeColor (&gBlackColour); MoveTo(windRect.right-197-EDGE1, windRect.bottom - 80-EDGE2); DrawString("\pClick here to quit"); if(ProgStart==1){ niceLilRectangle(windRect.right-200+2-EDGE1,windRect.right-50+2-EDGE2, windRect.bottom-200+2-EDGE1,windRect.bottom-170+2-EDGE2,'b',-3); MoveTo(windRect.right-160-EDGE1, windRect.bottom - 180-EDGE2); DrawString("\pOptions"); } TextFace(normal); } void escape (void) { int time; GetMouse (&p); //GlobalToLocal(&p); if ((p.h > windRect.right-200-EDGE1)&&(p.h < windRect.right-50-EDGE1) && (p.v > windRect.bottom-200-EDGE2)&&(p.v windRect.right-200-EDGE1)&&(p.h < windRect.right-50-EDGE1) && (p.v > windRect.bottom-100-EDGE2)&&(p.v 100)&&(p.h<200)&&(p.v>100)&&(p.v<150)) { pictureSize = 4; } else if((p.h>100)&&(p.h<200)&&(p.v>200)&&(p.v<250)) { pictureSize = 2; } else if((p.h>100)&&(p.h<200)&&(p.v>300)&&(p.v<350)) { pictureSize = 1; } else goto marker1; do { EventRecord event; WaitNextEvent(everyEvent, &event, 30,0); }while(Button()); RGBForeColor (&gBlackColour); SetRect(&rect1, 0, 0, 1024, 768); PaintRect (&rect1); RGBForeColor (&gGreenColour); MoveTo(70,70); TextFace(italic | bold); GetFNum("\pPalatino",&fontNum); TextFont(fontNum); TextSize(windRect.right/20); DrawString("\pWelcome to Cheap Mandel v3"); TextSize(16); TextFace(normal); GetFNum("\pNew York",&fontNum); TextFont(fontNum); MoveTo(170,90); DrawString("\pPlease choose colour scheme"); TextSize(12); TextFace(normal); GetFNum("\pNew York",&fontNum); TextFont(fontNum); RGBForeColor (&gBlackColour); // colour 1 niceLilRectangle(100,200,100,150,'y',2); MoveTo(110,110); DrawString("\pClick here"); MoveTo(110,120); DrawString("\pfor Black"); MoveTo(110,130); DrawString("\pand White."); // colour 2 niceLilRectangle(100,200,200,250,'y',2); MoveTo(110,210); DrawString("\pClick here"); MoveTo(110,220); DrawString("\pfor colour"); MoveTo(110,230); DrawString("\pset two"); // color 3 niceLilRectangle(100,200,300,350,'y',2); MoveTo(110,310); DrawString("\pClick here"); MoveTo(110,320); DrawString("\pfor colour"); MoveTo(110,330); DrawString("\pset three"); // crazy color niceLilRectangle(300,400,100,150,'y',2); MoveTo(310,110); DrawString("\pClick here"); MoveTo(310,120); DrawString("\pfor crazy"); MoveTo(310,130); DrawString("\pcolours"); niceLilRectangle(400,450,530,550,'b',2); comm(); marker2:; do { EventRecord event; WaitNextEvent(everyEvent, &event, 30,0); }while(!Button()); escape(); GetMouse (&p); if((p.h>100)&&(p.h<200)&&(p.v>100)&&(p.v<150)) { colourRateA = 0; colourRateB = 0; colourRateC = 0; } else if((p.h>100)&&(p.h<200)&&(p.v>200)&&(p.v<250)) { colourRateA = 800; colourRateB = 830; } else if((p.h>100)&&(p.h<200)&&(p.v>300)&&(p.v<350)) { colourRateA = 1000; colourRateB = 2000; colourRateC = 3000; } else if((p.h>300)&&(p.h<400)&&(p.v>100)&&(p.v<150)) { colourRateA = 11000; colourRateB = 19000; colourRateC = 7000; ACCURACY=5; } else goto marker2; do { EventRecord event; WaitNextEvent(everyEvent, &event, 30,0); }while(Button()); // number of calculations screen RGBForeColor (&gBlackColour); SetRect(&rect1, 0, 0, 1024, 768); PaintRect (&rect1); RGBForeColor (&gGreenColour); MoveTo(70,70); TextFace(italic | bold); GetFNum("\pPalatino",&fontNum); TextFont(fontNum); TextSize(windRect.right/20); DrawString("\pWelcome to Cheap Mandel v3"); TextFace(normal); TextSize(windRect.right/50); MoveTo(70,70+windRect.right/20); DrawString("\pPlease choose the maximum number of calculations"); MoveTo(70,70+windRect.right/14); DrawString("\pto be completed per pixel drawn"); TextSize(12); GetFNum("\pChicago",&fontNum); TextFont(fontNum); //100 calcs niceLilRectangle(100,200,200,250,'b',2); RGBForeColor (&gBlackColour); MoveTo(130,210); DrawString("\p100"); MoveTo(110,225); DrawString("\pcalculations"); MoveTo(110,240); DrawString("\p-fastest-"); //500 calcs niceLilRectangle(100,200,300,350,'b',2); RGBForeColor (&gBlackColour); MoveTo(130,310); DrawString("\p500"); MoveTo(110,325); DrawString("\pcalculations"); MoveTo(102,340); DrawString("\p-recomended-"); //2000 calcs niceLilRectangle(400,520,200,250,'b',2); RGBForeColor (&gBlackColour); MoveTo(430,210); DrawString("\p2000"); MoveTo(410,225); DrawString("\pcalculations"); MoveTo(402,240); DrawString("\p-highly accurate-"); //50000 calcs niceLilRectangle(400,523,300,360,'b',2); RGBForeColor (&gBlackColour); MoveTo(430,310); DrawString("\p50000"); MoveTo(410,325); DrawString("\pcalculations"); MoveTo(412,340); DrawString("\p-You want a -"); MoveTo(402,355); DrawString("\p-super computer-"); comm(); marker3:; do { EventRecord event; WaitNextEvent(everyEvent, &event, 30,0); }while(!Button()); escape(); GetMouse (&p); if((p.h>100)&&(p.h<200)&&(p.v>200)&&(p.v<250)) { ACCURACY = 100; } else if((p.h>100)&&(p.h<200)&&(p.v>300)&&(p.v<350)) { ACCURACY = 500; } else if((p.h>400)&&(p.h<520)&&(p.v>200)&&(p.v<250)) { ACCURACY = 2000; } else if((p.h>400)&&(p.h<523)&&(p.v>300)&&(p.v<360)) { ACCURACY = 50000; } else goto marker3; do { EventRecord event; WaitNextEvent(everyEvent, &event, 30,0); }while(Button()); ///MMMMMMMMMMM**** RGBForeColor (&gBlackColour); SetRect(&rect1, 0, 0, windRect.right, windRect.bottom); PaintRect (&rect1); TextFace(normal); } // draws a rectangle in the specified region of a certain colour void niceLilRectangle(int left,int right, int top, int bottom, char color, int depth) { Rect rect1; RGBColor col1,col2,initialCol; GetForeColor(&initialCol); switch(color) { case 'y' : col1 = gYellowColourD1; col2 = gYellowColour; break; case 'r' : col1 = gRedColourD1; col2 = gRedColour; break; case 'b' : col1 = gBlueColourD1; col2 = gBlueColourB1; break; } RGBForeColor (&col1); SetRect(&rect1, left-depth, top-depth, right-depth, bottom-depth); PaintRect (&rect1); RGBForeColor (&col2); SetRect(&rect1, left, top, right, bottom); PaintRect (&rect1); RGBForeColor (&initialCol); } /* { #if TARGET_API_MAC_CARBON QDFlushPortBuffer( GetQDGlobalsThePort(), NULL ); // carbon accessor #endif } */