Tscope5
primitives.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // __ ______
4 // / /_______________ ____ ___ / ____/
5 // / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
6 // / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
7 // \__/____/\___/\____/ .___/\___/ /_____/
8 // /_/
9 //
10 /// \file primitives.c
11 /// Definitions of primitives drawing functions
12 /// \example primitives01.c
13 /// \example primitives02.c
14 /// \example primitives03.c
15 /// \example primitives04.c
16 ////////////////////////////////////////////////////////////////////////////////
17 
18 
19 #include "../include/tscope5/primitives.h"
20 #include "../include/tscope5/graphics.h"
21 #include "../include/tscope5/primitives_internal.h"
22 #include "../include/tscope5/system_internal.h"
23 #include "../include/tscope5/graphics_internal.h"
24 
25 #include <allegro5/allegro_primitives.h>
26 
27 
28 ////////////////////////////////////////////////////////////////////////////////
29 /// @name Primitives drawing functions
30 /// The drawing functions all take the smallest possible number of parameters
31 /// to put something on the screen: some coordinates.
32 /// Drawing parameters are set with the graphics parameter functions.
33 //@{
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// Draw a pixel.
39 ///
40 /// \param x Horizontal position of the pixel.
41 /// \param y Vertical position of the pixel.
42 ////////////////////////////////////////////////////////////////////////////////
43 void ts5_draw_pixel(double x, double y)
44 {
45  ts5_check_primitives("ts5_draw_pixel");
46  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_pixel(%f,%f)\n", x, y);
47 
48  if (_ts5_status.graphics.coordinate_scale
49  == TS5_RELATIVE_COORDINATES) {
52  }
53 
54  if (_ts5_status.graphics.coordinate_system
55  == TS5_CARTESIAN_COORDINATES) {
58  }
59 
60  if (_ts5_status.graphics.drawing_thickness <= 0.0) {
61  al_draw_pixel(x, y, _ts5_status.graphics.foreground_color);
62  }
63  else {
64  al_draw_filled_circle(x, y,
65  _ts5_status.graphics.drawing_thickness,
66  _ts5_status.graphics.foreground_color);
67  }
68 }
69 
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Get the color of a pixel.
73 ///
74 /// \param x Horizontal position of the pixel.
75 /// \param y Vertical position of the pixel.
76 ///
77 /// Return value:
78 /// TS5_COLOR structure.
79 ////////////////////////////////////////////////////////////////////////////////
80 TS5_COLOR ts5_get_pixel_color(double x, double y)
81 {
82  ts5_check_primitives("ts5_get_pixel_color");
83  ts5_log(TS5_LOGLEVEL_5, "ts5_get_pixel_color(%f,%f)\n", x, y);
84 
85  if (_ts5_status.graphics.coordinate_scale
86  == TS5_RELATIVE_COORDINATES) {
89  }
90 
91  if (_ts5_status.graphics.coordinate_system
92  == TS5_CARTESIAN_COORDINATES) {
95  }
96 
97  return (al_get_pixel(_ts5_data.target, x, y));
98 }
99 
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 /// Draw a line segment.
103 ///
104 /// \param x1 Horizontal position of the start point of the line segment.
105 /// \param y1 Vertical position of the start point of the line segment.
106 /// \param x2 Horizontal position of the end point of the line segment.
107 /// \param y2 Vertical position of the end point of the line segment.
108 ////////////////////////////////////////////////////////////////////////////////
109 void ts5_draw_line(double x1, double y1, double x2, double y2)
110 {
111  ts5_check_primitives("ts5_draw_line");
112  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_line(%f,%f,%f,%f)\n", x1, y1, x2, y2);
113 
114  if (_ts5_status.graphics.coordinate_scale
115  == TS5_RELATIVE_COORDINATES) {
120  }
121 
122  if (_ts5_status.graphics.coordinate_system
123  == TS5_CARTESIAN_COORDINATES) {
128  }
129 
130  al_draw_line(x1, y1, x2, y2, _ts5_status.graphics.foreground_color,
131  _ts5_status.graphics.drawing_thickness);
132 }
133 
134 
135 ////////////////////////////////////////////////////////////////////////////////
136 /// Draw a rectangle.
137 ///
138 /// \param x1 Horizontal position of the first corner of the rectangle.
139 /// \param y1 Vertical position of the first corner of the rectangle.
140 /// \param x2 Horizontal position of the second corner of the rectangle.
141 /// \param y2 Vertical position of the second corner of the rectangle.
142 ////////////////////////////////////////////////////////////////////////////////
143 void ts5_draw_rectangle(double x1, double y1, double x2, double y2)
144 {
145  ts5_check_primitives("ts5_draw_rectangle");
146  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_rectangle(%f,%f,%f,%f)\n",
147  x1, y1, x2, y2);
148 
149  if (_ts5_status.graphics.coordinate_scale
150  == TS5_RELATIVE_COORDINATES) {
155  }
156 
157  if (_ts5_status.graphics.coordinate_system
158  == TS5_CARTESIAN_COORDINATES) {
163  }
164 
165  if (!_ts5_status.graphics.fill_mode) {
166  al_draw_rectangle(x1, y1, x2, y2,
167  _ts5_status.graphics.foreground_color,
168  _ts5_status.graphics.drawing_thickness);
169  }
170  else {
171  al_draw_filled_rectangle(x1, y1, x2, y2,
172  _ts5_status.graphics.foreground_color);
173  }
174 }
175 
176 
177 ////////////////////////////////////////////////////////////////////////////////
178 /// Draw a rounded rectangle.
179 ///
180 /// \param x1 Horizontal position of the first corner of the rectangle.
181 /// \param y1 Vertical position of the first corner of the rectangle.
182 /// \param x2 Horizontal position of the second corner of the rectangle.
183 /// \param y2 Vertical position of the second corner of the rectangle.
184 /// \param rx Horizontal radius of the round.
185 /// \param ry Vertical radius of the round.
186 ////////////////////////////////////////////////////////////////////////////////
187 void ts5_draw_rounded_rectangle(double x1, double y1, double x2, double y2,
188  double rx, double ry)
189 {
190  ts5_check_primitives("ts5_draw_rounded_rectangle");
191  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_rounded_rectangle(%f,%f,%f,%f,%f,%f)\n",
192  x1, y1, x2, y2, rx, ry);
193 
194  if (_ts5_status.graphics.coordinate_scale
195  == TS5_RELATIVE_COORDINATES) {
202  }
203 
204  if (_ts5_status.graphics.coordinate_system
205  == TS5_CARTESIAN_COORDINATES) {
210  }
211 
212  if (x1 > x2) {
213  double t = x1;
214  x1 = x2;
215  x2 = t;
216  }
217 
218  if (y1 > y2) {
219  double t = y1;
220  y1 = y2;
221  y2 = t;
222  }
223 
224  double w = x2 - x1;
225 
226  if (rx>w/2.0) {
227  rx=w/2.0;
228  }
229 
230  double h = y2 - y1;
231 
232  if (ry>h/2.0) {
233  ry=h/2.0;
234  }
235 
236 
237  if (!_ts5_status.graphics.fill_mode) {
238  al_draw_rounded_rectangle(x1, y1, x2, y2, rx, ry,
239  _ts5_status.graphics.foreground_color,
240  _ts5_status.graphics.drawing_thickness);
241  }
242  else {
243  al_draw_filled_rounded_rectangle(x1, y1, x2, y2, rx, ry,
244  _ts5_status.graphics.foreground_color);
245  }
246 }
247 
248 
249 ////////////////////////////////////////////////////////////////////////////////
250 /// Draw a triangle.
251 ///
252 /// \param x1 Horizontal position of the first corner of the triangle.
253 /// \param y1 Vertical position of the first corner of the triangle.
254 /// \param x2 Horizontal position of the second corner of the triangle.
255 /// \param y2 Vertical position of the second corner of the triangle.
256 /// \param x3 Horizontal position of the third corner of the triangle.
257 /// \param y3 Vertical position of the third corner of the triangle.
258 ////////////////////////////////////////////////////////////////////////////////
259 void ts5_draw_triangle(double x1, double y1, double x2, double y2,
260  double x3, double y3)
261 {
262  ts5_check_primitives("ts5_draw_triangle");
263  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_triangle(%f,%f,%f,%f,%f,%f)\n",
264  x1, y1, x2, y2, x3, y3);
265 
266  if (_ts5_status.graphics.coordinate_scale
267  == TS5_RELATIVE_COORDINATES) {
274  }
275 
276  if (_ts5_status.graphics.coordinate_system
277  == TS5_CARTESIAN_COORDINATES) {
284  }
285 
286  if (!_ts5_status.graphics.fill_mode) {
287  al_draw_triangle(x1, y1, x2, y2, x3, y3,
288  _ts5_status.graphics.foreground_color,
289  _ts5_status.graphics.drawing_thickness);
290  }
291  else {
292  al_draw_filled_triangle(x1, y1, x2, y2, x3, y3,
293  _ts5_status.graphics.foreground_color);
294  }
295 }
296 
297 
298 ////////////////////////////////////////////////////////////////////////////////
299 /// Draw a circle.
300 ///
301 /// \param cx Horizontal position of the center of the circle.
302 /// \param cy Vertical position of the center of the circle.
303 /// \param r Radius of the circle.
304 ////////////////////////////////////////////////////////////////////////////////
305 void ts5_draw_circle(double cx, double cy, double r)
306 {
307  ts5_check_primitives("ts5_draw_circle");
308  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_circle(%f,%f,%f)\n", cx, cy, r);
309 
310  if (_ts5_status.graphics.coordinate_scale
311  == TS5_RELATIVE_COORDINATES) {
315  }
316 
317  if (_ts5_status.graphics.coordinate_system
318  == TS5_CARTESIAN_COORDINATES) {
321  }
322 
323  if (!_ts5_status.graphics.fill_mode) {
324  al_draw_circle(cx, cy, r, _ts5_status.graphics.foreground_color,
325  _ts5_status.graphics.drawing_thickness);
326  }
327  else {
328  al_draw_filled_circle(cx, cy, r,
329  _ts5_status.graphics.foreground_color);
330  }
331 }
332 
333 
334 ////////////////////////////////////////////////////////////////////////////////
335 /// Draw an ellipse.
336 ///
337 /// \param cx Horizontal position of the center of the ellipse.
338 /// \param cy Vertical position of the center of the ellipse.
339 /// \param rx Horizontal radius of the ellipse.
340 /// \param ry Vertical radius of the ellipse.
341 ////////////////////////////////////////////////////////////////////////////////
342 void ts5_draw_ellipse(double cx, double cy, double rx, double ry)
343 {
344  ts5_check_primitives("ts5_draw_ellipse");
345  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_ellipse(%f,%f,%f,%f)\n", cx, cy, rx, ry);
346 
347  if (_ts5_status.graphics.coordinate_scale
348  == TS5_RELATIVE_COORDINATES) {
353  }
354 
355  if (_ts5_status.graphics.coordinate_system
356  == TS5_CARTESIAN_COORDINATES) {
359  }
360 
361  if (!_ts5_status.graphics.fill_mode) {
362  al_draw_ellipse(cx, cy, rx, ry,
363  _ts5_status.graphics.foreground_color,
364  _ts5_status.graphics.drawing_thickness);
365  }
366  else {
367  al_draw_filled_ellipse(cx, cy, rx, ry,
368  _ts5_status.graphics.foreground_color);
369  }
370 }
371 
372 
373 ////////////////////////////////////////////////////////////////////////////////
374 /// Draw an arc.
375 ///
376 /// \param cx Horizontal position of the center of the arc.
377 /// \param cy Vertical position of the center of the arc.
378 /// \param r Radius of the arc.
379 /// \param start The initial angle of the arc (0.0 = west, 90.0 = north, ...).
380 /// \param delta Span of the arc (positive is counter clockwise,
381 /// negative is clockwise).
382 ////////////////////////////////////////////////////////////////////////////////
383 void ts5_draw_arc(double cx, double cy, double r, double start, double delta)
384 {
385  ts5_check_primitives("ts5_draw_arc");
386  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_arc(%f,%f,%f,%f,%f)\n",
387  cx, cy, r, start, delta);
388 
389  if (_ts5_status.graphics.coordinate_scale
390  == TS5_RELATIVE_COORDINATES) {
394  }
395 
396  if (_ts5_status.graphics.coordinate_system
397  == TS5_CARTESIAN_COORDINATES) {
400  }
401 
402  start = - start * TS5_PI / 180;
403  delta = - delta * TS5_PI / 180;
404  al_draw_arc(cx, cy, r, start, delta,
405  _ts5_status.graphics.foreground_color,
406  _ts5_status.graphics.drawing_thickness);
407 }
408 
409 
410 ////////////////////////////////////////////////////////////////////////////////
411 //@}
412 ////////////////////////////////////////////////////////////////////////////////