RandomDemoClient1.mesa
Copyright © 1984, 1986 by Xerox Corporation. All rights reserved.
Created by: Jacobi, September 19, 1984 10:03:05 am PDT
Last Edited by: Christian Jacobi, August 12, 1986 4:09:59 pm PDT
DIRECTORY
BasicTime, Convert, Icons, Imager, ImagerBackdoor, ImagerFont, ImagerPath, ImagerTransformation, RandomDemo, Real, RealFns, Rope, Terminal;
RandomDemoClient1:
CEDAR
PROGRAM
IMPORTS BasicTime, Convert, Icons, Imager, ImagerBackdoor, ImagerFont, ImagerPath, ImagerTransformation, RandomDemo, Real, RealFns, Rope, Terminal =
BEGIN
RModX:
PROC [x:
REAL]
RETURNS [
INTEGER] =
INLINE {
-- maps a real value to screen width
RETURN [Real.RoundI[x-Real.Fix[x/RandomDemo.screenW]*RandomDemo.screenW]]
};
RModY:
PROC [y:
REAL]
RETURNS [
INTEGER] =
INLINE {
-- maps a real value to screen height
RETURN [Real.RoundI[y-Real.Fix[y/RandomDemo.screenH]*RandomDemo.screenH]]
};
RandomColor:
PROC[allowWhite:
BOOL ←
FALSE] = {
Imager.SetColor[
RandomDemo.context,
(
SELECT RandomDemo.Rand[8]
FROM
0 => Imager.black,
1 => IF allowWhite THEN Imager.white ELSE Imager.black,
2 => ImagerBackdoor.invert,
3 => Imager.MakeGray[MAX[1.0, RandomDemo.Rand[6]/5.0]],
4 => ImagerBackdoor.MakeStipple[RandomDemo.Rand[20000], TRUE],
5 => ImagerBackdoor.MakeStipple[RandomDemo.Rand[20000], FALSE],
ENDCASE => Imager.black
)
]
};
RandomRotation:
PROC[] = {
RandomRotate:
PROC[] = {
Imager.TranslateT[RandomDemo.context, [RandomDemo.screenW/2, RandomDemo.screenH/2]];
Imager.RotateT[RandomDemo.context, RandomDemo.Rand[360]];
Imager.TranslateT[RandomDemo.context, [-RandomDemo.screenW/2, -RandomDemo.screenH/2]];
};
IF RandomDemo.Rand[5]=1 THEN RandomRotate[]
};
RandomStrokeThickness:
PROC [fifeoff:
INT] = {
IF 5>=RandomDemo.Rand[fifeoff]
THEN
Imager.SetStrokeWidth[RandomDemo.context, 1.0+RandomDemo.Rand[6]];
};
SwapBackgroundColor:
PROC = {
backG: Terminal.BWBackground ← Terminal.GetBWBackground[RandomDemo.vt];
backG ← (IF backG=black THEN white ELSE black);
[] ← Terminal.SetBWBackground[RandomDemo.vt, backG];
};
Lines
Demo:
PROC = {
x1, x2, y1, y2: INTEGER;
x1 ← RandomDemo.Rand[RandomDemo.screenw];
y1 ← RandomDemo.Rand[RandomDemo.screenh];
DO
x2 ← x1; y2 ← y1;
x1 ← RandomDemo.Rand[RandomDemo.screenw];
y1 ← RandomDemo.Rand[RandomDemo.screenh];
RandomDemo.DrawLine[x1, y1, x2, y2];
RandomDemo.Pause[1];
ENDLOOP;
};
RandomDots:
PROC = {
DO
RandomDemo.DrawDot[RandomDemo.Rand[RandomDemo.screenw], RandomDemo.Rand[RandomDemo.screenh], xor];
ENDLOOP;
};
RectanglePaintingDemo:
PROC = {
x: INT ← RandomDemo.Rand[RandomDemo.screenW];
y: INT ← RandomDemo.Rand[RandomDemo.screenH];
newD: BOOL = RandomDemo.Rand[1]=0;
newC: BOOL = RandomDemo.Rand[3]=0;
dRange: CARDINAL = 2+RandomDemo.Rand[120];
dx: INT;
dy: INT;
NewD:
PROC [] = {
dx ← 1+RandomDemo.Rand[dRange];
dy ← 1+RandomDemo.Rand[dRange]
};
RandomRotation[];
RandomColor[FALSE];
NewD[];
DO
x ← RandomDemo.Rand[RandomDemo.screenW];
y ← RandomDemo.Rand[RandomDemo.screenH];
Imager.MaskBox[RandomDemo.context, [xmin: x-dx, xmax: x+dx, ymin: y-dy, ymax: y+dy]];
RandomDemo.Pause[0];
IF newC THEN RandomColor[TRUE];
IF newD THEN NewD[];
ENDLOOP;
};
RandomWalkDemo:
PROC = {
mid: INTEGER = RandomDemo.Rand[70]+2;
step: INTEGER = mid*2;
last, this: Imager.VEC;
this.x ← RandomDemo.Rand[RandomDemo.screenw];
this.y ← RandomDemo.Rand[RandomDemo.screenh];
RandomStrokeThickness[15];
DO
last ← this;
this.x ← MAX[MIN[INTEGER[RandomDemo.screenw], last.x+RandomDemo.Rand[step]-mid], 0];
this.y ← MAX[MIN[INTEGER[RandomDemo.screenh], last.y+RandomDemo.Rand[step]-mid], 0];
Imager.MaskVector[RandomDemo.context, last, this];
RandomDemo.Pause[0];
ENDLOOP;
};
ParallelLinesDemo:
PROC = {
notParallel: BOOL ← RandomDemo.Rand[3]=1;
off, center: Imager.VEC;
off.x ← RandomDemo.Rand[RandomDemo.screenw/2];
off.y ← RandomDemo.Rand[RandomDemo.screenh/2];
RandomStrokeThickness[25];
DO
center.x ← RandomDemo.Rand[RandomDemo.screenw];
center.y ← RandomDemo.Rand[RandomDemo.screenh];
Imager.MaskVector[RandomDemo.context, [center.x+off.x, center.y+off.y], [center.x-off.x, center.y-off.y]];
RandomDemo.Pause[0];
IF notParallel
THEN {
off.x ← RandomDemo.Rand[RandomDemo.screenw/2];
off.y ← RandomDemo.Rand[RandomDemo.screenh/2];
}
ENDLOOP;
};
PathDemo:
PROC = {
RandomPath:
PROC [s:
CARDINAL ← 200, i:
CARDINAL ← 5]
RETURNS [path: Imager.Trajectory] = {
n: INT ← 3+RandomDemo.Rand[3]*5+RandomDemo.Rand[i];
x: CARDINAL;
y: CARDINAL;
s ← 10+RandomDemo.Rand[s];
x ← RandomDemo.Rand[s];
y ← RandomDemo.Rand[s];
path ← ImagerPath.MoveTo[[x, y]];
FOR i:
INT
IN [0..n-2)
DO
path ← ImagerPath.LineTo[path, [RandomDemo.Rand[s], RandomDemo.Rand[s]]]
ENDLOOP;
path ← ImagerPath.LineTo[path, [x, y]]
};
x, y: CARDINAL;
big: BOOL ← RandomDemo.Rand[2]=0;
fill: BOOL ← RandomDemo.Rand[3]#0;
parity: BOOL ← RandomDemo.Rand[2]=0;
changeColor: BOOL ← RandomDemo.Rand[2]=0;
path: Imager.Trajectory = RandomPath[IF big THEN RandomDemo.screenH ELSE 200];
RandomColor[FALSE];
RandomStrokeThickness[5];
DO
x ← RandomDemo.Rand[RandomDemo.screenW];
y ← RandomDemo.Rand[RandomDemo.screenH];
Imager.TranslateT[RandomDemo.context, [x, y]];
IF fill THEN Imager.MaskFillTrajectory[RandomDemo.context, path, parity]
ELSE Imager.MaskStrokeTrajectory[RandomDemo.context, path];
Imager.TranslateT[RandomDemo.context, [-x, -y]];
IF changeColor THEN RandomColor[fill];
RandomDemo.Pause[0];
ENDLOOP;
};
ScrollOutDemo:
PROC = {
fromX, fromY, toX, toY, num: INTEGER ← 0;
DO
toX ← 1-INTEGER[RandomDemo.Rand[2]];
toY ← 1-INTEGER[RandomDemo.Rand[2]];
IF toX#0 OR toY#0 THEN EXIT
ENDLOOP;
RandomDemo.Pause[RandomDemo.Rand[500]];
Imager.SetColor[RandomDemo.context, Imager.white];
IF RandomDemo.Rand[4]<=2
THEN {
--don't smir
Imager.MaskVector[RandomDemo.context, [0, 0], [0, RandomDemo.screenh]];
Imager.MaskVector[RandomDemo.context, [0, 0], [RandomDemo.screenw, 0]];
Imager.MaskVector[RandomDemo.context, [0, RandomDemo.screenh], [RandomDemo.screenw, RandomDemo.screenh]];
Imager.MaskVector[RandomDemo.context, [RandomDemo.screenw, 0], [RandomDemo.screenw, RandomDemo.screenh]];
};
num ← IF toX=0 THEN RandomDemo.screenh ELSE RandomDemo.screenw;
FOR i:
INT
IN [0..num)
DO
RandomDemo.Pause[0];
ImagerBackdoor.MoveViewRectangle[context: RandomDemo.context,
width: RandomDemo.screenw,
height: RandomDemo.screenh,
fromX: fromX,
fromY: fromY,
toX: toX,
toY: toY
];
ENDLOOP;
};
CopyDemo:
PROC = {
w: INTEGER ← RandomDemo.Rand[RandomDemo.screenw/2];
h: INTEGER ← RandomDemo.Rand[RandomDemo.screenh/2];
fromX: INTEGER ← RandomDemo.Rand[RandomDemo.screenw];
fromY: INTEGER ← RandomDemo.Rand[RandomDemo.screenh];
DO
ImagerBackdoor.MoveViewRectangle[context: RandomDemo.context,
width: w,
height: h,
fromX: fromX,
fromY: fromY,
toX: RandomDemo.Rand[RandomDemo.screenw-w],
toY: RandomDemo.Rand[RandomDemo.screenh-h]
];
RandomDemo.Pause[150];
ENDLOOP;
};
BisectionDemo:
PROC = {
toX, toY, temx, temy, temw, temh, stepx, stepy: INTEGER ← 0;
toX ← RandomDemo.screenw;
toY ← RandomDemo.screenh;
DO
IF toX>toY
THEN {
temw ← temx ← toX ← toX/2 ;
temy ← 0;
temh ← RandomDemo.screenh;
}
ELSE {
temh ← temy ← toY ← toY/2;
temx ← 0;
temw ← RandomDemo.screenw;
};
IF toX<=4 THEN RETURN;
stepx ← stepy ← 0;
DO
stepx ← stepx+temx;
stepy ← stepy+temy;
IF stepx>=RandomDemo.screenw OR stepy>RandomDemo.screenh THEN EXIT;
ImagerBackdoor.MoveViewRectangle[context: RandomDemo.context,
width: temw,
height: temh,
fromX: 0,
fromY: 0,
toX: stepx,
toY: stepy
];
ENDLOOP;
RandomDemo.Pause[200];
ENDLOOP;
};
LetterSoupDemo:
PROC = {
RandRope:
PROC []
RETURNS [r: Rope.
ROPE←
NIL] = {
n: INT ← RandomDemo.Rand[10];
IF n=10 THEN n ← RandomDemo.Rand[100];
FOR i:
INT
IN [0..n]
DO
r ← Rope.Concat[r, Rope.FromChar[RandomDemo.RandChar[]]];
ENDLOOP;
};
RandomWords:
PROC[] = {
r: Rope.ROPE;
SELECT RandomDemo.Rand[6]
FROM
0 => r ← "random demo";
1 => r ← "programmed in Cedar";
2 => r ← RandRope[];
3 => r ← "Copyright (C) 1985 by Xerox Corporation. All rights reserved.";
4 => r ← Convert.RopeFromTime[from: BasicTime.Now[]];
ENDCASE => r ← "Xerox PARC";
DO
Imager.SetXY[RandomDemo.context, [RandomDemo.Rand[RandomDemo.screenw], RandomDemo.Rand[RandomDemo.screenh]]];
Imager.ShowRope[RandomDemo.context, r];
RandomDemo.Pause[0];
ENDLOOP;
};
RandomRotation[];
IF RandomDemo.Rand[5]=0 THEN RandomWords[]
ELSE {
DO
Imager.SetXY[RandomDemo.context,[RandomDemo.Rand[RandomDemo.screenw], RandomDemo.Rand[RandomDemo.screenh]]];
Imager.ShowChar[RandomDemo.context, RandomDemo.RandChar[]];
RandomDemo.Pause[0];
ENDLOOP;
};
};
BigLetterDemo:
PROC = {
Scale:
PROC []
RETURNS [s:
REAL] = {
s ← RandomDemo.Rand[RandomDemo.Rand[35]]*RandomDemo.Rand[64];
IF s<=0 THEN s ← 5;
};
helvetica: Imager.Font ← ImagerFont.Find["xerox/pressfonts/helvetica-mrr"];
w: Imager.VEC;
font: Imager.Font ← ImagerFont.Scale[helvetica, Scale[]];
newScale: BOOL ← RandomDemo.Rand[1]=0;
ch: CHAR;
DO
ch ← RandomDemo.RandChar[];
IF newScale THEN font ← ImagerFont.Scale[helvetica, Scale[]];
RandomColor[];
font ← ImagerFont.Modify[font, ImagerTransformation.Rotate[RandomDemo.Rand[359]]];
w ← ImagerFont.RopeWidth[font: font, rope: Rope.FromChar[ch]];
w.x ← (-w.x/2)+RandomDemo.Rand[RandomDemo.screenw];
w.y ← (-w.y/2)+RandomDemo.Rand[RandomDemo.screenh];
Imager.SetXY[RandomDemo.context, w];
Imager.SetFont[RandomDemo.context, font];
Imager.ShowChar[RandomDemo.context, ch];
ENDLOOP;
};
ShuffleLinesDemo:
PROC = {
x1, x2, y1, y2, dx1, dx2, dy1, dy2: INTEGER;
m: RandomDemo.Mode ← IF RandomDemo.Rand[3]=1 THEN set ELSE xor;
x1 ← RandomDemo.Rand[RandomDemo.screenw];
x2 ← RandomDemo.Rand[RandomDemo.screenw];
y1 ← RandomDemo.Rand[RandomDemo.screenh];
y2 ← RandomDemo.Rand[RandomDemo.screenh];
dx1 ← 1; dx2 ← 0;
dy1 ← RandomDemo.Rand[1]; dy2 ← 1;
DO
x1 ← x1+dx1;
IF x1<0
OR x1>RandomDemo.screenw
THEN {
x1 ← x1-dx1; dx1 ← -dx1; dy1 ← (dy1+2) MOD 3 -1;
};
y1 ← y1+dy1;
IF y1<0
OR y1>RandomDemo.screenh
THEN {
y1 ← y1-dy1; dy1 ← -dy1; dx1 ← (dx1+2) MOD 3 -1;
};
x2 ← x2+dx2;
IF x2<0
OR x2>RandomDemo.screenw
THEN {
x2 ← x2-dx2; dx2 ← -dx2; dy2 ← (dy2+5) MOD 5 -2;
};
y2 ← y2+dy2;
IF y2<0
OR y2>RandomDemo.screenh
THEN {
y2 ← y2-dy2; dy2 ← -dy2; dx2 ← (dx2+5) MOD 5 -5;
};
RandomDemo.DrawLine[x1, y1, x2, y2, m]
ENDLOOP;
};
LineStarsDemo:
PROC = {
mid: INTEGER = RandomDemo.Rand[200]+5;
step: INTEGER = mid*2;
centerx, centery: INTEGER;
x, y: INTEGER;
n: INTEGER ← 3+RandomDemo.Rand[80];
RandomColor[FALSE];
RandomStrokeThickness[];
DO
centerx ← RandomDemo.Rand[RandomDemo.screenw];
centery ← RandomDemo.Rand[RandomDemo.screenh];
FOR i:
INTEGER
IN [0..n]
DO
IF RandomDemo.stop THEN EXIT;
x ← centerx-mid+RandomDemo.Rand[step];
y ← centery-mid+RandomDemo.Rand[step];
Imager.MaskVectorI[RandomDemo.context, centerx, centery, x, y];
ENDLOOP;
ENDLOOP;
};
SomeGraphsDemo:
PROC = {
DoGraphs:
PROC [
Fx,
Fy:
PROC[
REAL]
RETURNS[
REAL]] = {
w: REAL;
last, this: Imager.VEC;
RandomColor[FALSE];
w ← RandomDemo.Rand[1000]/55;
this.x ← RModX[Fx[w]];
this.y ← RModY[Fy[w]];
DO
IF RandomDemo.stop THEN EXIT;
w ← w+1; --w is >0
last ← this;
this.x ← RModX[Fx[w]];
this.y ← RModY[Fy[w]];
Imager.MaskVector[RandomDemo.context, last, this];
ENDLOOP;
};
small1: REAL = 2+RandomDemo.Rand[30];
small2: REAL = 2+RandomDemo.Rand[30];
F1:
PROC[x:
REAL]
RETURNS[f:
REAL] = {
f ← (1+RealFns.Sin[x/small1])*500
};
F2:
PROC[x:
REAL]
RETURNS[f:
REAL] = {
f ← (1+RealFns.Sin[1+x/small2])*500
};
F3:
PROC[x:
REAL]
RETURNS[f:
REAL] = {
f ← (small1+x/small2)*30
};
F4:
PROC[x:
REAL]
RETURNS[f:
REAL] = {
x ← x/small1;
f ← (1+small2*x+x*x)*30
};
RandomStrokeThickness[50];
SELECT RandomDemo.Rand[6]
FROM
0 => DoGraphs[F1, F2];
1 => DoGraphs[F1, F3];
2 => DoGraphs[F1, F4];
3 => DoGraphs[F2, F3];
4 => DoGraphs[F2, F4];
5 => DoGraphs[F3, F4];
ENDCASE => DoGraphs[F1, F2]
};
SlowEraseDemo:
PROC = {
x, y: INTEGER;
d: INTEGER𡤁
n: INTEGER𡤁
Imager.SetColor[RandomDemo.context, Imager.white];
DO
n ← n+1;
IF n>600 THEN {n𡤀 d ← d*2};
x ← RandomDemo.Rand[RandomDemo.screenw];
y ← RandomDemo.Rand[RandomDemo.screenh];
Imager.MaskBox[RandomDemo.context, [xmin: x-d, xmax: x+d, ymin: y-d, ymax: y+d]];
ENDLOOP;
};
DrawIconsDemo:
PROC = {
maxIcon: CARDINAL ← 14; --by peeking IconsImpl.nextFlavor..
icon: CARDINAL ← RandomDemo.Rand[maxIcon];
newI: BOOL ← RandomDemo.Rand[1]=0;
DrawIcon:
PROC = {
Icons.DrawIcon[flavor:
LOOPHOLE[
MIN[icon, maxIcon]],
context: RandomDemo.context,
x: RandomDemo.Rand[RandomDemo.screenW-Icons.iconH],
y: RandomDemo.Rand[RandomDemo.screenH-Icons.iconH]
];
};
DO
IF newI THEN icon ← RandomDemo.Rand[maxIcon];
Imager.DoSave[RandomDemo.context, DrawIcon];
RandomDemo.Pause[0];
ENDLOOP;
};
WalkIconsDemo:
PROC = {
DrawIcon:
PROC = {
Icons.DrawIcon[flavor: LOOPHOLE[icon], context: RandomDemo.context, x: x, y: y];
};
c: NAT = 7;
maxIcon: CARDINAL ← 14; --by peeking IconsImpl.nextFlavor..
icon: CARDINAL ← RandomDemo.Rand[maxIcon];
w: INTEGER ← RandomDemo.screenW-Icons.iconW-c; --1 spare unit
h: INTEGER ← RandomDemo.screenH-Icons.iconH-c; --1 spare unit
x, y, nx, ny: INTEGER; --point to position of icon
dx, dy: INTEGER; --point to position of icon
NewSpeed:
PROC [] = {
dx ← 1+c-INTEGER[RandomDemo.Rand[2*c-2]];
dy ← 1+c-INTEGER[RandomDemo.Rand[2*c-2]];
IF (dx=0 AND dy=0) OR (ABS[dx]+ABS[dy])>8 THEN dy ← 1;
};
x ← c+RandomDemo.Rand[w-c];
y ← c+RandomDemo.Rand[h-c];
NewSpeed[];
Imager.DoSave[RandomDemo.context, DrawIcon];
DO
Terminal.WaitForBWVerticalRetrace[RandomDemo.vt];
nx ← x+dx;
IF nx<c THEN {nx𡤌-nx; dx ← -dx}
ELSE IF nx>w THEN {nx𡤂*w-nx; dx ← -dx};
ny ← y+dy;
IF ny<c THEN {ny𡤌-ny; dy ← -dy}
ELSE IF ny>h THEN {nx𡤂*h-ny; dy ← -dy};
ImagerBackdoor.MoveViewRectangle[context: RandomDemo.context,
width: Icons.iconW+c+c, height: Icons.iconH+c+c,
fromX: x-c, fromY: y-c,
toX: nx-c, toY: ny-c
];
x ← nx; y ← ny;
RandomDemo.Pause[0];
ENDLOOP;
};
Blink
Demo:
PROC =
BEGIN
bg: Terminal.BWBackground;
DO
IF RandomDemo.Rand[1]=0 THEN bg ← white ELSE bg ← black;
[] ← Terminal.SetBWBackground[RandomDemo.vt, bg];
RandomDemo.Pause[RandomDemo.Rand[100]];
ENDLOOP;
END;
CircleClear:
PROC = {
i: INTEGER;
s: INTEGER ← IF RandomDemo.Rand[5]<=3 THEN 1 ELSE 2;
init: INTEGER ← 0;
xc: INTEGER ← 300+RandomDemo.Rand[RandomDemo.screenw-600]; --reduce the range for speed
yc: INTEGER ← 200+RandomDemo.Rand[RandomDemo.screenh-400];
DO
i ← init;
WHILE i<RandomDemo.screenW
DO
RandomDemo.DrawLine[xc, yc, i, 0, clr];
i ← i+s
ENDLOOP;
i ← init;
WHILE i<RandomDemo.screenW
DO
RandomDemo.DrawLine[xc, yc, RandomDemo.screenw, i, clr];
i ← i+s
ENDLOOP;
i ← init;
WHILE i<RandomDemo.screenW
DO
RandomDemo.DrawLine[xc, yc, RandomDemo.screenw-i, RandomDemo.screenh, clr];
i ← i+s
ENDLOOP;
i ← init;
WHILE i<RandomDemo.screenH
DO
RandomDemo.DrawLine[xc, yc, 0, RandomDemo.screenh-i, clr];
i ← i+s
ENDLOOP;
IF init#0 OR s=1 THEN EXIT;
init ← 1;
ENDLOOP
};
RandomDemo.Register[proc: LinesDemo];
RandomDemo.Register[proc: RandomDots];
RandomDemo.Register[proc: SwapBackgroundColor, b1: 0];
RandomDemo.Register[proc: RectanglePaintingDemo, b1: 0.5];
RandomDemo.Register[proc: RandomWalkDemo, b1: 0.8, b2: 0.9];
RandomDemo.Register[proc: LetterSoupDemo, b1: 0.8];
RandomDemo.Register[proc: LineStarsDemo, b1: 0.8];
RandomDemo.Register[proc: SomeGraphsDemo];
RandomDemo.Register[proc: ShuffleLinesDemo, time: 10000];
RandomDemo.Register[proc: PathDemo, b2: 0.9];
RandomDemo.Register[proc: ParallelLinesDemo];
RandomDemo.Register[proc: CopyDemo, b1: 0, b2: 0.9];
RandomDemo.Register[proc: DrawIconsDemo];
RandomDemo.Register[proc: WalkIconsDemo, time: 4000];
RandomDemo.Register[proc: BigLetterDemo];
RandomDemo.Register[proc: BlinkDemo, b1: 0, weight: 0.4, b2: 0.5];
RandomDemo.Register[proc: BisectionDemo, clearProc: TRUE, b2: 0.5];
RandomDemo.Register[proc: ScrollOutDemo, clearProc: TRUE, time: 10000, b2: 0.5];
RandomDemo.Register[proc: SlowEraseDemo, clearProc: TRUE, b2: 0.5];
RandomDemo.Register[proc: CircleClear, clearProc: TRUE, time: 15000, b2: 0.5];
END.