Debian Codegraph @

texmf-dist/doc/metapost/featpost/example/splineperspective.mp

raw
1 % splineperspective.mp
2 % L. Nobre G. and Troy Henderson
3 % 2007 -- 2012
4
5 prologues := 1;
6 defaultfont := "cmss17";
7 color f, viewcentr;
8 boolean ParallelProj;
9
10 f := (3,5,4); % This f is the point of view in 3D
11
12 viewcentr := black; % This is the aim of the view
13
14 ParallelProj := false; % Kind of perspective %
15
16 def X(expr A) =
17 if color A: redpart A else: cyanpart A fi
18 enddef;
19
20 def Y(expr A) =
21 if color A: greenpart A else: magentapart A fi
22 enddef;
23
24 def Z(expr A) =
25 if color A: bluepart A else: yellowpart A fi
26 enddef;
27
28 def conorm(expr A) =
29 ( X(A) ++ Y(A) ++ Z(A) )
30 enddef;
31
32 def N(expr A) =
33 begingroup
34 save M, exitcolor;
35 numeric M;
36 color exitcolor;
37 M = conorm( A );
38 if M > 0:
39 exitcolor = ( X(A)/M, Y(A)/M, Z(A)/M );
40 else:
41 exitcolor := black;
42 fi;
43 ( exitcolor )
44 endgroup
45 enddef;
46
47 def cdotprod(expr A, B) =
48 ( X(A)*X(B) + Y(A)*Y(B) + Z(A)*Z(B) )
49 enddef;
50
51 def ccrossprod(expr A, B) =
52 ( Y(A)*Z(B) - Z(A)*Y(B),
53 Z(A)*X(B) - X(A)*Z(B),
54 X(A)*Y(B) - Y(A)*X(B) )
55 enddef;
56
57 % The dotproduct of two normalized vectors is the cosine of the angle
58 % they form.
59
60 def ndotprod(expr A, B) =
61 begingroup
62 save a, b;
63 color a, b;
64 a = N(A);
65 b = N(B);
66 ( ( X(a)*X(b) + Y(a)*Y(b) + Z(a)*Z(b) ) )
67 endgroup
68 enddef;
69
70 % The normalized crossproduct of two vectors.
71 % Also check getangle below.
72
73 def ncrossprod(expr A, B) =
74 N( ccrossprod( A, B ) )
75 enddef;
76
77 % Haahaa! Trigonometry.
78
79 def getangle(expr A, B) =
80 begingroup
81 save coss, sine;
82 numeric coss, sine;
83 coss := cdotprod( A, B );
84 sine := conorm( ccrossprod( A, B ) );
85 ( angle( coss, sine ) )
86 endgroup
87 enddef;
88
89 def rp(expr R) =
90 begingroup
91
92 save v, u;
93 save verti, horiz, eta, squarf, radio;
94 color v, u;
95 numeric verti, horiz, eta, squarf, radio;
96
97 v = N( (-Y(f-viewcentr), X(f-viewcentr), 0) );
98 u = ncrossprod( f-viewcentr, v );
99
100 horiz = cdotprod( R-viewcentr, v );
101 verti = cdotprod( R-viewcentr, u );
102
103 if ParallelProj:
104 eta = 1;
105 else:
106 squarf = cdotprod( f-viewcentr, f-viewcentr );
107 radio = cdotprod( R-viewcentr, f-viewcentr );
108 eta = 1 - radio/squarf;
109 fi;
110 ( 150*(horiz,verti)/eta )
111
112 endgroup
113 enddef;
114
115 def cartaxes(expr axex, axey, axez) =
116 begingroup
117 save orig, axxc, ayyc, azzc;
118 color orig, axxc, ayyc, azzc;
119 orig = (0,0,0);
120 axxc = (axex,0,0);
121 ayyc = (0,axey,0);
122 azzc = (0,0,axez);
123 drawarrow rp(orig)..rp(axxc);
124 drawarrow rp(orig)..rp(ayyc);
125 drawarrow rp(orig)..rp(azzc);
126 label.bot( "x" ,rp(axxc)); %%%%%%%%%%%%%%%%%%%%%%%%%
127 label.bot( "y" ,rp(ayyc)); %% Some Labels... %%
128 label.lft( "z" ,rp(azzc)); %%%%%%%%%%%%%%%%%%%%%%%%%
129 endgroup
130 enddef;
131
132 def line( expr Ang ) =
133 begingroup
134 numeric a, b, c;
135 a = (2-(1 ++ cosd(Ang))*cosd(3*Ang))*cosd(Ang);
136 b = (2-(1 ++ cosd(Ang))*cosd(3*Ang))*sind(Ang);
137 c =1.5+(1 ++ cosd(Ang))*sind(3*Ang);
138 ( (a,b,c) )
139 endgroup
140 enddef;
141
142 % Evaluate a cubic polynomial of the "standard" Bezier form at t
143 vardef evalbezier(expr p,t) =
144 save _a,_b,_c,_d;
145 numeric _a,_b,_c,_d;
146 _a:=(1-t)**3;
147 _b:=3*((1-t)**2)*t;
148 _c:=3*(1-t)*(t**2);
149 _d:=t**3;
150 (point 0 of p)*_a + (postcontrol 0 of p)*_b + (precontrol 1 of p)*_c +
151 (point 1 of p)*_d
152 enddef;
153
154 % Evaluate the derivative of a cubic polynomial of the "standard"
155 % Bezier form at t
156 vardef evalbezierderivative(expr p,t) =
157 save _a,_b,_c;
158 pair _a,_b,_c;
159 _a:=3*((point 1 of p) - 3*(precontrol 1 of p) + 3*(postcontrol 0 of p)
160 -(point 0 of p));
161 _b:=6*((precontrol 1 of p) - 2*(postcontrol 0 of p) + (point 0 of p));
162 _c:=3*((postcontrol 0 of p) - (point 0 of p));
163 _a*(t**2) + _b*t + _c
164 enddef;
165
166 % Evaluate a rational function of the "standard" cubic NURBS form at t
167 vardef evalnurbs(expr p,w,t) =
168 save _q,_r;
169 path _q,_r;
170 _q:=((cyanpart w)*(point 0 of p))..
171 controls ((magentapart w)*(postcontrol 0 of p))
172 and ((yellowpart w)*(precontrol 1 of p)) .. ((blackpart w)*(point 1 of p));
173 _r:=(cyanpart w,0) ..
174 controls (magentapart w,0) and (yellowpart w,0) .. (blackpart w,0);
175 evalbezier(_q,t)/(xpart evalbezier(_r,t))
176 enddef;
177
178 % Evaluate the derivative of a rational function of the "standard"
179 % cubic NURBS form at t
180 vardef evalnurbsderivative(expr p,w,t) =
181 save _a,_b,_c,_d,_q,_r;
182 pair _a,_b;
183 numeric _c,_d;
184 path _q,_r;
185 _q:=((cyanpart w)*(point 0 of p)) ..
186 controls ((magentapart w)*(postcontrol 0 of p))
187 and ((yellowpart w)*(precontrol 1 of p)) .. ((blackpart w)*(point 1 of p));
188 _r:=(cyanpart w,0) ..
189 controls (magentapart w,0) and (yellowpart w,0) .. (blackpart w,0);
190 _a:=evalbezier(_q,t);
191 _b:=evalbezierderivative(_q,t);
192 _c:=xpart evalbezier(_r,t);
193 _d:=xpart evalbezierderivative(_r,t);
194 (_b*_c-_a*_d)/(_c**2)
195 enddef;
196
197 % Fit a cubic polynomial of the "standard" Bezier form to a
198 % rational function of the
199 % "standard" cubic NURBS form with function and derivative agreement
200 % at tmin and tmax
201 vardef nurbstobezier(expr p,w,tmin,tmax) =
202 save _a,_b,_c,_d,_e;
203 pair _a,_b,_c,_d;
204 numeric _e;
205 _e:=(tmax-tmin)/3;
206 _a:=evalnurbs(p,w,tmin);
207 _b:=_a + _e*evalnurbsderivative(p,w,tmin);
208 _d:=evalnurbs(p,w,tmax);
209 _c:=_d - _e*evalnurbsderivative(p,w,tmax);
210 _a .. controls _b and _c .. _d
211 enddef;
212
213 % Reparameterize a cubic polynomial of the "standard" Bezier form by mapping
214 % the interval [tmin,tmax] to [0,1]
215 vardef beziertobezier(expr p,tmin,tmax) =
216 nurbstobezier(p,(1,1,1,1),tmin,tmax)
217 enddef;
218
219 % Evalute the L^2[0,1] norm of a cubic polynomial of the "standard"
220 % Bezier form
221 vardef beziernorm(expr p) =
222 save _a,_b,_c,_d,_i,_xabs,_yabs,_A,_B,_C,_D,_I;
223 numeric _a,_b,_c,_d,_i,_xabs,_yabs,_A,_B,_C,_D,_I;
224 _xabs:=max(
225 abs(xpart point 0 of p),
226 abs(xpart postcontrol 0 of p),
227 abs(xpart precontrol 1 of p),
228 abs(xpart point 1 of p));
229 _yabs:=max(
230 abs(ypart point 0 of p),
231 abs(ypart postcontrol 0 of p),
232 abs(ypart precontrol 1 of p),
233 abs(ypart point 1 of p));
234 if (_xabs > 0):
235 _a:=xpart((point 1 of p) - 3*(precontrol 1 of p)
236 + 3*(postcontrol 0 of p) - (point 0 of p))/_xabs;
237 _b:=3*xpart((precontrol 1 of p) - 2*(postcontrol 0 of p)
238 + (point 0 of p))/_xabs;
239 _c:=3*xpart((postcontrol 0 of p) - (point 0 of p))/_xabs;
240 _d:=xpart(point 0 of p)/_xabs;
241 _i:=(_a**2)/7 + ((_b)**2 + 2*_a*_c)/5 + (_a*_b + 2*_b*_d + (_c**2))/3 + (_a*_d + _b*_c)/2 + (_c*_d + (_d**2));
242 else:
243 _i:=0;
244 fi;
245 if (_yabs > 0):
246 _A:=ypart((point 1 of p) - 3*(precontrol 1 of p)
247 + 3*(postcontrol 0 of p) - (point 0 of p))/_yabs;
248 _B:=3*ypart((precontrol 1 of p) - 2*(postcontrol 0 of p)
249 + (point 0 of p))/_yabs;
250 _C:=3*ypart((postcontrol 0 of p) - (point 0 of p))/_yabs;
251 _D:=ypart(point 0 of p)/_yabs;
252 _I:=(_A**2)/7 + ((_B)**2 + 2*_A*_C)/5
253 + (_A*_B + 2*_B*_D + (_C**2))/3 + (_A*_D + _B*_C)/2 + (_C*_D + (_D**2));
254 else:
255 _I:=0;
256 fi;
257 (_xabs*sqrt(_i)) ++ (_yabs*sqrt(_I))
258 enddef;
259
260 % Fit a cubic Bezier spline to a rational function of the "standard"
261 % cubic NURBS form by iteratively refining the Bezier curve.
262 % p is a 4 point path containing the 4 cubic NURBS (2D) control points
263 % w is a cmykcolor containing the 4 cubic NURBS weights
264 % EPS is the tolerance to stop refining each branch of the Bezier spline
265 vardef fitnurbswithbezier(expr p,w,EPS) =
266 save _a,_b,_c,_e,_error,_k,_q;
267 numeric _a,_b,_c,_error,_k;
268 path _q,_q[],_e;
269 _a:=0;
270 _b:=1;
271 _k:=1/sqrt(2);
272 _q:=(point 0 of p);
273 _q[4]:=nurbstobezier(p,w,_a,_b);
274 forever:
275 exitunless(_a<1);
276 _q[1]:=_q[4];
277 _c:=_b-_k*((_b-_a)**2);
278 _q[2]:=beziertobezier(_q[1],_a,_c);
279 _q[3]:=nurbstobezier(p,w,_a,_c);
280 _q[4]:=_q[3];
281 _e:=((point 0 of _q[2])-(point 0 of _q[3])) ..
282 controls ((postcontrol 0 of _q[2])-(postcontrol 0 of _q[3]))
283 and ((precontrol 1 of _q[2])-(precontrol 1 of _q[3])) ..
284 ((point 1 of _q[2])-(point 1 of _q[3]));
285 _error:=beziernorm(_e)/beziernorm(_q[3]);
286 % show _error;
287 if (_error > EPS):
288 _b:=_c;
289 else:
290 _q[2]:=beziertobezier(_q[1],_c,_b);
291 _q[3]:=nurbstobezier(p,w,_c,_b);
292 _e:=((point 0 of _q[2])-(point 0 of _q[3])) ..
293 controls ((postcontrol 0 of _q[2])-(postcontrol 0 of _q[3]))
294 and ((precontrol 1 of _q[2])-(precontrol 1 of _q[3])) ..
295 ((point 1 of _q[2])-(point 1 of _q[3]));
296 _error:=beziernorm(_e)/beziernorm(_q[3]);
297 if (_error > EPS):
298 _q:=_q .. controls (postcontrol 0 of _q[4])
299 and (precontrol 1 of _q[4]) .. (point 1 of _q[4]);
300 _a:=_c;
301 _q[4]:=_q[3];
302 else:
303 _q:=_q .. controls (postcontrol 0 of _q[1])
304 and (precontrol 1 of _q[1]) .. (point 1 of _q[1]);
305 _a:=_b;
306 _q[4]:=nurbstobezier(p,w,_a,1);
307 fi;
308 _b:=1;
309 fi;
310 endfor;
311 _q
312 enddef;
313
314 % This macro is used to provide a path to draw the NURBS
315 % It returns a path of length N passing through N+1 equally spaced
316 % (in time) points along the NURBS connected by line segments
317 vardef samplednurbs(expr p,w,N) =
318 save _a,_b,_c,_d,_n,_t,_q;
319 numeric _a,_b,_c,_d,_n,_t;
320 path _q;
321 _q:=(point 0 of p);
322 for _n=1 upto N:
323 _t:=_n/N;
324 _a:=(cyanpart w)*((1-_t)**3);
325 _b:=3*(magentapart w)*((1-_t)**2)*_t;
326 _c:=3*(yellowpart w)*(1-_t)*(_t**2);
327 _d:=(blackpart w)*(_t**3);
328 _q:=_q .. ((_a*(point 0 of p)+_b*(postcontrol 0 of p)
329 +_c*(precontrol 1 of p)+_d*(point 1 of p))/(_a+_b+_c+_d));
330 endfor;
331 ( _q )
332 enddef;
333
334 % The code below is a development by Troy from an original by Przemek Koprowski
335
336 vardef rationalbezier(expr A,B,C,D) =
337 begingroup
338 save P,Q,E,a,b,c,d,r,EPS;
339 color P[],Q[],E;
340 pair a,b,c,d;
341 EPS:=1/10;
342 a:=(redpart A,greenpart A)/(bluepart A);
343 b:=(redpart B,greenpart B)/(bluepart B);
344 c:=(redpart C,greenpart C)/(bluepart C);
345 d:=(redpart D,greenpart D)/(bluepart D);
346 r:=max(abs(a-b),abs(a-c),abs(a-d),abs(b-c),abs(b-d),abs(c-d));
347 if (max(abs(b-1/2[a,c]),abs(c-1/2[b,d])) > EPS*r):
348 P[0]:=A;
349 P[1]:=1/2[A,B];
350 E:=1/2[B,C];
351 Q[2]:=1/2[C,D];
352 Q[3]:=D;
353 P[2]:=1/2[P[1],E];
354 Q[1]:=1/2[E,Q[2]];
355 P[3]:=1/2[P[2],Q[1]];
356 Q[0]:=P[3];
357 rationalbezier(P[0],P[1],P[2],P[3]) & rationalbezier(Q[0],Q[1],Q[2],Q[3])
358 else:
359 a .. controls b and c .. d
360 fi
361 endgroup
362 enddef;
363
364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365 % Here's where the fun begins %
366 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367
368 def casteljau( expr Za, Zb, Zc, Zd, Pt ) = %%%%%%%%%%%%%%%%%%% 2D or 3D
369 begingroup
370 save A, B, C, D;
371 numeric A, B, C, D;
372 A = (1-Pt)**3;
373 B = 3*((1-Pt)**2)*Pt;
374 C = 3*(1-Pt)*(Pt**2);
375 D = Pt**3;
376 ( (A*Za+B*Zb+C*Zc+D*Zd) )
377 endgroup
378 enddef;
379
380 def twothr( expr Z ) = ( xpart Z, ypart Z, 0 ) enddef;
381
382 def twotwo( expr Z ) = rp( twothr( Z ) ) enddef;
383
384 def xoy( expr Z ) = rp( ( X(Z), Y(Z), 0 ) ) enddef;
385
386 def yoz( expr W ) = rp( ( 0, Y(W), Z(W) ) ) enddef;
387
388 def xoz( expr W ) = rp( ( X(W), 0, Z(W) ) ) enddef;
389
390 def nextthirty( expr Za, Zb, Zc, Zd, Pt ) = %%% input 3D and return 2D
391 begingroup
392 save A, B, C, D, Tot, P;
393 numeric A, B, C, D, Tot;
394 color P;
395 P = N( f - viewcentr );
396 A = ((1-Pt)**3)*cdotprod( P, f-Za );
397 B = 3*((1-Pt)**2)*Pt*cdotprod( P, f-Zb );
398 C = 3*(1-Pt)*(Pt**2)*cdotprod( P, f-Zc );
399 D = (Pt**3)*cdotprod( P, f-Zd );
400 Tot = A+B+C+D;
401 ( (A*rp(Za)+B*rp(Zb)+C*rp(Zc)+D*rp(Zd))/Tot )
402 endgroup
403 enddef;
404
405 vardef nurbstobezierold (expr p,w) =
406 save _a,_b,_c,_d,_j,_n,_r,_s,_t,_A,_B,_Aold,_Bold,_C,_D,_EPS,_J,_N;
407 _EPS:=0.00001;
408 _J:=10;
409
410 _Aold:=0;
411 _Bold:=0;
412 _A:=1;
413 _B:=1;
414 _s:=((_A-_Aold)++(_B-_Bold))/(_A++_B);
415 _j:=1; _r:=0;
416 forever:
417 exitunless((_s>_EPS) and (_j<_J));
418 _j:=_j+1;
419 _N:=2**_j;
420 _Aold:=_A;
421 _Bold:=_B;
422 _D:=_N+1/_N-21/_N/_N/_N-1/_N/_N/_N/_N/_N+20/_N/_N/_N/_N/_N/_N/_N;
423 _C:=120*(2+2/_N/_N-5/_N/_N/_N/_N)/_D;
424 _D:=60*(3+3/_N/_N+10/_N/_N/_N/_N)/_D;
425 _c:=5/_N/_N/_N/_N;
426 _a:=2+2/_N/_N-_c;
427 _b:=2-3/_N/_N+_c;
428 _c:=1+6/_N/_N+_c;
429 _A:=(-2*(cyanpart p)*_a+(blackpart p)*_b)/_c;
430 _B:=((cyanpart p)*_b-2*(blackpart p)*_a)/_c;
431 for _n=0 upto _N:
432 _t:=_n/_N;
433 _a:=(1-_t)**3;
434 _b:=((1-_t)**2)*_t;
435 _c:=(1-_t)*(_t**2);
436 _d:=_t**3;
437 _r:=((cyanpart w)*(cyanpart p)*_a + 3*(magentapart
438 w)*(magentapart p)*_b + 3*(yellowpart w)*(yellowpart p)*_c +
439 (blackpart w)*(blackpart p)*_d)/((cyanpart w)*_a + 3*(magentapart
440 w)*_b + 3*(yellowpart w)*_c + (blackpart w)*_d);
441 _A:=_A+(_C*_b-_D*_c)*_r;
442 _B:=_B+(_C*_c-_D*_b)*_r;
443 endfor;
444 _s:=((_A-_Aold)++(_B-_Bold))/(_A++_B);
445 endfor;
446 (_A,_B)/3
447 enddef;
448
449 def nurbsapprox( expr Pa, Pb, Pc, Pd ) =
450 begingroup
451 color Pn;
452 numeric wa, wb, wc, wd;
453 path returnpath;
454 pair xpair, ypair, ba, bb, bc, bd;
455 cmykcolor xcontrols, ycontrols;
456 Pn = N( f - viewcentr );
457 wa = cdotprod( Pn, f-Pa );
458 wb = cdotprod( Pn, f-Pb );
459 wc = cdotprod( Pn, f-Pc );
460 wd = cdotprod( Pn, f-Pd );
461 xcontrols = (xpart rp(Pa),xpart rp(Pb),xpart rp(Pc),xpart rp(Pd));
462 ycontrols = (ypart rp(Pa),ypart rp(Pb),ypart rp(Pc),ypart rp(Pd));
463 xpair = nurbstobezierold( xcontrols, (wa,wb,wc,wd) );
464 ypair = nurbstobezierold( ycontrols, (wa,wb,wc,wd) );
465 ba = rp( Pa );
466 bb = (xpart xpair, xpart ypair);
467 bc = (ypart xpair, ypart ypair);
468 bd = rp( Pd );
469 %show ba;
470 %show bb;
471 %show bc;
472 %show bd;
473 %show wa;
474 %show wb;
475 %show wc;
476 %show wd;
477 returnpath = ba..controls bb and bc..bd;
478 (returnpath)
479 endgroup
480 enddef;
481
482 def fitthreednurbswithtwodbezier( expr pa, pb, pc, pd, EPS ) =
483 begingroup
484 save _a,_b,_c,_e,_error,_k,_q,w,wa,wb,wc,wd,pn,za, zb, zc, zd;
485 numeric _a,_b,_c,_error,_k,wa,wb,wc,wd;
486 path p,_q,_q[],_e;
487 color pn;
488 pair za, zb, zc, zd;
489 cmykcolor w;
490 za = rp(pa); show za;
491 zb = rp(pb); show zb;
492 zc = rp(pc); show zc;
493 zd = rp(pd); show zd;
494 p = za .. controls zb and zc .. zd;
495 pn = N( f - viewcentr );
496 wa = cdotprod( pn, f-pa ); show wa;
497 wb = cdotprod( pn, f-pb ); show wb;
498 wc = cdotprod( pn, f-pc ); show wc;
499 wd = cdotprod( pn, f-pd ); show wd;
500 w = ( wa, wb, wc, wd );
501 _a:=0;
502 _b:=1;
503 _k:=1/sqrt(2);
504 _q:=(point 0 of p);
505 _q[4]:=nurbstobezier(p,w,_a,_b);
506 forever:
507 exitunless(_a<1);
508 _q[1]:=_q[4];
509 _c:=_b-_k*((_b-_a)**2);
510 _q[2]:=beziertobezier(_q[1],_a,_c);
511 _q[3]:=nurbstobezier(p,w,_a,_c);
512 _q[4]:=_q[3];
513 _e:=((point 0 of _q[2])-(point 0 of _q[3])) ..
514 controls ((postcontrol 0 of _q[2])-(postcontrol 0 of _q[3]))
515 and ((precontrol 1 of _q[2])-(precontrol 1 of _q[3])) ..
516 ((point 1 of _q[2])-(point 1 of _q[3]));
517 _error:=beziernorm(_e)/beziernorm(_q[3]);
518 if (_error > EPS):
519 _b:=_c;
520 else:
521 _q[2]:=beziertobezier(_q[1],_c,_b);
522 _q[3]:=nurbstobezier(p,w,_c,_b);
523 _e:=((point 0 of _q[2])-(point 0 of _q[3])) ..
524 controls ((postcontrol 0 of _q[2])-(postcontrol 0 of _q[3]))
525 and ((precontrol 1 of _q[2])-(precontrol 1 of _q[3])) ..
526 ((point 1 of _q[2])-(point 1 of _q[3]));
527 _error:=beziernorm(_e)/beziernorm(_q[3]);
528 if (_error > EPS):
529 _q:=_q .. controls (postcontrol 0 of _q[4])
530 and (precontrol 1 of _q[4]) .. (point 1 of _q[4]);
531 _a:=_c;
532 _q[4]:=_q[3];
533 else:
534 _q:=_q .. controls (postcontrol 0 of _q[1])
535 and (precontrol 1 of _q[1]) .. (point 1 of _q[1]);
536 _a:=_b;
537 _q[4]:=nurbstobezier(p,w,_a,1);
538 fi;
539 _b:=1;
540 fi;
541 endfor;
542 ( _q )
543 endgroup
544 enddef;
545
546 beginfig(0);
547 draw for i=1 step 6 until 360: xoy(line(i)).. endfor cycle;
548 draw for i=1 step 6 until 360: rp(line(i)).. endfor cycle;
549 for i=1 step 15 until 360:
550 draw rp(line(i)) withpen pencircle scaled 2mm;
551 endfor;
552 endfig;
553
554 beginfig(4);
555 % p contains the 4 control points of the rational function of the
556 % "standard" cubic NURBS form
557 path p;
558 p:=(297.63725,297.63725) .. controls (132.98871,286.67885) and (180.62535,152.16249) .. (429.54399,226.31157);
559
560 % w contains the 4 weights for the rational function of the
561 % "standard" cubic NURBS form
562 cmykcolor w;
563 w:=(2.15756,1.6709,0.8598,1.34647);
564
565 % EPS represents the minimum "acceptable error" to stop refining any
566 % given branch of the Bezier
567 Err:=0.067;
568
569 % q represents the Bezier spline fit to the rational function of the
570 % "standard" cubic NURBS form
571 path q;
572 q:=fitnurbswithbezier(p,w,Err);
573 % q:=fitnurbswithbezier(reverse p,(blackpart w,yellowpart w,magentapart w,cyanpart w),Err);
574
575 % draw the NURBS by sampling it at many points and connecting the
576 % samples via line segments
577 draw samplednurbs(p,w,20) withcolor red withpen pencircle scaled 2bp;
578
579 % draw the Bezier spline and its knots
580 draw q;
581 for n=0 upto length q:
582 draw fullcircle scaled 2 shifted point n of q withcolor blue;
583 endfor;
584 endfig;
585
586 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
587
588 f := 0.35*(3,5,2);
589 beginfig(1);
590 numeric tu, num, i, fac;
591 pen pencontrol, penspline, penalytic;
592 color colcontrol, colspline, colorytic, colormark;
593 color w[];
594 pencontrol = pencircle scaled 4pt;
595 penspline = pencircle scaled 2pt;
596 penalytic = pencircle scaled 1pt;
597 colcontrol = black;
598 colspline = red;
599 colorytic = blue+green;
600 colormark = (0.8,0.8,0.1);
601 tu = 6cm;
602 num = 50;
603 fac = 1.2;
604 transform T;
605 T = identity scaled tu;
606 z21 = origin;
607 z22 = (1,0);
608 z23 = (1,1);
609 z24 = (0,1);
610 z1 = z21 transformed T;
611 z2 = z22 transformed T;
612 z3 = z23 transformed T;
613 z4 = z24 transformed T;
614 z6 = (fac,0) transformed T;
615 z8 = (0,fac) transformed T;
616 drawarrow z1--z6;
617 drawarrow z1--z8;
618 label.lrt( "x", z6 );
619 label.ulft( "y", z8 );
620 dotlabels.urt(1,2,3,4);
621 z11 = twotwo( z21 );
622 z12 = twotwo( z22 );
623 z13 = twotwo( z23 );
624 z14 = twotwo( z24 );
625 w1 = twothr( z21 );
626 w2 = twothr( z22 );
627 w3 = twothr( z23 );
628 w4 = twothr( z24 );
629 cartaxes( fac, fac, 0.3*fac );
630 draw z12--z13--z14 dashed evenly;
631 draw z2--z3--z4 dashed evenly;
632 % 1) Next line: MetaPost intrinsic path.
633 draw z1..controls z2 and z3..z4 withpen penspline withcolor colspline;
634 % 2) Next line: my implementation of the MetaPost intrinsic path.
635 draw z1 for i=1 upto num: ..casteljau(z1,z2,z3,z4,i/num) endfor
636 withpen penalytic withcolor colorytic;
637 % 5) Next line: hopefully, how it should be done. Yeah! Way to go!
638 draw z11 for i=1 upto num: ..nextthirty(w1,w2,w3,w4,i/num) endfor
639 withpen pencontrol withcolor colcontrol;
640 % 4) Next line: MetaPost intrinsic path of perspectived control points.
641 draw z11..controls z12 and z13..z14
642 withpen penspline withcolor colspline;
643 % 3) Next line: what should be drawn in perspective.
644 draw z11 for i=1 upto num: ..rp(casteljau(w1,w2,w3,w4,i/num)) endfor
645 withpen penalytic withcolor colorytic;
646 % 6) Next line: Troy's approximation
647 draw nurbsapprox(w1,w2,w3,w4) withcolor colormark;
648
649
650 endfig;
651
652 f := 1.05*(3,5,2);
653
654 beginfig(2);
655 color w[];
656 w1 = (1,0,0);
657 w2 = (0,0,1);
658 w3 = (0,1,0);
659 w4 = (1,1,1);
660 w5 = (1,1,0);
661 w6 = (1,0,1);
662 w7 = (0,1,1);
663 cartaxes( fac, fac, fac );
664 draw rp(w1)--rp(w2)--rp(w3)--rp(w4)--rp(w5)--rp(w1)--rp(w6)--
665 rp(w2)--rp(w7)--rp(w3)--rp(w5) dashed withdots;
666 draw rp(w6)--rp(w4)--rp(w7) dashed withdots;
667 draw xoy(w1) for i=1 upto num:
668 ..xoy(casteljau(w1,w3,black,w5,i/num))
669 endfor withcolor colormark;
670 draw xoy(w1) for i=1 upto num: ..xoy(casteljau(w1,w2,w3,w4,i/num)) endfor;
671 draw xoz(w1) for i=1 upto num: ..xoz(casteljau(w1,w2,w3,w4,i/num)) endfor;
672 draw rp(w1) for i=1 upto num: ..nextthirty(w1,w2,w3,w4,i/num) endfor
673 withpen pencontrol withcolor colcontrol;
674 draw rp(w1) for i=1 upto num: ..rp(casteljau(w1,w2,w3,w4,i/num)) endfor
675 withpen penalytic withcolor colorytic;
676 draw nextthirty(w1,w2,w3,w4,0.5) withpen pencontrol withcolor red;
677 endfig;
678
679 beginfig(5);
680 color w[];
681 for i=1 upto 4:
682 w[i]=(uniformdeviate(1),uniformdeviate(1),uniformdeviate(1));
683 draw rp(w[i]) withpen pencontrol;
684 draw xoy(w[i]) withpen penspline;
685 draw xoz(w[i]) withpen penspline;
686 draw yoz(w[i]) withpen penspline;
687 endfor;
688 cartaxes( fac, fac, fac );
689 draw rp(w1)--rp(w2)--rp(w3)--rp(w4) withpen penspline dashed evenly;
690 draw xoy(w1)--xoy(w2)--xoy(w3)--xoy(w4) withpen penalytic dashed evenly;
691 draw xoz(w1)--xoz(w2)--xoz(w3)--xoz(w4) withpen penalytic dashed evenly;
692 draw yoz(w1)--yoz(w2)--yoz(w3)--yoz(w4) withpen penalytic dashed evenly;
693 draw xoy(w1) for i=1 upto num: ..xoy(casteljau(w1,w2,w3,w4,i/num)) endfor;
694 draw xoz(w1) for i=1 upto num: ..xoz(casteljau(w1,w2,w3,w4,i/num)) endfor;
695 draw yoz(w1) for i=1 upto num: ..yoz(casteljau(w1,w2,w3,w4,i/num)) endfor;
696 draw rp(w1) for i=1 upto num: ..nextthirty(w1,w2,w3,w4,i/num) endfor
697 withpen pencontrol withcolor colcontrol;
698 %%%%%%%%%%%%% the following line may crash;
699 %% draw fitthreednurbswithtwodbezier(w1,w2,w3,w4,0.2)
700 %% withpen penalytic withcolor red;
701 endfig;
702
703 beginfig(3);
704 path xyp, xzp;
705 xyp = origin..tension 2 and 0.75..right...(right+up+right+up);
706 xzp = origin..tension 2 and 0.75..(right+up)...(right+up+right);
707 z1 = postcontrol 0 of xyp;
708 z2 = postcontrol 0 of xzp;
709 z3 = precontrol 1 of xyp;
710 z4 = precontrol 1 of xzp;
711 % show (xpart z1);
712 % show (xpart z2);
713 % show (xpart z3);
714 % show (xpart z4);
715 draw xyp;
716 draw xzp;
717 endfig;
718
719 end.