1 | /* |
---|
2 | * see COPYRIGHT |
---|
3 | */ |
---|
4 | |
---|
5 | #include <stdio.h> |
---|
6 | #include <stdlib.h> |
---|
7 | #include "bzscreen.h" |
---|
8 | |
---|
9 | /* |
---|
10 | * functions to draw the bezier curves in text mode |
---|
11 | */ |
---|
12 | |
---|
13 | double |
---|
14 | fmin(a,b) |
---|
15 | double a, b; |
---|
16 | { |
---|
17 | if(a<b) |
---|
18 | return a; |
---|
19 | else |
---|
20 | return b; |
---|
21 | } |
---|
22 | |
---|
23 | int |
---|
24 | abs(x) |
---|
25 | int x; |
---|
26 | { |
---|
27 | if(x<0) |
---|
28 | return -x; |
---|
29 | else |
---|
30 | return x; |
---|
31 | } |
---|
32 | |
---|
33 | void |
---|
34 | initscreen(physx, physy, cols, rows, xoff, yoff, minx, miny, maxx, maxy) |
---|
35 | unsigned physx, physy, cols, rows, xoff, yoff, minx, miny, maxx, maxy; |
---|
36 | { |
---|
37 | int i,j; |
---|
38 | double yxscale; |
---|
39 | |
---|
40 | if(screen.dots != NULL) |
---|
41 | free(screen.dots); |
---|
42 | |
---|
43 | if(physx==0 || physy==0 || rows==0 || cols==0) { |
---|
44 | fprintf(stderr, "*** negative or zero screen size\n"); |
---|
45 | exit(1); |
---|
46 | } |
---|
47 | |
---|
48 | if(physx+xoff > cols || physy+yoff > rows) { |
---|
49 | fprintf(stderr, "*** drawable area out of screen\n"); |
---|
50 | exit(1); |
---|
51 | } |
---|
52 | |
---|
53 | if(minx>maxx || miny>maxy) { |
---|
54 | fprintf(stderr, "*** empty drawable area\n"); |
---|
55 | exit(1); |
---|
56 | } |
---|
57 | |
---|
58 | screen.physx = physx; |
---|
59 | screen.physy = physy; |
---|
60 | screen.rows = rows; |
---|
61 | screen.cols = cols+2; /* for '\n\0' */ |
---|
62 | screen.xoff = xoff; |
---|
63 | screen.yoff = yoff; |
---|
64 | screen.minx = minx; |
---|
65 | screen.miny = miny; |
---|
66 | |
---|
67 | if(( screen.dots=malloc(screen.rows*screen.cols) )==NULL) { |
---|
68 | perror("*** no memory for screen: "); |
---|
69 | exit(1); |
---|
70 | } |
---|
71 | |
---|
72 | j=screen.rows*screen.cols; |
---|
73 | for(i=0; i<j; i++) |
---|
74 | screen.dots[i]=' '; |
---|
75 | |
---|
76 | /* scale of Y to X on the screen, i.e. x=YXSCALE*y */ |
---|
77 | /* 3/4 is the approx. ratio of Y/X sizes of the physical screen */ |
---|
78 | yxscale = ((double)physx/(double)physy*3.0/4.0); |
---|
79 | |
---|
80 | /* scale of "logical" to "physical", i.e. physical=PHYSSCALE*logical */ |
---|
81 | screen.yscale = fmin( ((double)physy-0.51)/(maxy+1-miny), |
---|
82 | ((double)physx-0.51)/yxscale/(maxx+1-minx) ); |
---|
83 | screen.xscale = yxscale * screen.yscale; |
---|
84 | } |
---|
85 | |
---|
86 | void |
---|
87 | drawcurve(mark, ax,ay, bx,by, cx,cy, dx,dy) |
---|
88 | int mark, ax,ay, bx,by, cx,cy, dx,dy; |
---|
89 | { |
---|
90 | int i,j,n,c; |
---|
91 | int maxn=(screen.physx + screen.physy)*2; |
---|
92 | |
---|
93 | ax-=screen.minx; bx-=screen.minx; cx-=screen.minx; dx-=screen.minx; |
---|
94 | ay-=screen.miny; by-=screen.miny; cy-=screen.miny; dy-=screen.miny; |
---|
95 | |
---|
96 | for(i=0; i<=maxn; i++) { |
---|
97 | double t, t2, t3, nt, nt2, nt3; |
---|
98 | |
---|
99 | t=(double)i/(double)maxn; t2=t*t; t3=t2*t; |
---|
100 | nt=1-t; nt2=nt*nt; nt3=nt2*nt; |
---|
101 | |
---|
102 | setfdot( |
---|
103 | mark, |
---|
104 | ( ax*t3 + bx*3*t2*nt + cx*3*t*nt2 + dx*nt3 ), |
---|
105 | ( ay*t3 + by*3*t2*nt + cy*3*t*nt2 + dy*nt3 ) |
---|
106 | ); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | /* draw curve and mark direction at the ends */ |
---|
111 | |
---|
112 | void |
---|
113 | drawcurvedir(mark, ax,ay, bx,by, cx,cy, dx,dy) |
---|
114 | int mark, ax,ay, bx,by, cx,cy, dx,dy; |
---|
115 | { |
---|
116 | int i,j,n,c; |
---|
117 | int maxn=(screen.physx + screen.physy)*2; |
---|
118 | double t, t2, t3, nt, nt2, nt3; |
---|
119 | int markb, marke; |
---|
120 | |
---|
121 | ax-=screen.minx; bx-=screen.minx; cx-=screen.minx; dx-=screen.minx; |
---|
122 | ay-=screen.miny; by-=screen.miny; cy-=screen.miny; dy-=screen.miny; |
---|
123 | |
---|
124 | if(bx==ax && by==ay) { |
---|
125 | markb=mark; |
---|
126 | } else if( abs(by-ay) > abs(bx-ax) ) { |
---|
127 | if(by>ay) |
---|
128 | markb='^'; |
---|
129 | else |
---|
130 | markb='v'; |
---|
131 | } else { |
---|
132 | if(bx>ax) |
---|
133 | markb='>'; |
---|
134 | else |
---|
135 | markb='<'; |
---|
136 | } |
---|
137 | |
---|
138 | if(dx==cx && dy==cy) { |
---|
139 | marke=mark; |
---|
140 | } else if( abs(dy-cy) > abs(dx-cx) ) { |
---|
141 | if(dy>cy) |
---|
142 | marke='^'; |
---|
143 | else |
---|
144 | marke='v'; |
---|
145 | } else { |
---|
146 | if(dx>cx) |
---|
147 | marke='>'; |
---|
148 | else |
---|
149 | marke='<'; |
---|
150 | } |
---|
151 | |
---|
152 | for(i=1; i<maxn; i++) { |
---|
153 | t=(double)i/(double)maxn; t2=t*t; t3=t2*t; |
---|
154 | nt=1-t; nt2=nt*nt; nt3=nt2*nt; |
---|
155 | |
---|
156 | setfdot( |
---|
157 | mark, |
---|
158 | ( ax*t3 + bx*3*t2*nt + cx*3*t*nt2 + dx*nt3 ), |
---|
159 | ( ay*t3 + by*3*t2*nt + cy*3*t*nt2 + dy*nt3 ) |
---|
160 | ); |
---|
161 | } |
---|
162 | /* mark the ends */ |
---|
163 | setfdot( markb, (double)ax, (double)ay ); |
---|
164 | setfdot( marke, (double)dx, (double)dy ); |
---|
165 | } |
---|
166 | |
---|
167 | void |
---|
168 | drawdot(mark, x, y) |
---|
169 | int mark; |
---|
170 | int x, y; |
---|
171 | { |
---|
172 | x=(int)((x-screen.minx)*screen.xscale+0.5); |
---|
173 | y=(int)((y-screen.miny)*screen.yscale+0.5); |
---|
174 | |
---|
175 | if(y<0 || y>=screen.physy || x<0 || x>=screen.physx) |
---|
176 | return; |
---|
177 | screendot(x,y)=mark; |
---|
178 | } |
---|
179 | |
---|
180 | void |
---|
181 | setabsdot(mark, x, y) |
---|
182 | int x, y, mark; |
---|
183 | { |
---|
184 | if(y<0 || y>=screen.rows || x<0 || x>=screen.cols-2) |
---|
185 | return; |
---|
186 | screenabsdot(x,y)=mark; |
---|
187 | } |
---|
188 | |
---|
189 | void |
---|
190 | setfdot(mark, fx, fy) |
---|
191 | int mark; |
---|
192 | double fx, fy; |
---|
193 | { |
---|
194 | int x, y; |
---|
195 | |
---|
196 | x=(int)(fx*screen.xscale+0.5); |
---|
197 | y=(int)(fy*screen.yscale+0.5); |
---|
198 | |
---|
199 | if(y<0 || y>=screen.physy || x<0 || x>=screen.physx) |
---|
200 | return; |
---|
201 | screendot(x,y)=mark; |
---|
202 | } |
---|
203 | |
---|
204 | /* destructive */ |
---|
205 | void |
---|
206 | printscreen(f) |
---|
207 | FILE *f; |
---|
208 | { |
---|
209 | int r; |
---|
210 | char *pi, *pc; |
---|
211 | |
---|
212 | for(r=screen.rows-1; r>=0; r--) { |
---|
213 | pc=&screenabsdot(0,r); |
---|
214 | for(pi=&screenabsdot(-2,r+1); pi>=pc && *pi == ' '; pi--) |
---|
215 | {} |
---|
216 | pi[1]='\n'; |
---|
217 | pi[2]=0; |
---|
218 | fputs(pc, f); |
---|
219 | } |
---|
220 | } |
---|