File: mandel.c

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (84 lines) | stat: -rw-r--r-- 2,164 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
  !!DESCRIPTION!! mandelbrot test program
  !!ORIGIN!!      testsuite
  !!LICENCE!!     Public Domain
  !!AUTHOR!!      Groepaz/Hitmen
*/

#include <stdio.h>
#include <stdlib.h>

static unsigned short SCREEN_X;
static unsigned char  SCREEN_Y;

#define MAXCOL     8

#define maxiterations 16

#define fpshift (12)

#define tofp(_x)        ((_x)<<fpshift)
#define fromfp(_x)      ((_x)>>fpshift)
#define fpabs(_x)       (abs(_x))

#define mulfp(_a,_b)    ((((signed long)_a)*(_b))>>fpshift)
#define divfp(_a,_b)    ((((signed long)_a)<<fpshift)/(_b))

unsigned char dither[MAXCOL]={" .*o+0%#"};

void mandelbrot(signed short x1,signed short y1,signed short x2,signed short y2)
{
register signed short  r,r1,i;
register unsigned char count;
register signed short xs,ys,xx,yy;
register signed short x;
register unsigned char y;

        /* calc stepwidth */
        xs=((x2-x1)/(SCREEN_X));
        ys=((y2-y1)/(SCREEN_Y));

        yy=y1;
        for(y = 0; y < (SCREEN_Y); ++y)
        {
                yy+=ys; xx=x1;
                for(x = 0; x < (SCREEN_X); ++x)
                {
                    xx+=xs;
                    /* do iterations */
                    r=0;i=0;
                    for(count=0;(count<maxiterations)&&
                                (fpabs(r)<tofp(2))&&
                                (fpabs(i)<tofp(2))
                                ;++count)
                    {
                            r1 = (mulfp(r,r)-mulfp(i,i))+xx;
                            /* i = (mulfp(mulfp(r,i),tofp(2)))+yy; */
                            i = (((signed long)r*i)>>(fpshift-1))+yy;
                            r=r1;
                    }
                    if(count==maxiterations)
                    {
                            printf(" ");
                    }
                    else
                    {
                            printf("%c",dither[(count%MAXCOL)]);
                    }
                }
                    printf("\n");
        }
}

int main (void)
{
    SCREEN_X = 80;
    SCREEN_Y = 40;

    /* calc mandelbrot set */
    mandelbrot(tofp(-2),tofp(-2),tofp(2),tofp(2));

    /* Done */
    return 0;
}