Created
January 6, 2012 02:20
-
-
Save andrewrcollins/1568616 to your computer and use it in GitHub Desktop.
#TJHSST ~ Julia Set and Mandelbrot Set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <math.h> | |
| #include <graphics.h> | |
| #include <conio.h> | |
| #include <mouse.h> | |
| #define double long double | |
| #define MaxRI (double)6.0 | |
| #define MaxIter 32 | |
| #define ScrX 40 | |
| #define ScrY 40 | |
| #define MaxScrX 320 | |
| #define MaxScrY 200 | |
| #define Sets ((MaxScrX/ScrX)*(MaxScrY/ScrY)) | |
| #define ScaleR ((double)ScrX/(MaxR-MinR)) | |
| #define ScaleI ((double)ScrY/(MaxI-MinI)) | |
| #define ScaleD ((double)0.0625) | |
| typedef struct { | |
| double real,img; | |
| } complex; | |
| int ginit(void),sqr(complex *),add(complex *,complex); | |
| int plotjulia(void),zoomjulia(void),animatejulia(void); | |
| double norm(complex); | |
| double MinR=-3.0,MaxR=3.0,MinI=-3.0,MaxI=3.0; | |
| int zoomx=0,zoomy=0; | |
| main() | |
| { | |
| ginit(); | |
| while(!kbhit()) { | |
| plotjulia(); | |
| animatejulia(); | |
| zoomjulia(); | |
| } | |
| } | |
| int ginit(void) | |
| { | |
| int gmode,gdriver; | |
| gdriver=CGA; | |
| gmode=CGA; | |
| initgraph(&gdriver,&gmode,""); | |
| } | |
| int sqr(z) | |
| complex *z; | |
| { | |
| complex tmp; | |
| tmp=(*z); | |
| z->real=(tmp.real*tmp.real-tmp.img*tmp.img); | |
| z->img=(2*tmp.real*tmp.img); | |
| } | |
| int add(z,c) | |
| complex *z,c; | |
| { | |
| z->real+=c.real; | |
| z->img+=c.img; | |
| } | |
| double norm(z) | |
| complex z; | |
| { | |
| return sqrt(z.real*z.real+z.img*z.img); | |
| } | |
| int plotjulia(void) | |
| { | |
| complex z,c; | |
| int a,b,iter,d; | |
| c.real=-1.0; | |
| c.img=-1.25; | |
| for(d=0;d<Sets;d++) { | |
| c.img+=ScaleD; | |
| for(a=0;a<ScrX;a++) { | |
| if(getvaluator(MOUSE2)) | |
| break; | |
| for(b=0;b<ScrY;b++) { | |
| if(getvaluator(MOUSE2)) | |
| break; | |
| z.real=(double)a/ScaleR+MinR; | |
| z.img=(double)b/ScaleI+MinI; | |
| for(iter=0;iter<MaxIter;iter++) { | |
| sqr(&z); | |
| add(&z,c); | |
| if(norm(z)>MaxRI) | |
| break; | |
| } | |
| putpixel(a+1+zoomx,(ScrY-b+1)+zoomy,(iter+1)%16); | |
| } | |
| } | |
| zoomx+=ScrX; | |
| if(zoomx>(MaxScrX-ScrX)) { | |
| zoomx=0; | |
| zoomy+=ScrY; | |
| } | |
| if(zoomy>(MaxScrY-ScrY)) { | |
| zoomx=0; | |
| zoomy=0; | |
| } | |
| } | |
| } | |
| int zoomjulia(void) | |
| { | |
| int upx=0,upy=0,dnx=0,dny=0,done=0; | |
| double minr,maxr,mini,maxi; | |
| curson(); | |
| while(!done) { | |
| while(!getvaluator(MOUSE1)); | |
| upx=getvaluator(MOUSEX); | |
| upy=getvaluator(MOUSEY); | |
| while(!getvaluator(MOUSE3)); | |
| dnx=getvaluator(MOUSEX); | |
| dny=getvaluator(MOUSEY); | |
| done=(upx<dnx&&upy<dny); | |
| } | |
| minr=(double)(upx-zoomx)/ScaleR+MinR; | |
| maxr=(double)(dnx-zoomx)/ScaleR+MinR; | |
| mini=(double)(ScrY-(dny-zoomy))/ScaleI+MinI; | |
| maxi=(double)(ScrY-(upy-zoomy))/ScaleI+MinI; | |
| MinR=minr; | |
| MaxR=maxr; | |
| MinI=mini; | |
| MaxI=maxi; | |
| cursoff(); | |
| } | |
| int animatejulia(void) | |
| { | |
| int cnt,getx=0,gety=0,geti; | |
| char *graphbuf[Sets]; | |
| geti=imagesize(1,1,ScrX,ScrY); | |
| for(cnt=0;cnt<Sets;cnt++) { | |
| graphbuf[cnt]=(char *)malloc(geti); | |
| getimage(getx+1,gety+1,getx+ScrX,gety+ScrY,graphbuf[cnt]); | |
| getx+=ScrX; | |
| if(getx>(MaxScrX-ScrX)) { | |
| getx=0; | |
| gety+=ScrY; | |
| } | |
| if(gety>(MaxScrY-ScrY)) { | |
| getx=0; | |
| gety=0; | |
| } | |
| } | |
| while(!kbhit()) { | |
| for(cnt=0;cnt<Sets;cnt++) | |
| putimage(1,1,graphbuf[cnt],COPY_PUT); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*=-=-=-=-=-=-=-=-=* | |
| Complex Numbers | |
| *=-=-=-=-=-=-=-=-=*/ | |
| #include <math.h> | |
| #include <graphics.h> | |
| #include <stdlib.h> | |
| #include <conio.h> | |
| #include <stdio.h> | |
| #include <keybd.h> | |
| #define double long float | |
| #define MaxRI (double)6.0 | |
| #define MaxIter 16 | |
| #define ScrX 50 | |
| #define ScrY 50 | |
| #define MaxScrX 300 | |
| #define MaxScrY 200 | |
| #define ScaleR ((double)ScrX/(MaxR-MinR)) | |
| #define ScaleI ((double)ScrY/(MaxI-MinI)) | |
| #define NONE 0 | |
| #define X_AXIS 1 | |
| #define Y_AXIS 2 | |
| #define XY_AXIS 3 | |
| typedef struct { | |
| double r,i; | |
| } complex; | |
| int ginit(void), | |
| sqr(complex *), | |
| add(complex *,complex), | |
| plotmandel(void), | |
| zoommandel(void); | |
| double MinR=(-3.0),MaxR=(3.0),MinI=(-3.0),MaxI=(3.0); | |
| int zoomx=0,zoomy=0,zoomcnt=0,done=0; | |
| main() | |
| { | |
| ginit(); | |
| while(!done) { | |
| plotmandel(); | |
| zoommandel(); | |
| } | |
| } | |
| int ginit(void) | |
| { | |
| int gmode,gdriver; | |
| gdriver=CGA; | |
| gmode=CGA; | |
| initgraph(&gdriver,&gmode,""); | |
| } | |
| int sqr(z) | |
| complex *z; | |
| { | |
| complex tmp; | |
| tmp=(*z); | |
| z->r=(tmp.r*tmp.r-tmp.i*tmp.i); | |
| z->i=(2*tmp.r*tmp.i); | |
| } | |
| int add(z,c) | |
| complex *z,c; | |
| { | |
| z->r+=c.r; | |
| z->i+=c.i; | |
| } | |
| int plotmandel(void) | |
| { | |
| complex z,c; | |
| register int a,b,iter,sym=NONE,adone=0,bdone=0; | |
| if(zoomcnt==0) | |
| sym=X_AXIS; | |
| for(b=0;b<ScrY&&!bdone;b++) { | |
| for(a=0;a<ScrX&&!adone;a++) { | |
| c.r=(double)a/ScaleR+MinR; | |
| c.i=(double)b/ScaleI+MinI; | |
| z.r=0.0; | |
| z.i=0.0; | |
| for(iter=0;iter<MaxIter;iter++) { | |
| sqr(&z); | |
| add(&z,c); | |
| if(hypot(z.r,z.i)>MaxRI) | |
| break; | |
| } | |
| putpixel(a+zoomx+1,b+zoomy+1,iter%4); | |
| switch(sym) { | |
| case X_AXIS : | |
| putpixel(a+zoomx+1,ScrY-b+zoomy+1,iter%4); | |
| break; | |
| case Y_AXIS : | |
| putpixel(ScrX-a+zoomx+1,b+zoomy+1,iter%4); | |
| adone=a==ScrX/2; | |
| } | |
| } | |
| switch(sym) { | |
| case X_AXIS : | |
| bdone=b==ScrY/2; | |
| } | |
| adone=adone&&!kbhit(); | |
| bdone=bdone&&!kbhit(); | |
| } | |
| } | |
| int zoommandel(void) | |
| { | |
| int oc,ox,oy,upx,upy,dnx,dny,notdone=2; | |
| double minr,maxr,mini,maxi; | |
| char ch; | |
| upx=dnx=ScrX/2+zoomx,upy=dny=ScrY/2+zoomy; | |
| oc=getpixel(dnx,dny); | |
| while(notdone) { | |
| while(!((ch=getch())==13)) { | |
| if(!ch) | |
| ch=getch(); | |
| ox=dnx,oy=dny; | |
| switch(ch) { | |
| case HOME : | |
| case LEFT_ARROW : | |
| case END : | |
| dnx--; | |
| break; | |
| case PG_UP : | |
| case RIGHT_ARROW : | |
| case PG_DN : | |
| dnx++; | |
| } | |
| switch(ch) { | |
| case HOME : | |
| case UP_ARROW : | |
| case PG_UP : | |
| dny--; | |
| break; | |
| case END : | |
| case DOWN_ARROW : | |
| case PG_DN : | |
| dny++; | |
| } | |
| if(dnx!=ox||dny!=oy) { | |
| putpixel(ox,oy,oc); | |
| oc=getpixel(dnx,dny); | |
| putpixel(dnx,dny,~oc); | |
| } | |
| } | |
| if(notdone==2) { | |
| upx=dnx; | |
| upy=dny; | |
| } | |
| notdone--; | |
| } | |
| setcolor(2); | |
| rectangle(upx,upy,dnx,dny); | |
| upx-=zoomx; | |
| upy-=zoomy; | |
| dnx-=zoomx; | |
| dny-=zoomy; | |
| minr=(double)upx/ScaleR+MinR; | |
| maxr=(double)dnx/ScaleR+MinR; | |
| mini=(double)upy/ScaleI+MinI; | |
| maxi=(double)dny/ScaleI+MinI; | |
| MinR=minr; | |
| MaxR=maxr; | |
| MinI=mini; | |
| MaxI=maxi; | |
| zoomcnt++; | |
| zoomx+=ScrX; | |
| if(zoomx>=MaxScrX) { | |
| zoomx=0; | |
| zoomy+=ScrY; | |
| if(zoomy>=MaxScrY) | |
| zoomy=0; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mandelbrot and Julia sets code found on an old 5.25 floppy disk from when I was a nerd at Thomas Jefferson High School for Science and Technology between 1988 and 1992.
http://en.wikipedia.org/wiki/Mandelbrot_set
http://en.wikipedia.org/wiki/Julia_set
Anyone can do whatever they'd like to with this program--if anything.
I remain a complex nerd.