| 1 | – | /* Copyright (c) 1989 Regents of the University of California */ | 
| 2 | – |  | 
| 1 |  | #ifndef lint | 
| 2 | < | static char SCCSid[] = "$SunId$ LBL"; | 
| 2 | > | static const char       RCSid[] = "$Id$"; | 
| 3 |  | #endif | 
| 6 | – |  | 
| 4 |  | /* | 
| 5 |  | *  genbranch.c - program to generate 3D Christmas tree branches. | 
| 6 |  | * | 
| 9 |  |  | 
| 10 |  | #include  <stdio.h> | 
| 11 |  |  | 
| 12 | + | #include <stdlib.h> | 
| 13 | + |  | 
| 14 |  | #include  <math.h> | 
| 15 |  |  | 
| 16 |  | #include  "random.h" | 
| 31 |  |  | 
| 32 |  | double  var = 0.3;                      /* variability */ | 
| 33 |  |  | 
| 35 | – | #ifdef  DCL_ATOF | 
| 36 | – | extern double  atof(); | 
| 37 | – | #endif | 
| 34 |  |  | 
| 35 | + | static void | 
| 36 | + | stick(mat, beg, end, rad)               /* output a branch or leaf */ | 
| 37 | + | char  *mat; | 
| 38 | + | double  beg[3], end[3]; | 
| 39 | + | double  rad; | 
| 40 | + | { | 
| 41 | + | static int  nsticks = 0; | 
| 42 | + |  | 
| 43 | + | printf("\n%s cone s%d\n", mat, nsticks); | 
| 44 | + | printf("0\n0\n8\n"); | 
| 45 | + | printf("\t%18.12g\t%18.12g\t%18.12g\n", beg[0], beg[1], beg[2]); | 
| 46 | + | printf("\t%18.12g\t%18.12g\t%18.12g\n", end[0], end[1], end[2]); | 
| 47 | + | printf("\t%18.12g\t%18.12g\n", rad, bnarrow*rad); | 
| 48 |  |  | 
| 49 | + | printf("\n%s sphere e%d\n", mat, nsticks); | 
| 50 | + | printf("0\n0\n4"); | 
| 51 | + | printf("\t%18.12g\t%18.12g\t%18.12g\t%18.12g\n", | 
| 52 | + | end[0], end[1], end[2], bnarrow*rad); | 
| 53 | + |  | 
| 54 | + | nsticks++; | 
| 55 | + | } | 
| 56 | + |  | 
| 57 | + |  | 
| 58 | + | static void | 
| 59 | + | branch(beg, end, rad, lvl)              /* generate branch recursively */ | 
| 60 | + | double  beg[3], end[3]; | 
| 61 | + | double  rad; | 
| 62 | + | int  lvl; | 
| 63 | + | { | 
| 64 | + | double  sqrt(); | 
| 65 | + | double  newbeg[3], newend[3]; | 
| 66 | + | double  t; | 
| 67 | + | int  i, j; | 
| 68 | + |  | 
| 69 | + | if (lvl == 0) { | 
| 70 | + | stick(leafmat, beg, end, rad); | 
| 71 | + | return; | 
| 72 | + | } | 
| 73 | + |  | 
| 74 | + | stick(branchmat, beg, end, rad); | 
| 75 | + |  | 
| 76 | + | for (i = 1; i <= nshoots; i++) { | 
| 77 | + | /* right branch */ | 
| 78 | + | t = (i+errf())/(nshoots+2); | 
| 79 | + | t = (t + sqrt(t))/2.0; | 
| 80 | + | for (j = 0; j < 3; j++) { | 
| 81 | + | newbeg[j] = newend[j] = (end[j]-beg[j])*t + beg[j]; | 
| 82 | + | newend[j] += (end[j]-beg[j])*(1+errf())/(nshoots+2); | 
| 83 | + | } | 
| 84 | + | newend[0] += (end[2]-newbeg[2])*bratio*(1+errf()); | 
| 85 | + | newend[1] += (end[1]-newbeg[1])*bratio*(1+errf()); | 
| 86 | + | newend[2] -= (end[0]-newbeg[0])*bratio*(1+errf()); | 
| 87 | + | branch(newbeg, newend, | 
| 88 | + | (1-(1-bnarrow)*t)*(1+2*bratio)/3*rad, lvl-1); | 
| 89 | + |  | 
| 90 | + | /* left branch */ | 
| 91 | + | t = (i+errf())/(nshoots+2); | 
| 92 | + | t = (t + sqrt(t))/2.0; | 
| 93 | + | for (j = 0; j < 3; j++) { | 
| 94 | + | newbeg[j] = newend[j] = (end[j]-beg[j])*t + beg[j]; | 
| 95 | + | newend[j] += (end[j]-beg[j])*(1+errf())/(nshoots+2); | 
| 96 | + | } | 
| 97 | + | newend[0] -= (end[2]-newbeg[2])*bratio*(1+errf()); | 
| 98 | + | newend[1] += (end[1]-newbeg[1])*bratio*(1+errf()); | 
| 99 | + | newend[2] += (end[0]-newbeg[0])*bratio*(1+errf()); | 
| 100 | + | branch(newbeg, newend, | 
| 101 | + | (1-(1-bnarrow)*t)*(1+2*bratio)/3*rad, lvl-1); | 
| 102 | + | } | 
| 103 | + | } | 
| 104 | + |  | 
| 105 | + |  | 
| 106 | + | printhead(ac, av)               /* print command header */ | 
| 107 | + | register int  ac; | 
| 108 | + | register char  **av; | 
| 109 | + | { | 
| 110 | + | putchar('#'); | 
| 111 | + | while (ac--) { | 
| 112 | + | putchar(' '); | 
| 113 | + | fputs(*av++, stdout); | 
| 114 | + | } | 
| 115 | + | putchar('\n'); | 
| 116 | + | } | 
| 117 | + |  | 
| 118 | + |  | 
| 119 |  | main(argc, argv) | 
| 120 |  | int  argc; | 
| 121 |  | char  *argv[]; | 
| 192 |  | return(0); | 
| 193 |  | } | 
| 194 |  |  | 
| 116 | – |  | 
| 117 | – | branch(beg, end, rad, lvl)              /* generate branch recursively */ | 
| 118 | – | double  beg[3], end[3]; | 
| 119 | – | double  rad; | 
| 120 | – | int  lvl; | 
| 121 | – | { | 
| 122 | – | double  sqrt(); | 
| 123 | – | double  newbeg[3], newend[3]; | 
| 124 | – | double  t; | 
| 125 | – | int  i, j; | 
| 126 | – |  | 
| 127 | – | if (lvl == 0) { | 
| 128 | – | stick(leafmat, beg, end, rad); | 
| 129 | – | return; | 
| 130 | – | } | 
| 131 | – |  | 
| 132 | – | stick(branchmat, beg, end, rad); | 
| 133 | – |  | 
| 134 | – | for (i = 1; i <= nshoots; i++) { | 
| 135 | – | /* right branch */ | 
| 136 | – | t = (i+errf())/(nshoots+2); | 
| 137 | – | t = (t + sqrt(t))/2.0; | 
| 138 | – | for (j = 0; j < 3; j++) { | 
| 139 | – | newbeg[j] = newend[j] = (end[j]-beg[j])*t + beg[j]; | 
| 140 | – | newend[j] += (end[j]-beg[j])*(1+errf())/(nshoots+2); | 
| 141 | – | } | 
| 142 | – | newend[0] += (end[2]-newbeg[2])*bratio*(1+errf()); | 
| 143 | – | newend[1] += (end[1]-newbeg[1])*bratio*(1+errf()); | 
| 144 | – | newend[2] -= (end[0]-newbeg[0])*bratio*(1+errf()); | 
| 145 | – | branch(newbeg, newend, | 
| 146 | – | (1-(1-bnarrow)*t)*(1+2*bratio)/3*rad, lvl-1); | 
| 147 | – |  | 
| 148 | – | /* left branch */ | 
| 149 | – | t = (i+errf())/(nshoots+2); | 
| 150 | – | t = (t + sqrt(t))/2.0; | 
| 151 | – | for (j = 0; j < 3; j++) { | 
| 152 | – | newbeg[j] = newend[j] = (end[j]-beg[j])*t + beg[j]; | 
| 153 | – | newend[j] += (end[j]-beg[j])*(1+errf())/(nshoots+2); | 
| 154 | – | } | 
| 155 | – | newend[0] -= (end[2]-newbeg[2])*bratio*(1+errf()); | 
| 156 | – | newend[1] += (end[1]-newbeg[1])*bratio*(1+errf()); | 
| 157 | – | newend[2] += (end[0]-newbeg[0])*bratio*(1+errf()); | 
| 158 | – | branch(newbeg, newend, | 
| 159 | – | (1-(1-bnarrow)*t)*(1+2*bratio)/3*rad, lvl-1); | 
| 160 | – | } | 
| 161 | – | } | 
| 162 | – |  | 
| 163 | – |  | 
| 164 | – | stick(mat, beg, end, rad)               /* output a branch or leaf */ | 
| 165 | – | char  *mat; | 
| 166 | – | double  beg[3], end[3]; | 
| 167 | – | double  rad; | 
| 168 | – | { | 
| 169 | – | static int  nsticks = 0; | 
| 170 | – |  | 
| 171 | – | printf("\n%s cone s%d\n", mat, nsticks); | 
| 172 | – | printf("0\n0\n8\n"); | 
| 173 | – | printf("\t%18.12g\t%18.12g\t%18.12g\n", beg[0], beg[1], beg[2]); | 
| 174 | – | printf("\t%18.12g\t%18.12g\t%18.12g\n", end[0], end[1], end[2]); | 
| 175 | – | printf("\t%18.12g\t%18.12g\n", rad, bnarrow*rad); | 
| 176 | – |  | 
| 177 | – | printf("\n%s sphere e%d\n", mat, nsticks); | 
| 178 | – | printf("0\n0\n4"); | 
| 179 | – | printf("\t%18.12g\t%18.12g\t%18.12g\%18.12g\n", | 
| 180 | – | end[0], end[1], end[2], bnarrow*rad); | 
| 181 | – |  | 
| 182 | – | nsticks++; | 
| 183 | – | } | 
| 184 | – |  | 
| 185 | – |  | 
| 186 | – | printhead(ac, av)               /* print command header */ | 
| 187 | – | register int  ac; | 
| 188 | – | register char  **av; | 
| 189 | – | { | 
| 190 | – | putchar('#'); | 
| 191 | – | while (ac--) { | 
| 192 | – | putchar(' '); | 
| 193 | – | fputs(*av++, stdout); | 
| 194 | – | } | 
| 195 | – | putchar('\n'); | 
| 196 | – | } |