1 /*****************************************************************************
4 ** Name: sgp.c Simple Graphics Package
5 ** Programmer: Kevin Rosenberg
7 ** This is part of the CTSim program
8 ** Copyright (C) 1983-2000 Kevin Rosenberg
10 ** $Id: sgp.cpp,v 1.4 2000/07/11 10:32:44 kevin Exp $
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License (version 2) as
14 ** published by the Free Software Foundation.
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ******************************************************************************/
28 #include "ctsupport.h"
32 static SGP_ID _sgp2_cwin = NULL;
34 extern CHARSPEC cspec;
38 * sgp2_init Initialize 2 graphics system
45 sgp2_init (int xsize, int ysize, const char *win_title )
57 gid->pw_xsize = xsize;
58 gid->pw_ysize = ysize;
59 strncpy(gid->title, win_title, sizeof(gid->title));
61 gid->recalc_mc_to_ndc = TRUE;
62 gid->recalc_ndc_to_mc = TRUE;
63 ident_gmtx_2 (gid->wc_to_ndc_x);
64 ident_gmtx_2 (gid->mc_to_ndc_x);
65 ident_gmtx_2 (gid->ndc_to_mc_x);
66 ident_gmtx_2 (gid->ctm_2_x);
68 sgp2_window (0., 0., 1., 1.);
69 sgp2_viewport (0., 0., 1., 1.);
71 sgp2_move_abs (0., 0.);
74 gid->g2_id = g2_open_X11X (gid->pw_xsize, gid->pw_ysize, 10, 10, gid->title, gid->title, NULL, -1, -1);
77 _sgp2_init_dev (_sgp2_cwin);
84 sgp2_close (SGP_ID gid)
88 g2_close (gid->g2_id);
90 if (gid == _sgp2_cwin)
97 sgp2_set_active_win (SGP_ID gid)
103 sgp2_get_active_win (void)
110 * sgp2_clear Clear window
117 if (_sgp_cwin != NULL)
118 g2_clear (gid->g2_id);
123 * sgp2_window Set window in world coordinates
127 sgp2_window (double xmin, double ymin, double xmax, double ymax)
129 if (_sgp2_cwin == NULL)
132 if (xmin >= xmax || ymin >= ymax) {
133 sys_error (ERR_WARNING, "Minimum > Maximum [sgp2_window]");
137 _sgp2_cwin->xw_min = xmin;
138 _sgp2_cwin->yw_min = ymin;
139 _sgp2_cwin->xw_max = xmax;
140 _sgp2_cwin->yw_max = ymax;
146 * sgp2_viewport Set viewport in NDC
150 sgp2_viewport (double xmin, double ymin, double xmax, double ymax)
152 if (_sgp2_cwin == NULL)
155 if (xmin >= xmax || ymin >= ymax) {
156 sys_error (ERR_WARNING, "Minimum > Maximum [sgp2_viewport]");
160 _sgp2_cwin->xv_min = xmin;
161 _sgp2_cwin->yv_min = ymin;
162 _sgp2_cwin->xv_max = xmax;
163 _sgp2_cwin->yv_max = ymax;
166 _sgp2_cwin->view[0] = xmin; /* Array for clip_rect() */
167 _sgp2_cwin->view[1] = ymin;
168 _sgp2_cwin->view[2] = xmax;
169 _sgp2_cwin->view[3] = ymax;
174 * sgp2_frame_vpt draw box around viewport
178 sgp2_frame_vpt (void)
180 if (_sgp2_cwin == NULL)
183 _sgp2_stylus (_sgp2_cwin, _sgp2_cwin->xv_min, _sgp2_cwin->yv_min, 0);
184 _sgp2_stylus (_sgp2_cwin, _sgp2_cwin->xv_max, _sgp2_cwin->yv_min, 1);
185 _sgp2_stylus (_sgp2_cwin, _sgp2_cwin->xv_max, _sgp2_cwin->yv_max, 1);
186 _sgp2_stylus (_sgp2_cwin, _sgp2_cwin->xv_min, _sgp2_cwin->yv_max, 1);
187 _sgp2_stylus (_sgp2_cwin, _sgp2_cwin->xv_min, _sgp2_cwin->yv_min, 1);
192 * calc_wc_to_ndc Calculate transform matrix
196 calc_wc_to_ndc (void)
200 if (_sgp2_cwin == NULL)
203 sx = (_sgp2_cwin->xv_max - _sgp2_cwin->xv_min) / (_sgp2_cwin->xw_max - _sgp2_cwin->xw_min);
204 sy = (_sgp2_cwin->yv_max - _sgp2_cwin->yv_min) / (_sgp2_cwin->yw_max - _sgp2_cwin->yw_min);
206 ident_gmtx_2 (_sgp2_cwin->wc_to_ndc_x);
207 _sgp2_cwin->wc_to_ndc_x[0][0] = sx;
208 _sgp2_cwin->wc_to_ndc_x[2][0] = _sgp2_cwin->xv_min - sx * _sgp2_cwin->xw_min;
209 _sgp2_cwin->wc_to_ndc_x[1][1] = sy;
210 _sgp2_cwin->wc_to_ndc_x[2][1] = _sgp2_cwin->yv_min - sy * _sgp2_cwin->yw_min;
212 _sgp2_cwin->recalc_mc_to_ndc = TRUE;
213 _sgp2_cwin->recalc_ndc_to_mc = TRUE;
218 calc_ndc_to_mc (void)
220 if (_sgp2_cwin == NULL)
223 if (_sgp2_cwin->recalc_mc_to_ndc) {
224 mult_gmtx_2 (_sgp2_cwin->ctm_2_x, _sgp2_cwin->wc_to_ndc_x, _sgp2_cwin->mc_to_ndc_x);
225 _sgp2_cwin->recalc_mc_to_ndc = FALSE;
228 invert_gmtx_2 (_sgp2_cwin->mc_to_ndc_x, _sgp2_cwin->ndc_to_mc_x);
229 _sgp2_cwin->recalc_ndc_to_mc = FALSE;
234 * wc_to_ndc Map from world coordinates to NDC
238 wc_to_ndc (double xw, double yw, double *xn, double *yn)
240 if (_sgp2_cwin == NULL)
243 if (_sgp2_cwin->recalc_mc_to_ndc) {
244 mult_gmtx_2 (_sgp2_cwin->ctm_2_x, _sgp2_cwin->wc_to_ndc_x, _sgp2_cwin->mc_to_ndc_x);
245 _sgp2_cwin->recalc_mc_to_ndc = FALSE;
248 *xn = xw * _sgp2_cwin->mc_to_ndc_x[0][0] + yw * _sgp2_cwin->mc_to_ndc_x[1][0] + _sgp2_cwin->mc_to_ndc_x[2][0];
249 *yn = xw * _sgp2_cwin->mc_to_ndc_x[0][1] + yw * _sgp2_cwin->mc_to_ndc_x[1][1] + _sgp2_cwin->mc_to_ndc_x[2][1];
253 /*==============================================================*/
254 /* map from normalized device coords. to world coordinates */
255 /*==============================================================*/
257 ndc_to_wc (double xn, double yn, double *xw, double *yw)
259 if (_sgp2_cwin == NULL)
262 if (_sgp2_cwin->recalc_ndc_to_mc) {
264 _sgp2_cwin->recalc_ndc_to_mc = FALSE;
267 *xw = xn * _sgp2_cwin->ndc_to_mc_x[0][0] + yn * _sgp2_cwin->ndc_to_mc_x[1][0] + _sgp2_cwin->ndc_to_mc_x[2][0];
268 *yw = xn * _sgp2_cwin->ndc_to_mc_x[0][1] + yn * _sgp2_cwin->ndc_to_mc_x[1][1] + _sgp2_cwin->ndc_to_mc_x[2][1];
272 /*==============================================================*/
274 /*==============================================================*/
276 sgp2_color (int icol)
281 /*==============================================================*/
282 /* set line style. Pass 16 bit repeating pattern */
283 /*==============================================================*/
285 sgp2_line_style (int style)
290 /*==============================================================*/
291 /* absolute draw to */
292 /*==============================================================*/
294 sgp2_line_abs (double x, double y)
296 double x1, y1, x2, y2;
298 if (_sgp2_cwin == NULL)
301 wc_to_ndc (_sgp2_cwin->curx, _sgp2_cwin->cury, &x1, &y1);
302 wc_to_ndc (x, y, &x2, &y2);
304 if (clip_rect (x1, y1, x2, y2, _sgp2_cwin->view) == TRUE) { /* clip to viewport */
305 _sgp2_stylus (_sgp2_cwin, x1, y1, 0); /* move to first point */
306 _sgp2_stylus (_sgp2_cwin, x2, y2, 1); /* draw to second point */
309 _sgp2_cwin->curx = x;
310 _sgp2_cwin->cury = y;
313 /*==============================================================*/
314 /* absolute move to */
315 /*==============================================================*/
317 sgp2_move_abs (double x, double y)
319 if (_sgp2_cwin == NULL)
322 _sgp2_cwin->curx = x;
323 _sgp2_cwin->cury = y; /* moves are not clipped */
326 /*==============================================================*/
328 /*==============================================================*/
330 sgp2_line_rel (double x, double y)
332 if (_sgp2_cwin != NULL)
333 sgp2_line_abs (x + _sgp2_cwin->curx, y + _sgp2_cwin->cury);
336 /*==============================================================*/
338 /*==============================================================*/
340 sgp2_move_rel (double x, double y)
342 if (_sgp2_cwin != NULL)
343 sgp2_move_abs (x + _sgp2_cwin->curx, y + _sgp2_cwin->cury);
346 /*==============================================================*/
348 /*==============================================================*/
350 sgp2_draw_text (char *message)
353 if (_sgp2_cwin == NULL)
356 wc_to_ndc (_sgp2_cwin->curx, _sgp2_cwin->cury, &sx, &sy);
357 _sgp2_stylus (_sgp2_cwin, sx, sy, 0); /* move to location */
358 _sgp2_dev_text (_sgp2_cwin, message);
362 charsize (double wid, double height)
364 _sgp2_set_text (_sgp2_cwin, wid, height, cspec.textangle, cspec.font);
368 textangle (double angle)
370 _sgp2_set_text (_sgp2_cwin, cspec.width, cspec.height, angle, cspec.font);
374 sgp2_polyline_abs (double x[], double y[], int n)
376 double x1, y1, x2, y2;
380 if (_sgp2_cwin == NULL || n < 2)
383 wc_to_ndc (x[0], y[0], &x1, &y1);
384 wc_to_ndc (x[1], y[1], &x2, &y2);
386 xt = x2; /* don't pass (x2,y2) to clip, we need them */
387 yt = y2; /* as the beginning point of the next line */
389 if (clip_rect (x1, y1, xt, yt, _sgp2_cwin->view) == TRUE) {
390 _sgp2_stylus (_sgp2_cwin, x1, y1, 0);
391 _sgp2_stylus (_sgp2_cwin, xt, yt, 1);
394 for (i = 2; i < n; i++) {
395 x1 = x2; /* NDC endpoint of last line */
397 wc_to_ndc (x[i], y[i], &x2, &y2);
400 if (clip_rect (x1, y1, xt, yt, _sgp2_cwin->view) == TRUE) {
401 _sgp2_stylus (_sgp2_cwin, x1, y1, 0);
402 _sgp2_stylus (_sgp2_cwin, xt, yt, 1);
409 sgp2_mark_abs (double x, double y)
413 if (_sgp2_cwin == NULL)
416 wc_to_ndc (x, y, &xndc, &yndc);
417 markndc (_sgp2_cwin, xndc, yndc);
418 _sgp2_cwin->curx = x;
419 _sgp2_cwin->cury = y;
424 sgp2_mark_rel (double x, double y)
426 sgp2_mark_abs (x + _sgp2_cwin->curx, y + _sgp2_cwin->cury);
431 sgp2_point_abs (double x, double y)
435 if (_sgp2_cwin == NULL)
438 wc_to_ndc (x, y, &xndc, &yndc);
439 pntndc (_sgp2_cwin, xndc, yndc);
440 _sgp2_cwin->curx = x;
441 _sgp2_cwin->cury = y;
446 sgp2_point_rel (double x, double y)
448 sgp2_point_abs (x + _sgp2_cwin->curx, y + _sgp2_cwin->cury);
453 * sgp2_draw_rect Draw box in graphics mode
456 * drawbox (xmin, ymin, xmax, ymax)
457 * double xmin, ymin Lower left corner of box
458 * double xmax, ymax Upper left corner of box
461 * This routine leaves the current position of graphic cursor at lower
462 * left corner of box.
466 sgp2_draw_rect(double xmin, double ymin, double xmax, double ymax)
468 sgp2_move_abs (xmin, ymin);
469 sgp2_line_abs (xmax, ymin);
470 sgp2_line_abs (xmax, ymax);
471 sgp2_line_abs (xmin, ymax);
472 sgp2_line_abs (xmin, ymin);
476 * sgp2_circle - draw circle of radius r at current center
480 sgp2_draw_circle (const double r)
482 sgp2_draw_arc (0.0, 7.0, r);
485 /*==============================================================*/
486 /* draw arc around current center. pass angles and radius */
487 /*==============================================================*/
490 sgp2_draw_arc (double start, double stop, const double r)
492 double c, s, theta, angle;
495 if ((stop-start) > 2 * PI)
496 stop = start + 2 * PI;
497 if ((start-stop) > 2 * PI)
498 stop = start + 2 * PI;
499 while (start >= stop)
502 x = r * cos ((double) start);
503 y = r * sin ((double) start);
504 sgp2_move_rel (x, y); /* move from center to start of arc */
506 theta = 5 * PI / 180;
510 for (angle = start; angle < stop - theta; angle += theta) {
513 sgp2_line_rel (xp - x, yp - y);
517 c = cos (stop - angle);
518 s = sin (stop - angle);
521 sgp2_line_rel (xp - x, yp - y);
523 x = r * cos ((double) stop);
524 y = r * sin ((double) stop);
525 sgp2_move_rel (-x, -y); /* move back to center of circle */
529 /*----------------------------------------------------------------------*/
530 /* Current Transformation Matrix Routine */
531 /*----------------------------------------------------------------------*/
535 * ctm_clr_2 Clear current transformation matrix
556 * OUT GRFMTX_2D m Copy of ctm
560 ctm_get_2 (GRFMTX_2D m)
564 if (_sgp2_cwin == NULL)
567 for (x = 0; x < 3; x++)
568 for (y = 0; y < 3; y++)
569 m[x][y] = _sgp2_cwin->ctm_2_x[x][y];
574 * ctm_set_2 Set ctm to a matrix
578 * IN GRFMTX m New ctm
582 ctm_set_2 (GRFMTX_2D m)
585 if (_sgp2_cwin == NULL)
588 for (x = 0; x < 3; x++)
589 for (y = 0; y < 3; y++)
590 _sgp2_cwin->ctm_2_x[x][y] = m[x][y];
592 _sgp2_cwin->recalc_ndc_to_mc = TRUE;
593 _sgp2_cwin->recalc_mc_to_ndc = TRUE;
598 ctm_pre_mult_2 (GRFMTX_2D m)
602 mult_gmtx_2 (m, _sgp2_cwin->ctm_2_x, new_ctm);
608 ctm_post_mult_2 (GRFMTX_2D m)
612 mult_gmtx_2 (_sgp2_cwin->ctm_2_x, m, new_ctm);