summaryrefslogtreecommitdiffstats
path: root/hacks/vines.c
blob: 7f519b4b57b0049531be88ba89d06bd4a8f34626 (plain) (blame)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* -*- Mode: C; tab-width: 4 -*- */
/* vines --- vine fractals */

#if 0
static const char sccsid[] = "@(#)vines.c	5.00 2000/11/01 xlockmore";
#endif

/*-
 * Copyright (c) 1997 by Tracy Camp campt@hurrah.com
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * This file is provided AS IS with no warranties of any kind.  The author
 * shall have no liability with respect to the infringement of copyrights,
 * trade secrets or any patents by this file or any part thereof.  In no
 * event will the author be liable for any lost revenue or profits or
 * other special, indirect and consequential damages.
 *
 * If you make a modification I would of course appreciate a copy.
 *
 * Revision History:
 * 01-Nov-2000: Allocation checks
 * 11-Jul-1997: David Hansen <dhansen@metapath.com>
 *              Changed names to vines and modified draw loop
 *              to honor batchcount so vines can be grown or plotted.
 * 10-May-1997: Compatible with xscreensaver
 * 21-Mar-1997: David Hansen <dhansen@metapath.com>
 *              Updated mode to draw complete patterns on every
 *              iteration instead of growing the vine.  Also made
 *              adjustments to randomization and changed variable
 *              names to make logic easier to follow.
 */

/*-
 * This was modifed from a 'screen saver' that a friend and I
 * wrote on our TI-8x calculators in high school physics one day
 * Basically another geometric pattern generator, this ones claim
 * to fame is a pseudo-fractal looking vine like pattern that creates
 * nifty whorls and loops.
 */

#ifdef STANDALONE
# define MODE_vines
# define DEFAULTS	"*delay: 200000 \n" \
					"*count: 0 \n" \
					"*ncolors: 64 \n" \
					"*fpsSolid: true \n" \
				    "*lowrez: True \n" \

# include "xlockmore.h"		/* in xscreensaver distribution */
# define free_vines 0
# define release_vines 0
# define reshape_vines 0
# define vines_handle_event 0
#else /* STANDALONE */
# include "xlock.h"		/* in xlockmore distribution */
#endif /* STANDALONE */

#ifdef MODE_vines

ENTRYPOINT ModeSpecOpt vines_opts =
{0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};

#ifdef USE_MODULES
ModStruct   vines_description =
{"vines", "init_vines", "draw_vines", (char *) NULL,
 "refresh_vines", "init_vines", (char *) NULL, &vines_opts,
 200000, 0, 1, 1, 64, 1.0, "",
 "Shows fractals", 0, NULL};

#endif

typedef struct {
	int         a;
	int         x1;
	int         y1;
	int         x2;
	int         y2;
	int         i;
	int         length;
	int         iterations;
	int         constant;
	int         ang;
	int         centerx;
	int         centery;
} vinestruct;

static vinestruct *vines = (vinestruct *) NULL;

#ifndef STANDALONE
ENTRYPOINT void
refresh_vines(ModeInfo * mi)
{
	MI_CLEARWINDOW(mi);
}				/* refresh_vines */
#endif

ENTRYPOINT void
init_vines(ModeInfo * mi)
{
	vinestruct *fp;

	MI_INIT (mi, vines);
	fp = &vines[MI_SCREEN(mi)];

	fp->i = 0;
	fp->length = 0;
	fp->iterations = 30 + NRAND(100);

	MI_CLEARWINDOW(mi);
}				/* init_vines */

ENTRYPOINT void
draw_vines(ModeInfo * mi)
{
	Display    *display = MI_DISPLAY(mi);
	GC          gc = MI_GC(mi);
	int         count;
	vinestruct *fp;

	if (vines == NULL)
		return;
	fp = &vines[MI_SCREEN(mi)];

	MI_IS_DRAWN(mi) = True;
	if (fp->i >= fp->length) {
		if (--(fp->iterations) == 0) {
			init_vines(mi);
			return;
		}
		fp->centerx = NRAND(MI_WIDTH(mi));
		fp->centery = NRAND(MI_HEIGHT(mi));

		fp->ang = 60 + NRAND(720);
		fp->length = 100 + NRAND(3000);
		fp->constant = fp->length * (10 + NRAND(10));

		fp->i = 0;
		fp->a = 0;
		fp->x1 = 0;
		fp->y1 = 0;
		fp->x2 = 1;
		fp->y2 = 0;

		if (MI_NPIXELS(mi) > 2)
			XSetForeground(display, gc, MI_PIXEL(mi, NRAND(MI_NPIXELS(mi))));
		else
			XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
	}
	count = fp->i + MI_COUNT(mi);
	if ((count <= fp->i) || (count > fp->length))
		count = fp->length;

	while (fp->i < count) {
		XDrawLine(display, MI_WINDOW(mi), gc,
			  fp->centerx + (fp->x1 / fp->constant),
			  fp->centery - (fp->y1 / fp->constant),
			  fp->centerx + (fp->x2 / fp->constant),
			  fp->centery - (fp->y2 / fp->constant));

		fp->a += (fp->ang * fp->i);

		fp->x1 = fp->x2;
		fp->y1 = fp->y2;

		fp->x2 += (int) (fp->i * (cos((double) fp->a) * 360.0) / (2.0 * M_PI));
		fp->y2 += (int) (fp->i * (sin((double) fp->a) * 360.0) / (2.0 * M_PI));
		fp->i++;
	}
}				/* draw_vines */


XSCREENSAVER_MODULE ("Vines", vines)

#endif /* MODE_vines */