File: stroke.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (154 lines) | stat: -rw-r--r-- 3,791 bytes parent folder | download | duplicates (2)
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
/****************************************************************************
*
* MODULE:       Symbol library 
*   	    	
* AUTHOR(S):    Radim Blazek
*
* PURPOSE:      Stroke symbol
*
* COPYRIGHT:    (C) 2001 by the GRASS Development Team
*
*               This program is free software under the GNU General Public
*   	    	License (>=v2). Read the file COPYING that comes with GRASS
*   	    	for details.
*
*****************************************************************************/
#include <stdlib.h>
#include <math.h>
#include "gis.h"
#include "symbol.h"

#define PI 3.14159265

void 
add_coor ( SYMBCHAIN *chain, int x, int y)
{
    G_debug ( 5, "    add_coor %d, %d", x, y );
    if ( chain->scount == chain->salloc ) {
	chain->salloc += 10;
	chain->sx = (int *) G_realloc ( chain->sx, chain->salloc * sizeof(int) );
	chain->sy = (int *) G_realloc ( chain->sy, chain->salloc * sizeof(int) );
    }
    chain->sx[chain->scount] = x;
    chain->sy[chain->scount] = y;
    chain->scount++;
}

/* draw chain
*   s - scale
*   ch - chain number 
*/
int 
stroke_chain ( SYMBPART *part, int ch, double s ) 
{ 
    int k, l, first;
    SYMBEL *elem;
    SYMBCHAIN *chain;
    double r;
    double a1, a2, da;
    int x, y, x0, y0;

    G_debug ( 5, "  stroke_chain(): ch = %d", ch );
    chain = part->chain[ch];
    
    G_debug ( 5, "    element count = %d", chain->count );
    first = 1;
    for ( k = 0; k < chain->count; k++ ) {
	elem = chain->elem[k];
	switch ( elem->type ) {
	    case S_LINE:
                G_debug ( 5, "    LINE count = %d", elem->coor.line.count );
		for (l = 0; l < elem->coor.line.count; l++) {
		    add_coor ( chain, s * elem->coor.line.x[l], s * elem->coor.line.y[l] );
		    if ( first ) {
			x0 = s * elem->coor.line.x[l];
			y0 = s * elem->coor.line.y[l];
			first = 0;
		    }
		}
		break;
	    case S_ARC:
		da = 10 * PI / 180 ; /* later calc from size and tolerance */
		r = elem->coor.arc.r;
                G_debug ( 5, "    ARC da = %f r = %f", da, r );
		
	        /* convert to positive angles */
	        a1 = PI * elem->coor.arc.a1 / 180; 
	        if ( a1 < 0 ) a1 += 2 * PI; 
	        a2 = PI * elem->coor.arc.a2 / 180; 
	        if ( a2 < 0 ) a2 += 2 * PI; 

	        if ( elem->coor.arc.clock ) {	/* clockwise */
		    while (1) {
		        x = s * elem->coor.arc.x + s * r * cos(a1);
		        y = s * elem->coor.arc.y + s * r * sin(a1);
		        add_coor ( chain, x, y);
			if ( first ) {
			    x0 = x;
			    y0 = y;
			    first = 0;
			}
			if ( a1 == a2 ) break;
			a1 -= da;
			if ( a1 < a2 ) a1 = a2;
		    }

		} else { 
		    while (1) {
		        x = s * elem->coor.arc.x + s * r * cos(a1);
		        y = s * elem->coor.arc.y + s * r * sin(a1);
		        add_coor ( chain, x, y);
			if ( first ) {
			    x0 = x;
			    y0 = y;
			    first = 0;
			}
			if ( a1 == a2 ) break;
			a1 += da;
			if ( a1 > a2 ) a1 = a2;
		    }
	        }
		break;
	}
    }
    if ( part->type == S_POLYGON ) {
	add_coor ( chain, x0, y0); /* Close ring */
    }

    return 0;
}

/* 
*  Stroke symbol to form used for Xdriver.
*
*  rotation and tolerance currently not supported
*/
void
S_stroke ( SYMBOL *Symb, int size, double rotation, int tolerance )
{
    int i, j;
    double s;
    SYMBPART *part; 

    G_debug ( 3, "S_stroke(): size = %d rotation = %f tolerance = %d", size, rotation, tolerance );

    /* TODO: support for rotation and tolerance */

    s = size * Symb->scale;
    
    for ( i = 0; i < Symb->count; i++ ) { 
        G_debug ( 4, "  part %d", i );
	part = Symb->part[i];
	switch ( part->type ) {
	    case S_POLYGON: 
		for ( j = 0; j < part->count; j++ ) { /* RINGS */
		    stroke_chain ( part, j, s);
		}
		break;
	    case S_STRING: /* string has 1 chain */
		stroke_chain ( part, 0, s);
		break;
	}
    }
}