1 /*****************************************************************************
2 * This file is part of gfxprim library. *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
21 *****************************************************************************/
35 /* Set to 1 to use Anti Aliased drawing */
36 static int aa_flag
= 0;
39 * Generate color depending on distance from center
41 * We could do this only and only because the pixmap
42 * pixel type is fixed to GP_PIXEL_RGB888.
44 static GP_Pixel
do_color(int xc
, int yc
, float x
, float y
)
46 float dx
= GP_ABS(1.00 * xc
- x
)/(2*xc
);
47 float dy
= GP_ABS(1.00 * yc
- y
)/(2*yc
);
49 float dc
= sqrt(dx
*dx
+ dy
*dy
);
52 int gmask
= 0xff * sqrt(dc
);
53 int bmask
= 0xff * (1 - dc
);
56 rmask
= 0xff * (1 - dc
);
66 return bmask
| (gmask
<<8) | (rmask
<< 16);
69 static void draw(GP_Pixmap
*img
, int level
, float x0
, float y0
, float x1
, float y1
)
74 pixel
= do_color(img
->w
/2, img
->h
/2, 1.00 * (x0
+x1
)/2, 1.00 * (y0
+ y1
)/2);
77 GP_LineAA(img
, x0
* 256, y0
* 256, x1
* 256, y1
* 256, pixel
);
79 GP_Line(img
, x0
, y0
, x1
, y1
, pixel
);
84 /* Compute varation of Koch curve */
85 float x34
= (x0
+ 3 * x1
) / 4;
86 float y34
= (y0
+ 3 * y1
) / 4;
88 float x14
= (3 * x0
+ x1
) / 4;
89 float y14
= (3 * y0
+ y1
) / 4;
91 float x12
= (x0
+ x1
)/2;
92 float y12
= (y0
+ y1
)/2;
94 float dx
= (x1
- x0
)/4;
95 float dy
= (y1
- y0
)/4;
97 draw(img
, level
- 1, x0
, y0
, x14
, y14
);
98 draw(img
, level
- 1, x14
, y14
, x14
- dy
, y14
+ dx
);
99 draw(img
, level
- 1, x14
- dy
, y14
+ dx
, x12
- dy
, y12
+ dx
);
100 draw(img
, level
- 1, x12
- dy
, y12
+ dx
, x12
, y12
);
101 draw(img
, level
- 1, x12
, y12
, x12
+ dy
, y12
- dx
);
102 draw(img
, level
- 1, x12
+ dy
, y12
- dx
, x34
+ dy
, y34
- dx
);
103 draw(img
, level
- 1, x34
+ dy
, y34
- dx
, x34
, y34
);
104 draw(img
, level
- 1, x1
, y1
, x34
, y34
);
111 /* Create RGB 24 bit image */
112 img
= GP_PixmapAlloc(600, 600, GP_PIXEL_RGB888
);
115 fprintf(stderr
, "Failed to allocate pixmap");
119 /* Clean up the bitmap */
123 draw(img
, 4, 0, 0, img
->w
- 1, img
->h
- 1);
124 draw(img
, 4, 0, img
->h
- 1, img
->w
- 1, 0);
126 if (GP_SavePNG(img
, "out.png", NULL
)) {
127 fprintf(stderr
, "Failed to save image %s", strerror(errno
));