ColorTrixMapSnakeImpl.mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
Bloomenthal, January 13, 1986 9:45:06 pm PST
DIRECTORY CedarProcess, Commander, Controls, ImagerColor, InterminalBackdoor, ImagerPixelMap, Interminal, IO, Process, Random, Real, Terminal, Vector3d, ViewerClasses;
ColorTrixMapSnakeImpl: CEDAR PROGRAM
IMPORTS CedarProcess, Commander, Controls, ImagerColor, InterminalBackdoor, Process, Random, Real, Terminal, Vector3d
~ {
Triple: TYPE ~ Vector3d.Triple;
out: IO.STREAM;
CmSnake: Commander.CommandProc ~ {
outer: ViewerClasses.Viewer ← Controls.OuterViewer[name: "Snake"];
out ← cmd.out;
TRUSTED {Process.Detach[FORK Snaker[NARROW[outer.data, Controls.OuterData]]]};
};
Snaker: PROC [data: Controls.OuterData] ~ {
v: ImagerColor.RGB;
r, g, b: Terminal.ColorValue;
vt: Terminal.Virtual ← InterminalBackdoor.terminal;
CedarProcess.SetPriority[background];
WHILE NOT data.stop DO
FOR i: NAT IN[0..253] DO
[r, g, b] ← Terminal.GetColor[vt: vt, aChannelValue: 254-i];
Terminal.SetColor[vt, 255-i, , r, g, b];
ENDLOOP;
v ← ImagerColor.RGBFromHSV[GetSnakeHSV[]];
Terminal.SetColor[vt, 1,, Real.RoundI[255.0*v.R], Real.RoundI[255.0*v.G], Real.RoundI[255.0*v.B]];
ENDLOOP;
};
count: NAT ← 9;
p0, p1: Triple;
p2: Triple ← GetRandomPt[];
p3: Triple ← GetRandomPt[];
pts: ARRAY [0..9] OF Triple;
Bezier: TYPE ~ RECORD [b0, b1, b2, b3: Triple];
GenPoints: PROC [b: Bezier] ~ {
n: NAT ← 0;
Range: PROC [p: Triple] RETURNS [Triple] ~ {
InnerRange: PROC [r: REAL] RETURNS [REAL] ~ {
RETURN[IF r < 0.01 THEN 0.01 ELSE IF r > 0.99 THEN 0.99 ELSE r];
};
RETURN[[InnerRange[p.x], InnerRange[p.y], InnerRange[p.z]]];
};
Recurse: PROC [b: Bezier, lev: NAT] ~ {
b1, b2: Bezier;
Vector3d.Print["b0: ", b.b0, out];
Vector3d.Print["b1: ", b.b1, out];
Vector3d.Print["b2: ", b.b2, out];
Vector3d.Print["b3: ", b.b3, out];
IF n > 7 THEN RETURN;
pts[n] ← Range[b.b0];
Vector3d.Print["pt: ", pts[n], out];
n ← n+1;
[b1, b2] ← SplitBezier[b];
IF lev < 2 THEN {Recurse[b1, lev+1]; Recurse[b2, lev+1]};
};
Recurse[b, 0];
pts[8] ← Range[p1];
};
GetSnakeHSV: PROC RETURNS [ImagerColor.HSV] ~ {
IF count > 8 THEN {
count ← 0;
p0 ← p3;
p1 ← [p3.x+p3.x-p2.x, p3.y+p3.y-p2.y, p3.z+p3.z-p2.z];
p2 ← GetRandomPt[];
p3 ← GetRandomPt[];
GenPoints[[p0, p1, p2, p3]];
};
count ← count+1;
Vector3d.Print["getsnakehsv: ", pts[count], out];
RETURN[[pts[count].x, pts[count].y, pts[count].z]];
};
GetRandomPt: PROC RETURNS [Triple] ~ {
ChooseReal: PROC RETURNS [REAL] ~ {
RETURN[(1.0/1024.0)*Random.ChooseInt[min: 1, max: 1024]];
};
RETURN[[ChooseReal[], ChooseReal[], ChooseReal[]]];
};
SplitBezier: PUBLIC PROC [b: Bezier] RETURNS [b1, b2: Bezier] ~ {
Average: PROC [p, q: Triple] RETURNS [Triple] ~ {
RETURN[[0.5*(p.x+q.x), 0.5*(p.y+q.y), 0.5*(p.z+q.z)]];
};
b01: Triple ← Average[b.b0, b.b1];
b12: Triple ← Average[b.b1, b.b2];
b23: Triple ← Average[b.b2, b.b3];
b0112: Triple ← Average[b01, b12];
b1223: Triple ← Average[b12, b23];
b01121223: Triple ← Average[b0112, b1223];
RETURN[[b.b0, b01, b0112, b01121223], [b01121223, b1223, b23, b.b3]];
};
Commander.Register["CmSnake", CmSnake, "\nSnake through color space."];
}.