1 /* getCoords.c */
2
3 #include "../Tree.h"
4
5 #define MYDEBUG 0
6
7 /*--------------------------------------------------------------------*/
8 /*
9 ------------------------------------------------
10 purpose -- to get simple x[] and y[] coordinates
11 for the tree vertices
12
13 return values --
14 1 -- normal return
15 -1 -- tree is NULL
16 -2 -- heightflag is invalid
17 -3 -- coordflag is invalid
18 -4 -- xDV is NULL
19 -5 -- yDV is NULL
20
21 created -- 99jan07, cca
22 ------------------------------------------------
23 */
24 int
Tree_getSimpleCoords(Tree * tree,char heightflag,char coordflag,DV * xDV,DV * yDV)25 Tree_getSimpleCoords (
26 Tree *tree,
27 char heightflag,
28 char coordflag,
29 DV *xDV,
30 DV *yDV
31 ) {
32 double *x, *y ;
33 int count, I, J, n, nleaves ;
34 int *fch, *par, *sib ;
35 /*
36 ---------------
37 check the input
38 ---------------
39 */
40 if ( tree == NULL ) {
41 fprintf(stderr, "\n error in Tree_getSimpleCoords()"
42 "\n tree is NULL\n") ;
43 return(-1) ;
44 }
45 if ( heightflag != 'D' && heightflag != 'H' ) {
46 fprintf(stderr, "\n error in Tree_getSimpleCoords()"
47 "\n invalid heightflag = %c\n", heightflag) ;
48 return(-2) ;
49 }
50 if ( coordflag != 'C' && coordflag != 'P' ) {
51 fprintf(stderr, "\n error in Tree_getSimpleCoords()"
52 "\n invalid coordflag = %c\n", coordflag) ;
53 return(-3) ;
54 }
55 if ( xDV == NULL ) {
56 fprintf(stderr, "\n error in Tree_getSimpleCoords()"
57 "\n xDV is NULL\n") ;
58 return(-4) ;
59 }
60 if ( yDV == NULL ) {
61 fprintf(stderr, "\n error in Tree_getSimpleCoords()"
62 "\n yDV is NULL\n") ;
63 return(-5) ;
64 }
65 n = tree->n ;
66 par = tree->par ;
67 fch = tree->fch ;
68 sib = tree->sib ;
69 DV_setSize(xDV, n) ;
70 DV_setSize(yDV, n) ;
71 x = DV_entries(xDV) ;
72 y = DV_entries(yDV) ;
73 switch ( heightflag ) {
74 case 'D' : {
75 int J, K, maxdepth ;
76
77 for ( J = Tree_preOTfirst(tree), maxdepth = 0 ;
78 J != -1 ;
79 J = Tree_preOTnext(tree, J) ) {
80 if ( (K = par[J]) == -1 ) {
81 y[J] = 0.0 ;
82 } else {
83 y[J] = y[K] + 1.0 ;
84 }
85 if ( maxdepth < y[J] ) {
86 maxdepth = y[J] ;
87 }
88 }
89 if ( coordflag == 'C' ) {
90 for ( J = 0 ; J < n ; J++ ) {
91 y[J] = maxdepth - y[J] ;
92 }
93 }
94 } break ;
95 case 'H' : {
96 int height, I, J, maxheight ;
97
98 for ( J = Tree_postOTfirst(tree), maxheight = 0 ;
99 J != -1 ;
100 J = Tree_postOTnext(tree, J) ) {
101 if ( (I = fch[J]) == -1 ) {
102 y[J] = 0.0 ;
103 } else {
104 height = y[I] ;
105 for ( I = sib[I] ; I != -1 ; I = sib[I] ) {
106 if ( height < y[I] ) {
107 height = y[I] ;
108 }
109 }
110 y[J] = height + 1.0 ;
111 }
112 if ( maxheight < y[J] ) {
113 maxheight = y[J] ;
114 }
115 }
116 if ( coordflag == 'P' ) {
117 for ( J = 0 ; J < n ; J++ ) {
118 y[J] = maxheight - y[J] ;
119 }
120 }
121 } break ;
122 default :
123 break ;
124 }
125 #if MYDEBUG > 0
126 fprintf(stdout, "\n\n y") ;
127 DV_writeForHumanEye(yDV, stdout) ;
128 #endif
129 DV_zero(xDV) ;
130 nleaves = 0 ;
131 for ( J = Tree_postOTfirst(tree) ;
132 J != -1 ;
133 J = Tree_postOTnext(tree, J) ) {
134 if ( fch[J] == -1 ) {
135 x[J] = nleaves++ ;
136 } else {
137 for ( I = fch[J], count = 0 ; I != -1 ; I = sib[I] ) {
138 x[J] += x[I] ;
139 count++ ;
140 }
141 x[J] /= count ;
142 }
143 }
144 if ( coordflag == 'C' ) {
145 for ( J = 0 ; J < n ; J++ ) {
146 x[J] = x[J] / nleaves ;
147 }
148 } else {
149 double r, theta ;
150
151 for ( J = 0 ; J < n ; J++ ) {
152 theta = 6.283185 * x[J] / nleaves ;
153 r = y[J] ;
154 x[J] = r * cos(theta) ;
155 y[J] = r * sin(theta) ;
156 }
157 }
158 #if MYDEBUG > 0
159 fprintf(stdout, "\n\n x") ;
160 DV_writeForHumanEye(xDV, stdout) ;
161 #endif
162 return(1) ; }
163
164 /*--------------------------------------------------------------------*/
165