aboutsummaryrefslogtreecommitdiffstats
path: root/src/gwin/gwin_graph.c
blob: 87cdff5be8df0b6f96729489848cd14807a8e958 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://ugfx.io/license.html
 */

/**
 * @file    src/gwin/gwin_graph.c
 * @brief   GWIN sub-system button code
 */

#include "../../gfx.h"

#if GFX_USE_GWIN && GWIN_NEED_GRAPH

#include "gwin_class.h"

#define GGRAPH_FLG_CONNECTPOINTS			(GWIN_FIRST_CONTROL_FLAG<<0)
#define GGRAPH_ARROW_SIZE					5

static const GGraphStyle GGraphDefaultStyle = {
	{ GGRAPH_POINT_DOT, 0, GFX_WHITE },			// point
	{ GGRAPH_LINE_DOT, 2, GFX_GRAY },			// line
	{ GGRAPH_LINE_SOLID, 0, GFX_WHITE },		// x axis
	{ GGRAPH_LINE_SOLID, 0, GFX_WHITE },		// y axis
	{ GGRAPH_LINE_NONE, 0, GFX_WHITE, 0 },		// x grid
	{ GGRAPH_LINE_NONE, 0, GFX_WHITE, 0 },		// y grid
	GWIN_GRAPH_STYLE_XAXIS_ARROWS|GWIN_GRAPH_STYLE_YAXIS_ARROWS		// flags
};

static const gwinVMT graphVMT = {
		"Graph",				// The classname
		sizeof(GGraphObject),	// The object size
		0,						// The destroy routine
		0,						// The redraw routine
		0,						// The after-clear routine
};

static void pointto(GGraphObject *gg, gCoord x, gCoord y, const GGraphPointStyle *style) {
	if (style->type == GGRAPH_POINT_NONE)
		return;

	// Convert to device space. Note the y-axis is inverted.
	x += gg->g.x + gg->xorigin;
	y = gg->g.y + gg->g.height - 1 - gg->yorigin - y;

	if (style->size <= 1) {
		gdispGDrawPixel(gg->g.display, x, y, style->color);
		return;
	}

	switch(style->type) {
	case GGRAPH_POINT_SQUARE:
		gdispGDrawBox(gg->g.display, x-style->size, y-style->size, 2*style->size, 2*style->size, style->color);
		break;
#if GDISP_NEED_CIRCLE
	case GGRAPH_POINT_CIRCLE:
		gdispGDrawCircle(gg->g.display, x, y, style->size, style->color);
		break;
#endif
	case GGRAPH_POINT_DOT:
	default:
		gdispGDrawPixel(gg->g.display, x, y, style->color);
		break;
	}
}

static void lineto(GGraphObject *gg, gCoord x0, gCoord y0, gCoord x1, gCoord y1, const GGraphLineStyle *style) {
	gCoord	dy, dx;
	gCoord addx, addy;
	gCoord P, diff, i;
	gCoord	run_on, run_off, run;

	if (style->type == GGRAPH_LINE_NONE)
		return;

	// Convert to device space. Note the y-axis is inverted.
	x0 += gg->g.x + gg->xorigin;
	y0 = gg->g.y + gg->g.height - 1 - gg->yorigin - y0;
	x1 += gg->g.x + gg->xorigin;
	y1 = gg->g.y + gg->g.height - 1 - gg->yorigin - y1;

	if (style->size <= 0) {
		// Use the driver to draw a solid line
		gdispGDrawLine(gg->g.display, x0, y0, x1, y1, style->color);
		return;
	}

	switch (style->type) {
	case GGRAPH_LINE_DOT:
		run_on = 1;
		run_off = -style->size;
		break;

	case GGRAPH_LINE_DASH:
		run_on = style->size;
		run_off = -style->size;
		break;

	case GGRAPH_LINE_SOLID:
	default:
		// Use the driver to draw a solid line
		gdispGDrawLine(gg->g.display, x0, y0, x1, y1, style->color);
		return;
	}

	// Use Bresenham's algorithm modified to draw a stylized line
	run = 0;
	if (x1 >= x0) {
		dx = x1 - x0;
		addx = 1;
	} else {
		dx = x0 - x1;
		addx = -1;
	}
	if (y1 >= y0) {
		dy = y1 - y0;
		addy = 1;
	} else {
		dy = y0 - y1;
		addy = -1;
	}

	if (dx >= dy) {
		dy *= 2;
		P = dy - dx;
		diff = P - dx;

		for(i=0; i<=dx; ++i) {
			if (run++ >= 0) {
				if (run >= run_on)
					run = run_off;
				gdispGDrawPixel(gg->g.display, x0, y0, style->color);
			}
			if (P < 0) {
				P  += dy;
				x0 += addx;
			} else {
				P  += diff;
				x0 += addx;
				y0 += addy;
			}
		}
	} else {
		dx *= 2;
		P = dx - dy;
		diff = P - dy;

		for(i=0; i<=dy; ++i) {
			if (run++ >= 0) {
				if (run >= run_on)
					run = run_off;
				gdispGDrawPixel(gg->g.display, x0, y0, style->color);
			}
			if (P < 0) {
				P  += dx;
				y0 += addy;
			} else {
				P  += diff;
				x0 += addx;
				y0 += addy;
			}
		}
	}
}

GHandle gwinGGraphCreate(GDisplay *g, GGraphObject *gg, const GWindowInit *pInit) {
	if (!(gg = (GGraphObject *)_gwindowCreate(g, &gg->g, pInit, &graphVMT, 0)))
		return 0;
	gg->xorigin = gg->yorigin = 0;
	gg->lastx = gg->lasty = 0;
	gwinGraphSetStyle((GHandle)gg, &GGraphDefaultStyle);
	gwinSetVisible((GHandle)gg, pInit->show);
	_gwinFlushRedraws(REDRAW_WAIT);
	return (GHandle)gg;
}

void gwinGraphSetStyle(GHandle gh, const GGraphStyle *pstyle) {
	#define gg	((GGraphObject *)gh)

	if (gh->vmt != &graphVMT)
		return;

	gg->style.point = pstyle->point;
	gg->style.line = pstyle->line;
	gg->style.xaxis = pstyle->xaxis;
	gg->style.yaxis = pstyle->yaxis;
	gg->style.xgrid = pstyle->xgrid;
	gg->style.ygrid = pstyle->ygrid;
	gg->style.flags = pstyle->flags;

	#undef gg
}

void gwinGraphSetOrigin(GHandle gh, gCoord x, gCoord y) {
	#define gg	((GGraphObject *)gh)

	if (gh->vmt != &graphVMT)
		return;

	gg->xorigin = x;
	gg->yorigin = y;

	#undef gg
}

void gwinGraphDrawAxis(GHandle gh) {
	#define gg	((GGraphObject *)gh)
	gCoord		i, xmin, ymin, xmax, ymax;

	if (gh->vmt != &graphVMT || !_gwinDrawStart(gh))
		return;

	xmin = -gg->xorigin;
	xmax = gh->width-gg->xorigin-1;
	ymin = -gg->yorigin;
	ymax = gh->height-gg->yorigin-1;

	// x grid - this code assumes that the GGraphGridStyle is a superset of GGraphListStyle
	if (gg->style.xgrid.type != GGRAPH_LINE_NONE && gg->style.xgrid.spacing >= 2) {
		for(i = gg->style.xgrid.spacing; i <= xmax; i += gg->style.xgrid.spacing)
			lineto(gg, i, ymin, i, ymax, (GGraphLineStyle *)&gg->style.xgrid);
		for(i = -gg->style.xgrid.spacing; i >= xmin; i -= gg->style.xgrid.spacing)
			lineto(gg, i, ymin, i, ymax, (GGraphLineStyle *)&gg->style.xgrid);
	}

	// y grid - this code assumes that the GGraphGridStyle is a superset of GGraphListStyle
	if (gg->style.ygrid.type != GGRAPH_LINE_NONE && gg->style.ygrid.spacing >= 2) {
		for(i = gg->style.ygrid.spacing; i <= ymax; i += gg->style.ygrid.spacing)
			lineto(gg, xmin, i, xmax, i, (GGraphLineStyle *)&gg->style.ygrid);
		for(i = -gg->style.ygrid.spacing; i >= ymin; i -= gg->style.ygrid.spacing)
			lineto(gg, xmin, i, xmax, i, (GGraphLineStyle *)&gg->style.ygrid);
	}

	// x axis
	lineto(gg, xmin, 0, xmax, 0, &gg->style.xaxis);
	if ((gg->style.flags & GWIN_GRAPH_STYLE_XAXIS_NEGATIVE_ARROWS)) {
		if (xmin > 0 || xmin < -(GGRAPH_ARROW_SIZE+1)) {
			lineto(gg, xmin, 0, xmin+GGRAPH_ARROW_SIZE, GGRAPH_ARROW_SIZE, &gg->style.xaxis);
			lineto(gg, xmin, 0, xmin+GGRAPH_ARROW_SIZE, -GGRAPH_ARROW_SIZE, &gg->style.xaxis);
		}
	}
	if ((gg->style.flags & GWIN_GRAPH_STYLE_XAXIS_POSITIVE_ARROWS)) {
		if (xmax < 0 || xmax > (GGRAPH_ARROW_SIZE+1)) {
			lineto(gg, xmax, 0, xmax-GGRAPH_ARROW_SIZE, GGRAPH_ARROW_SIZE, &gg->style.xaxis);
			lineto(gg, xmax, 0, xmax-GGRAPH_ARROW_SIZE, -GGRAPH_ARROW_SIZE, &gg->style.xaxis);
		}
	}

	// y axis
	lineto(gg, 0, ymin, 0, ymax, &gg->style.yaxis);
	if ((gg->style.flags & GWIN_GRAPH_STYLE_YAXIS_NEGATIVE_ARROWS)) {
		if (ymin > 0 || ymin < -(GGRAPH_ARROW_SIZE+1)) {
			lineto(gg, 0, ymin, GGRAPH_ARROW_SIZE, ymin+GGRAPH_ARROW_SIZE, &gg->style.yaxis);
			lineto(gg, 0, ymin, -GGRAPH_ARROW_SIZE, ymin+GGRAPH_ARROW_SIZE, &gg->style.yaxis);
		}
	}
	if ((gg->style.flags & GWIN_GRAPH_STYLE_YAXIS_POSITIVE_ARROWS)) {
		if (ymax < 0 || ymax > (GGRAPH_ARROW_SIZE+1)) {
			lineto(gg, 0, ymax, GGRAPH_ARROW_SIZE, ymax-GGRAPH_ARROW_SIZE, &gg->style.yaxis);
			lineto(gg, 0, ymax, -GGRAPH_ARROW_SIZE, ymax-GGRAPH_ARROW_SIZE, &gg->style.yaxis);
		}
	}

	_gwinDrawEnd(gh);
	#undef gg
}

void gwinGraphStartSet(GHandle gh) {
	if (gh->vmt != &graphVMT)
		return;

	gh->flags &= ~GGRAPH_FLG_CONNECTPOINTS;
}

void gwinGraphDrawPoint(GHandle gh, gCoord x, gCoord y) {
	#define gg	((GGraphObject *)gh)

	if (gh->vmt != &graphVMT || !_gwinDrawStart(gh))
		return;

	if ((gh->flags & GGRAPH_FLG_CONNECTPOINTS)) {
		// Draw the line
		lineto(gg, gg->lastx, gg->lasty, x, y, &gg->style.line);

		// Redraw the previous point because the line may have overwritten it
		pointto(gg, gg->lastx, gg->lasty, &gg->style.point);

	} else
		gh->flags |= GGRAPH_FLG_CONNECTPOINTS;

	// Save this point for next time.
	gg->lastx = x;
	gg->lasty = y;

	// Draw this point.
	pointto(gg, x, y, &gg->style.point);

	_gwinDrawEnd(gh);
	#undef gg
}

void gwinGraphDrawPoints(GHandle gh, const gPoint *points, unsigned count) {
	#define gg	((GGraphObject *)gh)
	unsigned		i;
	const gPoint		*p;

	if (gh->vmt != &graphVMT || !_gwinDrawStart(gh))
		return;

	// Draw the connecting lines
	for(p = points, i = 0; i < count; p++, i++) {
		if ((gh->flags & GGRAPH_FLG_CONNECTPOINTS)) {
			// Draw the line
			lineto(gg, gg->lastx, gg->lasty, p->x, p->y, &gg->style.line);

			// Redraw the previous point because the line may have overwritten it
			if (i == 0)
				pointto(gg, gg->lastx, gg->lasty, &gg->style.point);

		} else
			gh->flags |= GGRAPH_FLG_CONNECTPOINTS;

		// Save this point for next time.
		gg->lastx = p->x;
		gg->lasty = p->y;
	}


	// Draw the points.
	for(p = points, i = 0; i < count; p++, i++)
		pointto(gg, p->x, p->y, &gg->style.point);

	_gwinDrawEnd(gh);
	#undef gg
}

#endif /* GFX_USE_GWIN && GWIN_NEED_GRAPH */