Cubic2Impl.mesa
Copyright © 1982, 1985 by Xerox Corporation. All rights reserved.
Last changed by Doug Wyatt, August 23, 1982 4:12 pm
Eric Nickell, June 27, 1985 10:16:03 pm PDT
Stone, June 29, 1985 10:40:13 am PDT
DIRECTORY
Cubic2,
Vector2 USING [Add, Sub, Mul, Div],
Imager USING [VEC];
Cubic2Impl:
CEDAR
PROGRAM
IMPORTS Vector2
EXPORTS Cubic2 = {
OPEN Vector2, Cubic2;
VEC: TYPE = Imager.VEC;
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: VEC ← 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: VEC;
Mid: PROC[u,v: VEC] RETURNS[VEC] = INLINE { RETURN[[(u.x+v.x)/2,(u.y+v.y)/2]] };
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: VEC;
oh: VEC=[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] };
};
Min: PROC[a: VEC, b: VEC] RETURNS[VEC] = INLINE { RETURN[[MIN[a.x,b.x],MIN[a.y,b.y]]] };
Max: PROC[a: VEC, b: VEC] RETURNS[VEC] = INLINE { RETURN[[MAX[a.x,b.x],MAX[a.y,b.y]]] };
In: PROC[a: VEC, b: VEC, c: VEC] RETURNS[BOOLEAN] = INLINE { RETURN[a.x IN[b.x..c.x] AND a.y IN[b.y..c.y]] };
}.