home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Graphics 16,000
/
graphics-16000.iso
/
amiga
/
fractal
/
fcloud.arc
/
cloud.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-11-05
|
5KB
|
201 lines
/***************************************************************/
/* */
/* FractalCloud version 0.9 (not promising anything!) */
/* (c) 1989 by Philip MacGovern */
/* 3433 1st ave. San Diego, CA 92103 */
/* */
/* These routines are freeware, fully public domain */
/* */
/***************************************************************/
#include <exec/types.h>
#include <intuition/intuition.h>
#include <stdio.h>
#include <math.h>
#define NumWeWant 128
#define MaxLevel 7
typedef float row[NumWeWant+1];
row *X;
struct Window *make_window();
struct Screen *make_screen();
struct IntuiMessage *GetMsg();
double Gauss();
void MidPointFM2D();
float f3();
float f4();
float f3(delta,x0,x1,x2)
float delta;
float x0,x1,x2;
{
return((float)((x0+x1+x2)/3.0 + delta*Gauss()));
}
float f4(delta,x0,x1,x2,x3)
float delta;
float x0,x1,x2,x3;
{
return((float)((x0+x1+x2+x3)/4.0 + delta*Gauss()));
}
/* MidPointFM2D will get fractal motion in two dimensions with the midpoint
displacement method with successive random additions
*/
void MidPointFM2D(maxlevel,sigma,H,addition,seed)
SHORT maxlevel,addition;
float sigma,H;
unsigned int seed;
{
int N,stage;
float delta;
int x,y,D,d;
sigma = 0.5;
InitGauss((unsigned)seed);
delta = sigma;
N = (int)pow2((double)maxlevel);
X[0][0] = (float)delta * Gauss();
X[0][N] = (float)delta * Gauss();
X[N][N] = (float)delta * Gauss();
X[N][0] = (float)delta * Gauss();
D = N;
d = N/2;
for(stage = 1;stage <= maxlevel; stage ++) {
delta = delta * (float)pow(0.5,0.5*H);
for(x=d;x<=(N-d);x+=D)
for(y=d;y<=(N-d);y+=D)
X[x][y] = f4(delta,X[x+d][y+d],X[x+d][y-d],X[x-d][y+d],X[x-d][y-d]);
if(addition)
for(x=0;x<=N;x+=D)
for(y=0;y<=N;y+=D)
X[x][y] += (float)delta * Gauss();
delta = delta * (float)pow(0.5,0.5*H);
for(x=d;x<=(N-d);x+=D) {
X[x][0] = f3(delta,X[x+d][0],X[x-d][0],X[x][d]);
X[x][N] = f3(delta,X[x+d][N],X[x-d][N],X[x][N-d]);
X[0][x] = f3(delta,X[0][x+d],X[0][x-d],X[d][x]);
X[N][x] = f3(delta,X[N][x+d],X[N][x-d],X[N-d][x]);
}
for(x=d;x<=(N-d);x+=D)
for(y=D;y<=(N-d);y+=D)
X[x][y] = f4(delta,X[x][y+d],X[x][y-d],X[x+d][y],X[x-d][y]);
for(x=D;x<=(N-d);x+=D)
for(y=d;y<=(N-d);y+=D)
X[x][y] = f4(delta,X[x][y+d],X[x][y-d],X[x+d][y],X[x-d][y]);
if(addition) {
for(x=0;x<=N;x+=D)
for(y=0;y<=N;y+=D)
X[x][y] += (float)delta * Gauss();
for(x=d;x<=(N-d);x+=D)
for(y=d;y<=(N-d);y+=D)
X[x][y] += (float)delta * Gauss();
}
D = D / 2;
d = d / 2;
}
}
void Set_Colors(vp,c)
struct ViewPort *vp;
UBYTE c[15][3];
{
int n;
for(n=0;n<15;n++)
SetRGB4(vp,n+3,c[n][0],c[n][1],c[n][2]);
}
/* main() will open a screen, window, and draw (hopefully) some Fractal
motion by whatever method we are using */
main(argc,argv)
int argc;
char **argv;
{
struct Window *win;
struct Screen *scrn;
struct RastPort *rport;
struct ViewPort *vport;
struct IntuiMessage *msg;
ULONG flags = ACTIVATE | WINDOWDEPTH | WINDOWDRAG |
WINDOWCLOSE | SMART_REFRESH ;
int n = NumWeWant;
float sigma;
int a;
USHORT clock[2];
SHORT i,j;
float H;
UBYTE colList[11] = { 3, 5, 7, 9, 11, 12, 13, 14, 15, 16, 17 };
UBYTE colors[15][3] =
{ { 0, 0, 15 },
{ 1, 1, 15 },
{ 2, 2, 15 },
{ 3, 3, 15 },
{ 4, 4, 15 },
{ 5, 5, 15 },
{ 6, 6, 15 },
{ 7, 7, 15 },
{ 8, 8, 15 },
{ 9, 9, 15 },
{ 10, 10, 15 },
{ 11, 11, 15 },
{ 12, 12, 15 },
{ 13, 13, 15 },
{ 14, 14, 15 } };
if((X = (row *)calloc(NumWeWant+1,sizeof(row)))==NULL) {
printf("\nUnable to allocate memory\n");
exit(FALSE);
}
if(argc>1)
H = atof(argv[1]); /* H = fractal dimension */
if(H>=1.0 || H<=0.0) {
printf("\nUsing H (=3-D) = 0.5\n");
H = 0.5;
}
else {
printf("\nUsing H (=3-D) = 0.5\n");
H = 0.5;
}
sigma = 0.5; /* initial s.d. */
timer(clock);
Open_All(); /* open the intuition.library */
scrn = make_screen(0,320,200,5,0x00,0x01,NULL,"FractalClouds v0.9 - Generating");
if(scrn==NULL) {
Close_All();
exit(FALSE);
}
win = make_window(30,30,NumWeWant,NumWeWant+10,NULL,flags,-1,-1,scrn);
if(win==NULL) {
Close_All();
CloseScreen(scrn);
exit(FALSE);
}
vport = &scrn->ViewPort;
Set_Colors(vport,colors);
rport = win->RPort;
/* Change the FALSE to TRUE if you want random additions */
MidPointFM2D(MaxLevel,sigma,H,FALSE,clock[1]);
SetWindowTitles(win,NULL,"FractalClouds v0.9 - Displaying");
for(i=0;i<n;i++)
for(j=0;j<n;j++) {
SetAPen(rport,3);
a = (int)(X[i][j]*10.0);
if(a>=-1&&a<=9) SetAPen(rport,colList[a+1]);
Move(rport,i,j+10);
Draw(rport,i,j+10);
}
SetWindowTitles(win,NULL,"FractalClouds v0.9 - Close to Quit");
for(;;) { /* wait 'til user closes */
Wait(1<<win->UserPort->mp_SigBit);
while((msg=GetMsg(win->UserPort))) {
if(msg->Class == CLOSEWINDOW) {
ReplyMsg(msg);
CloseWindow(win);
CloseScreen(scrn);
Close_All();
exit(NULL);
}
}
}
}