GGBuiltinShapesImpl.mesa
Copyright © 1986 by Xerox Corporation. All rights reserved.
Last edited by Bier on June 23, 1986
Contents: The regular polygons and other commonly requested shapes.
Pier, October 28, 1986 5:27:09 pm PST
Bier, November 13, 1986 6:55:14 pm PST
DIRECTORY
GGBasicTypes, GGBoundBox, GGBuiltinShapes, GGModelTypes, GGOutline, GGSegment, GGSegmentTypes, GGTransform, GGSlice, GGTraj, GGVector, ImagerColor, RealFns, Rope;
GGBuiltinShapesImpl: CEDAR PROGRAM
IMPORTS GGBoundBox, GGOutline, GGSegment, GGSlice, GGTraj, GGTransform, GGVector, ImagerColor, RealFns
EXPORTS GGBuiltinShapes = BEGIN
Outline: TYPE = GGModelTypes.Outline;
Point: TYPE = GGBasicTypes.Point;
Segment: TYPE = GGSegmentTypes.Segment;
Slice: TYPE = GGModelTypes.Slice;
SliceDescriptor: TYPE = GGModelTypes.SliceDescriptor;
Traj: TYPE = GGModelTypes.Traj;
Vector: TYPE = GGBasicTypes.Vector;
Problem: PUBLIC SIGNAL [msg: Rope.ROPE] = CODE;
PolygonInCircle: PUBLIC PROC [sideCount: NAT, origin: Point, radius: REAL] RETURNS [outline: Outline] = {
The bottom-most edge of the returned polygon will be horizontal. The polygon will be inscribed inside the circle with radius "radius" centered at "origin".
traj: Traj;
theta0, theta, deltaTheta, sin, cos: REAL;
lastPoint, thisPoint: Point;
success: BOOL;
seg: Segment;
deltaTheta ← 360.0/sideCount;
theta0 ← -90.0 + deltaTheta/2.0;
sin ← RealFns.SinDeg[theta0];
cos ← RealFns.CosDeg[theta0];
thisPoint ← GGVector.Add[GGVector.Scale[[cos, sin], radius], origin];
traj ← GGTraj.CreateTraj[thisPoint];
lastPoint ← thisPoint;
FOR i: NAT IN [1..sideCount-1] DO
theta ← theta0 + i*deltaTheta;
sin ← RealFns.SinDeg[theta];
cos ← RealFns.CosDeg[theta];
thisPoint ← GGVector.Add[GGVector.Scale[[cos, sin], radius], origin];
seg ← GGSegment.MakeLine[lastPoint, thisPoint, NIL];
success ← GGTraj.AddSegment[traj, hi, seg, lo];
IF NOT success THEN SIGNAL Problem[msg: "Couldn't add segment"];
lastPoint ← thisPoint;
ENDLOOP;
sin ← RealFns.SinDeg[theta0];
cos ← RealFns.CosDeg[theta0];
thisPoint ← GGVector.Add[GGVector.Scale[[cos, sin], radius], origin];
seg ← GGSegment.MakeLine[lastPoint, thisPoint, NIL];
GGTraj.CloseWithSegment[traj, seg, lo];
outline ← GGOutline.CreateOutline[traj, round, ImagerColor.ColorFromGray[0.5]];
};
Box: PUBLIC PROC [origin: Point, sideLength: REAL] RETURNS [slice: Slice] = {
halfSide: REAL ← sideLength/2.0;
box: GGBoundBox.BoundBox ← GGBoundBox.CreateBoundBox[origin.x-halfSide, origin.y-halfSide, origin.x+halfSide, origin.y+halfSide];
sliceD: SliceDescriptor ← GGSlice.MakeBoxSlice[box, ul, GGTransform.Identity[] ];
slice ← sliceD.slice;
};
Circle: PUBLIC PROC [origin: Point, radius: REAL] RETURNS [slice: Slice] = {
outerPoint: Point ← [origin.x + radius, origin.y];
sliceD: SliceDescriptor ← GGSlice.MakeCircleSlice[origin, outerPoint];
slice ← sliceD.slice;
};
Polygon: PUBLIC PROC [sideCount: NAT, origin: Point, sideLength: REAL] RETURNS [outline: Outline] = {
The bottom-most edge of the returned polygon will be horizontal. The polygon will be inscribed inside a circle centered at "origin". Each side of the polygon will have length "sideLength".
};
KnotchedLine: PUBLIC PROC [p0, p1: Point, segmentCount: NAT] RETURNS [outline: Outline] = {
Returns a trajectory with "segmentCount" straight line segments, all of the same length. The entire trajectory is a single straight line, from point p0 to p1. All of its segments are collinear. Knotched lines are useful as rulers.
delta: Vector;
lastPoint, thisPoint: Point;
traj: Traj;
seg: Segment;
success: BOOL;
delta ← GGVector.Scale[GGVector.Sub[p1, p0], 1.0/segmentCount];
lastPoint ← p0;
traj ← GGTraj.CreateTraj[lastPoint];
THROUGH [1..segmentCount-1] DO
thisPoint ← GGVector.Add[lastPoint, delta];
seg ← GGSegment.MakeLine[lastPoint, thisPoint, NIL];
success ← GGTraj.AddSegment[traj, hi, seg, lo];
IF NOT success THEN SIGNAL Problem[msg: "Couldn't add segment"];
lastPoint ← thisPoint;
ENDLOOP;
thisPoint ← p1;
seg ← GGSegment.MakeLine[lastPoint, thisPoint, NIL];
success ← GGTraj.AddSegment[traj, hi, seg, lo];
IF NOT success THEN SIGNAL Problem[msg: "Couldn't add segment"];
outline ← GGOutline.CreateOutline[traj, round, ImagerColor.ColorFromGray[0.5]];
};
END.