PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Fractal Generator problem

Can anybody tell me what is wrong with the following code? It works fine for the most part, but at the middle of the screen, the two halves of the fractal don't line up like (I'm told) they're supposed to.

Any help would be greatly appreciated.

Code:

#include <EasyCE.h>
#include <math.h>

#define ITER 32

#define rgb(r,g,b) ((((r)>>3)<<11)|(((g)>>2)<<5)|((b)>>3))

unsigned short colors[256];
unsigned short* screen;
unsigned char buffer1[240*320];
int sn[FP_ONE];

bool bu=false;

void makepal(void){
unsigned short *ppal;
int i;

ppal = colors;
for (i=0; i<256; i++) {
//colors[i] = ((i>>3)<<11)|((i>>2)<<5)|(i>>3);
colors[i] = rgb(i,i,i);
}
}

inline int msin( int x )
{
return sn[x&(FP_ONE-1)];
}

inline int mcos( int x )
{
return sn[(x+(FP_ONE>>2))&(FP_ONE-1)];
}

void main()
{
int x,y,i,a=0;
int x0,y0,p,q,xs,ys,xsq,ysq,xmin,xmax,ymin,ymax;
int iter=ITER,s=3;
xmin=FP(-1.5);
ymin=FP(-1.5);
xmax=FP(1.5);
ymax=FP(1.5);
xs=(xmax-xmin)/240;
ys=(ymax-ymin)/320;
makepal();

for (int i=0; i<FP_ONE; i++)
{
sn[i] = int(sin((float(i)/float(FP_ONE))*float(3.141592f*2))*FP_ONE);
}

while (!buttondown())
{
screen = (unsigned short*)getbuffer();
resettimer();
a+=0x40000;
if (pendown())
{
p=(penxpos()-120)*xs;
q=(penypos()-160)*ys;
}
else
{
p=mcos(a>>15);
q=msin(a>>15);
}
unsigned char* p1=&buffer1;[0];
unsigned char* p2=&buffer1;[320*240-1];
for (y=0;y<160;y++)
{
for (x=0;x<240;x++)
{
x0=xmin+x*xs;
y0=ymin+y*ys;
for (i=1;i<iter && (ysq=FPMUL(y0,y0))+(xsq=FPMUL(x0,x0))<IFP(4);i++) {
y0=q+(FPMUL(x0,y0)<<1);
x0=p+xsq-ysq;
}
i=(i==ITER) ? 1 : --i&255;
*p1 = i<<3;
p1++;
*p2 = i<<3;
p2--;
}
if (buttondown())
{
break;
}
}/**/
unsigned short* s=screen+240;
unsigned char* buf1=buffer1+240;
i=(240*318)-1;
register int z;
do
{
z = *buf1;
*(s++) = colors[z&255];
buf1++;
} while (i--);
while (msecs()<20)
;
resettimer();
update();
}
}


One more note, the problem just began when I converted the program to fixed-point math, with floating-point numbers it worked fine.
Lewil · 25 Feb 2002, 15:52 UTC — hu
looks like the Top fractal part is shifted by 4 pix on the right, the break in the middle only comes from the way you draw the bottom, like a central symetry, which has the effect of shifting from 4 pix the bottom part but on the left :/ (i mean the top is shifted right SO the bottom is shifted left by the symetrical calculation which makes a 4+4=8 pix break) so the question is in fact: why is the Top part shifted on the right ?
Lewil · 25 Feb 2002, 16:51 UTC — works
because if you don't use symetry, it works well (slower of course):

Code:

#include "EasyCE.h"
#include <math.h>

#define ITER 32

#define rgb(r,g,b) ((((r)>>3)<<11)|(((g)>>2)<<5)|((b)>>3))

unsigned short colors[256];
unsigned short* screen;
unsigned char buffer1[240*320];
int sn[FP_ONE];

bool bu=false;

void makepal(void){
unsigned short *ppal;
int i;

ppal = colors;
for (i=0; i<256; i++) {
//colors[i] = ((i>>3)<<11)|((i>>2)<<5)|(i>>3);
colors[i] = rgb(i,i,i);
}
}

inline int msin( int x )
{
return sn[x&(FP_ONE-1)];
}

inline int mcos( int x )
{
return sn[(x+(FP_ONE>>2))&(FP_ONE-1)];
}

void main()
{
int x,y,i,a=0;
int x0,y0,p,q,xs,ys,xsq,ysq,xmin,xmax,ymin,ymax;
int iter=ITER,s=3;
xmin=FP(-1.5);
ymin=FP(-1.5);
xmax=FP(1.5);
ymax=FP(1.5);
xs=(xmax-xmin)/240;
ys=(ymax-ymin)/320;
makepal();

for (int ii=0; ii<FP_ONE; ii++)
{
sn[ii] = int(sin((float(ii)/float(FP_ONE))*float(3.141592f*2))*FP_ONE);
}

while (!buttondown())
{
screen = (unsigned short*)getbuffer();
resettimer();
a+=0x40000;
if (pendown())
{
p=(penxpos()-120)*xs;
q=(penypos()-160)*ys;
}
else
{
p=mcos(a>>15);
q=msin(a>>15);
}
unsigned char* p1=&buffer1;[0];
unsigned char* p2=&buffer1;[320*240-1];
for (y=0;y<320;y++)
{
for (x=0;x<240;x++)
{
x0=xmin+x*xs;
y0=ymin+y*ys;
for (i=1;i<iter && (ysq=FPMUL(y0,y0))+(xsq=FPMUL(x0,x0))<IFP(4);i++)
{
y0=q+(FPMUL(x0,y0)<<1);
x0=p+xsq-ysq;
}
i=(i==ITER) ? 1 : --i&255;
*p1 = i<<3;
p1++;
// *p2 = i<<3;
// p2--;
}
if (buttondown())
{
break;
}
}/**/
unsigned short* s=screen+240;
unsigned char* buf1=buffer1+240;
i=(240*318)-1;
register int z;
do
{
z = *buf1;
*(s++) = colors[z&255];
buf1++;
} while (i--);
while (msecs()<20);
resettimer();
update();
}
}