IPCubicImpl.mesa
Last changed by:
Doug Wyatt, April 9, 1983 3:18 pm
DIRECTORY
IPCubic USING [Coeffs],
IPImagerBasic USING [Bezier, Pair];
IPCubicImpl: CEDAR PROGRAM
EXPORTS IPCubic
= BEGIN OPEN IPCubic, IPImagerBasic;
Add: PROC[a: Pair, b: Pair] RETURNS[Pair]
= INLINE { RETURN[[a.x+b.x,a.y+b.y]] };
Sub: PROC[a: Pair, b: Pair] RETURNS[Pair]
= INLINE { RETURN[[a.x-b.x,a.y-b.y]] };
Mul: PROC[a: Pair, s: REAL] RETURNS[Pair]
= INLINE { RETURN[[a.x*s,a.y*s]] };
Div: PROC[a: Pair, s: REAL] RETURNS[Pair]
= INLINE { RETURN[[a.x/s,a.y/s]] };
In: PROC[a: Pair, b: Pair, c: Pair] RETURNS[BOOLEAN]
= INLINE { RETURN[a.x IN[b.x..c.x] AND a.y IN[b.y..c.y]] };
Min: PROC[a: Pair, b: Pair] RETURNS[Pair]
= INLINE { RETURN[[MIN[a.x,b.x],MIN[a.y,b.y]]] };
Max: PROC[a: Pair, b: Pair] RETURNS[Pair]
= INLINE { RETURN[[MAX[a.x,b.x],MAX[a.y,b.y]]] };
Mid: PROC[a: Pair, b: Pair] RETURNS[Pair]
= INLINE { RETURN[[(a.x+b.x)/2,(a.y+b.y)/2]] };
CoeffsToBezier: PUBLIC PROC[coeffs: Coeffs] RETURNS[bezier: Bezier] = {
Compute the bezier control points of the cubic
These points form a convex hull of the curve
b0 is the starting point, b3 is the ending point
b0 ← c0;
b1 ← c0 + c1/3;
b2 ← c0 + 2*c1/3 + c2/3; [ = b1 + (c1+c2)/3 ]
b3 ← c0 + c1 + c2 + c3;
bezier.b0 ← coeffs.c0;
bezier.b1 ← Add[coeffs.c0,Div[coeffs.c1,3]];
bezier.b2 ← Add[bezier.b1,Div[Add[coeffs.c1,coeffs.c2],3]];
bezier.b3 ← Add[Add[Add[coeffs.c0,coeffs.c1],coeffs.c2],coeffs.c3];
RETURN[bezier];
};
BezierToCoeffs: PUBLIC PROC[bezier: Bezier] RETURNS[coeffs: Coeffs] = {
Compute the cubic coefficients from the Bezier points.
c0 ← b0;
c1 ← 3(b1-b0);
c2 ← 3(b0-2b1+b2); [ = 3(b2-b1) - c1 ]
c3 ← -b0 +3(b1-b2) + b3; [ = b3 - (b0 + 3(b2-b1)) ]
t: Pair ← Mul[Sub[bezier.b2,bezier.b1],3];
coeffs.c0 ← bezier.b0;
coeffs.c1 ← Mul[Sub[bezier.b1,bezier.b0],3];
coeffs.c2 ← Sub[t,coeffs.c1];
coeffs.c3 ← Sub[bezier.b3,Add[bezier.b0,t]];
RETURN[coeffs];
};
Split: PUBLIC PROC[bezier: Bezier] RETURNS[Bezier,Bezier] = {
a,b,c,ab,bc,p: Pair;
a ← Mid[bezier.b0,bezier.b1];
b ← Mid[bezier.b1,bezier.b2];
c ← Mid[bezier.b2,bezier.b3];
ab ← Mid[a,b];
bc ← Mid[b,c];
p ← Mid[ab,bc];
RETURN[[bezier.b0, a, ab, p],[p, bc, c, bezier.b3]];
};
Flat: PUBLIC PROC[bezier: Bezier, epsilon: REAL] RETURNS[BOOLEAN] = {
dx,dy: REAL;
d1,d2,d,bl,bh: Pair;
oh: Pair=[0.5,0.5];
bh ← Add[Max[bezier.b0,bezier.b3],oh];
bl ← Sub[Min[bezier.b0,bezier.b3],oh];
IF ~In[bezier.b1,bl,bh] OR ~In[bezier.b2,bl,bh] THEN RETURN[FALSE];
d1 ← Sub[bezier.b1,bezier.b0];
d2 ← Sub[bezier.b2,bezier.b0];
d ← Sub[bezier.b3,bezier.b0];
dx ← ABS[d.x]; dy ← ABS[d.y];
IF dx+dy < 1 THEN RETURN[TRUE];
IF dy < dx THEN {
dydx: REAL ← d.y/d.x;
RETURN[ABS[d2.y-d2.x*dydx]<epsilon AND ABS[d1.y-d1.x*dydx]<epsilon] }
ELSE {
dxdy: REAL ← d.x/d.y;
RETURN[ABS[d2.x-d2.y*dxdy]<epsilon AND ABS[d1.x-d1.y*dxdy]<epsilon] };
};
END.