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" |
32 |
|
double var = 0.3; /* variability */ |
33 |
|
|
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 |
|
|
112 |
– |
|
113 |
– |
branch(beg, end, rad, lvl) /* generate branch recursively */ |
114 |
– |
double beg[3], end[3]; |
115 |
– |
double rad; |
116 |
– |
int lvl; |
117 |
– |
{ |
118 |
– |
double sqrt(); |
119 |
– |
double newbeg[3], newend[3]; |
120 |
– |
double t; |
121 |
– |
int i, j; |
122 |
– |
|
123 |
– |
if (lvl == 0) { |
124 |
– |
stick(leafmat, beg, end, rad); |
125 |
– |
return; |
126 |
– |
} |
127 |
– |
|
128 |
– |
stick(branchmat, beg, end, rad); |
129 |
– |
|
130 |
– |
for (i = 1; i <= nshoots; i++) { |
131 |
– |
/* right branch */ |
132 |
– |
t = (i+errf())/(nshoots+2); |
133 |
– |
t = (t + sqrt(t))/2.0; |
134 |
– |
for (j = 0; j < 3; j++) { |
135 |
– |
newbeg[j] = newend[j] = (end[j]-beg[j])*t + beg[j]; |
136 |
– |
newend[j] += (end[j]-beg[j])*(1+errf())/(nshoots+2); |
137 |
– |
} |
138 |
– |
newend[0] += (end[2]-newbeg[2])*bratio*(1+errf()); |
139 |
– |
newend[1] += (end[1]-newbeg[1])*bratio*(1+errf()); |
140 |
– |
newend[2] -= (end[0]-newbeg[0])*bratio*(1+errf()); |
141 |
– |
branch(newbeg, newend, |
142 |
– |
(1-(1-bnarrow)*t)*(1+2*bratio)/3*rad, lvl-1); |
143 |
– |
|
144 |
– |
/* left branch */ |
145 |
– |
t = (i+errf())/(nshoots+2); |
146 |
– |
t = (t + sqrt(t))/2.0; |
147 |
– |
for (j = 0; j < 3; j++) { |
148 |
– |
newbeg[j] = newend[j] = (end[j]-beg[j])*t + beg[j]; |
149 |
– |
newend[j] += (end[j]-beg[j])*(1+errf())/(nshoots+2); |
150 |
– |
} |
151 |
– |
newend[0] -= (end[2]-newbeg[2])*bratio*(1+errf()); |
152 |
– |
newend[1] += (end[1]-newbeg[1])*bratio*(1+errf()); |
153 |
– |
newend[2] += (end[0]-newbeg[0])*bratio*(1+errf()); |
154 |
– |
branch(newbeg, newend, |
155 |
– |
(1-(1-bnarrow)*t)*(1+2*bratio)/3*rad, lvl-1); |
156 |
– |
} |
157 |
– |
} |
158 |
– |
|
159 |
– |
|
160 |
– |
stick(mat, beg, end, rad) /* output a branch or leaf */ |
161 |
– |
char *mat; |
162 |
– |
double beg[3], end[3]; |
163 |
– |
double rad; |
164 |
– |
{ |
165 |
– |
static int nsticks = 0; |
166 |
– |
|
167 |
– |
printf("\n%s cone s%d\n", mat, nsticks); |
168 |
– |
printf("0\n0\n8\n"); |
169 |
– |
printf("\t%18.12g\t%18.12g\t%18.12g\n", beg[0], beg[1], beg[2]); |
170 |
– |
printf("\t%18.12g\t%18.12g\t%18.12g\n", end[0], end[1], end[2]); |
171 |
– |
printf("\t%18.12g\t%18.12g\n", rad, bnarrow*rad); |
172 |
– |
|
173 |
– |
printf("\n%s sphere e%d\n", mat, nsticks); |
174 |
– |
printf("0\n0\n4"); |
175 |
– |
printf("\t%18.12g\t%18.12g\t%18.12g\%18.12g\n", |
176 |
– |
end[0], end[1], end[2], bnarrow*rad); |
177 |
– |
|
178 |
– |
nsticks++; |
179 |
– |
} |
180 |
– |
|
181 |
– |
|
182 |
– |
printhead(ac, av) /* print command header */ |
183 |
– |
register int ac; |
184 |
– |
register char **av; |
185 |
– |
{ |
186 |
– |
putchar('#'); |
187 |
– |
while (ac--) { |
188 |
– |
putchar(' '); |
189 |
– |
fputs(*av++, stdout); |
190 |
– |
} |
191 |
– |
putchar('\n'); |
192 |
– |
} |