> restart; > # BEZIER CURVE ALGORITHM 3.6 > # > # To construct the cubic Bezier curves C0, ..., Cn-1 in > # parameter form, where Ci is represented by > # > # (xi(t),yi(t)) = ( a0(i) + a1(i)*t + a2(i)*t^2 + a3(i)*t^3, > # b0(i) + b1(i)*t + b2(i)*t^2 + b3(i)*t^3) > # > # for 0 <= t <= 1 as determined by the left endpoint (x(i),y(i)), > # left guidepoint (x+(i),y+(i)), right endpoint (x(i+1),y(i+1)) and > # right guidepoint (x-(i+1),y-(i+1)) for each i = 0, 1, ... , n-1; > # > # INPUT n, ( (x(i),y(i)), i = 0,...,n ), > # ( (x+(i),y+(i)), i = 0,...,n-1 ), > # ( (x-(i),y-(i)), i = 1,...,n ). > # > # OUTPUT coefficients ( a0(i), a1(i), a2(i), a3(i), > # b0(i), b1(i), b2(i), b3(i), i = 0, ... , n-1 ). > alg036 := proc() local OK, FLAG, N, X, Y, XPL, YPL, I, XMI, YMI, A, NAME, INP, OUP, A0, B0, A1, B1, A2, B2, A3, B3; > printf(`This is the Bezier Curve Algorithm.\n`); > OK := FALSE; > while OK = FALSE do > printf(`Choice of input method:\n`); > printf(`1. Input entry by entry from keyboard\n`); > printf(`2. Input data from a text file\n`); > printf(`Choose 1 or 2 please\n`); > FLAG := scanf(`%d`)[1]; > if FLAG = 1 or FLAG = 2 then > OK := TRUE; > fi; > od; > if FLAG = 1 then > OK := FALSE; > while OK = FALSE do > printf(`Input n\n`); > N := scanf(`%d`)[1]; > if N > 0 then > OK := TRUE; > printf(`Input X[0],Y[0],X+[0],Y+[0]\n`); > printf(`separated by a space\n`); > X[0] := scanf(`%f`)[1]; > Y[0] := scanf(`%f`)[1]; > XPL[0] := scanf(`%f`)[1]; > YPL[0] := scanf(`%f`)[1]; > for I from 1 to N-1 do > printf(`Input X(%d),Y(%d)\n`, I, I); > printf(`separated by space\n`); > X[I] := scanf(`%f`)[1]; > Y[I] := scanf(`%f`)[1]; > printf(`Input X-(%d),Y-(%d)\n`, I, I); > printf(`separated by space\n`); > XMI[I-1] := scanf(`%f`)[1]; > YMI[I-1] := scanf(`%f`)[1]; > printf(`Input X+(%d),Y+(%d)\n`, I, I); > printf(`separated by space\n`); > XPL[I] := scanf(`%f`)[1]; > YPL[I] := scanf(`%f`)[1]; > od; > printf(`Input X[n],Y[n],X-[n],Y-[n]\n`); > printf(`separated by a space\n`); > X[N] := scanf(`%f`)[1]; > Y[N] := scanf(`%f`)[1]; > XMI[N-1] := scanf(`%f`)[1]; > YMI[N-1] := scanf(`%f`)[1]; > else > printf(`Number must be a positive integer\n`); > fi; > od; > fi; > if FLAG = 2 then > printf(`Has a text file been created with the data as follows ?\n\n`); > printf(`X[0] Y[0] X+[0] Y+[0]\n`); > printf(`X[1] Y[1] X-[1] Y-[1] X+[1] Y+[1]\n`); > printf(`...\n`); > printf(`X[n-1] Y[n-1] X-[n-1] Y-[n-1] X+[n-1] Y+[n-1]\n`); > printf(`X[n] Y[n] X-[n] Y-[n]\n\n`); > printf(`Enter Y or N\n`); > A := scanf(`\n%c`)[1]; > if A = "Y" or A = "y" then > printf(`Input the file name in the form - `); > printf(`drive:\\name.ext\n`); > printf(`For example: A:\\DATA.DTA\n`); > NAME := scanf(`%s`)[1]; > INP := fopen(NAME,READ,TEXT); > OK := FALSE; > while OK = FALSE do > printf(`Input n\n`); > N := scanf(`\n%d`)[1]; > if N > 0 then > OK := TRUE; > X[0] := fscanf(INP, `%f`)[1]; > Y[0] := fscanf(INP, `%f`)[1]; > XPL[0] := fscanf(INP, `%f`)[1]; > YPL[0] := fscanf(INP, `%f`)[1]; > for I from 1 to N-1 do > X[I] := fscanf(INP, `%f`)[1]; > Y[I] := fscanf(INP, `%f`)[1]; > XMI[I-1] := fscanf(INP, `%f`)[1]; > YMI[I-1] := fscanf(INP, `%f`)[1]; > XPL[I] := fscanf(INP, `%f`)[1]; > YPL[I] := fscanf(INP, `%f`)[1]; > od; > X[N] := fscanf(INP, `%f`)[1]; > Y[N] := fscanf(INP, `%f`)[1]; > XMI[N-1] := fscanf(INP, `%f`)[1]; > YMI[N-1] := fscanf(INP, `%f`)[1]; > fclose(INP); > else > printf(`Number must be a positive integer\n`); > fi; > od; > else > printf(`Please create the input file as indicated.\n`); > printf(`The program will end so the input file can `); > printf(`be created.\n`); > OK := FALSE; > fi; > fi; > if OK = TRUE then > printf(`Select output destination\n`); > printf(`1. Screen\n`); > printf(`2. Text file\n`); > printf(`Enter 1 or 2\n`); > FLAG := scanf(`%d`)[1]; > if FLAG = 2 then > printf(`Input the file name in the form - drive:\\name.ext\n`); > printf(`For example: A:\\OUTPUT.DTA\n`); > NAME := scanf(`%s`)[1]; > OUP := fopen(NAME,WRITE,TEXT); > else > OUP := default; > fi; > fprintf(OUP, `BEZIER CURVE ALGORITHM\n\n`); > fprintf(OUP, ` A0 A1 A2 A3`); > fprintf(OUP,` on the first line\n`); > fprintf(OUP, ` B0 B1 B2 B3`); > fprintf(OUP,` on the second line\n`); > # Step 1 > for I from 0 to N-1 do > # Step 2 > A0[I] := X[I]; > B0[I] := Y[I]; > A1[I] := 3*(XPL[I] - X[I]); > B1[I] := 3*(YPL[I] - Y[I]); > A2[I] := 3*(X[I]+XMI[I]-2*XPL[I]); > B2[I] := 3*(Y[I]+YMI[I]-2*YPL[I]); > A3[I] := X[I+1]-X[I]+3*XPL[I]-3*XMI[I]; > B3[I] := Y[I+1]-Y[I]+3*YPL[I]-3*YMI[I]; > # Step 3 > fprintf(OUP,` %11.6f %11.6f %11.6f %11.6f\n`, A0[I], A1[I], A2[I], A3[I]); > fprintf(OUP,` %11.6f %11.6f %11.6f %11.6f\n`, B0[I], B1[I], B2[I], B3[I]); > fprintf(OUP, `\n`); > od; > if OUP <> default then > fclose(OUP); > printf(`Output file %s created successfully`,NAME); > fi; > fi; > # Step 4 > RETURN(0); > end; > alg036();