[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
5.5 Frames and pictures
-
frame
-
Frames are canvases for drawing in
PostScript
coordinates. While working with frames directly is occasionally necessary for constructing deferred drawing routines, pictures are usually more convenient to work with. The implicit initializer for frames isnewframe
. The functionbool empty(frame f)
returnstrue
only if the framef
is empty. A frame may be erased with theerase(frame)
routine. The functionspair min(frame)
andpair max(frame)
return the (left,bottom) and (right,top) coordinates of the frame bounding box, respectively. The contents of framesrc
may be appended to framedest
with the commandvoid add(frame dest, frame src);
or prepended with
void prepend(frame dest, frame src);
A frame obtained by aligning frame
f
in the directionalign
, in a manner analogous to thealign
argument oflabel
(see section label), is returned byframe align(frame f, pair align);
To draw or fill a box or ellipse around a label or frame and return the boundary as a path, use one of the predefined
envelope
routinespath box(frame f, Label L="", real xmargin=0, real ymargin=xmargin, pen p=currentpen, filltype filltype=NoFill, bool above=true); path roundbox(frame f, Label L="", real xmargin=0, real ymargin=xmargin, pen p=currentpen, filltype filltype=NoFill, bool above=true); path ellipse(frame f, Label L="", real xmargin=0, real ymargin=xmargin, pen p=currentpen, filltype filltype=NoFill, bool above=true);
-
picture
-
Pictures are high-level structures (see section Structures) defined in the module
plain
that provide canvases for drawing in user coordinates. The default picture is calledcurrentpicture
. A new picture can be created like this:picture pic;
Anonymous pictures can be made by the expression
new picture
.The
size
routine specifies the dimensions of the desired picture:void size(picture pic=currentpicture, real x, real y=x, bool keepAspect=Aspect);
If the
x
andy
sizes are both 0, user coordinates will be interpreted asPostScript
coordinates. In this case, the transform mappingpic
to the final output frame isidentity()
.If exactly one of
x
ory
is 0, no size restriction is imposed in that direction; it will be scaled the same as the other direction.If
keepAspect
is set toAspect
ortrue
, the picture will be scaled with its aspect ratio preserved such that the final width is no more thanx
and the final height is no more thany
.If
keepAspect
is set toIgnoreAspect
orfalse
, the picture will be scaled in both directions so that the final width isx
and the height isy
.To make the user coordinates of picture
pic
represent multiples ofx
units in the x direction andy
units in the y direction, usevoid unitsize(picture pic=currentpicture, real x, real y=x);
When nonzero, these
x
andy
values override the corresponding size parameters of picturepic
.void size(picture pic=currentpicture, real xsize, real ysize, pair min, pair max);
forces the final picture scaling to map the user coordinates
box(min,max)
to a region of widthxsize
and heightysize
(when these parameters are nonzero).Alternatively, calling the routine
transform fixedscaling(picture pic=currentpicture, pair min, pair max, pen p=nullpen, bool warn=false);
will cause picture
pic
to use a fixed scaling to map user coordinates inbox(min,max)
to the (already specified) picture size, taking account of the width of penp
. A warning will be issued if the final picture exceeds the specified size.A picture can be fit to a frame and converted into a
PostScript
image by calling the functionshipout
:void shipout(string prefix=defaultfilename, picture pic, orientation orientation=orientation, string format="", bool wait=false, bool view=true); void shipout(string prefix=defaultfilename, orientation orientation=orientation, string format="", bool wait=false, bool view=true);
A
shipout()
command is added implicitly at file exit if no previousshipout
commands have been executed.A picture
pic
can be explicitly fit to a frame by callingframe pic.fit(real xsize=pic.xsize, real ysize=pic.ysize, bool keepAspect=pic.keepAspect);
The default size and aspect ratio settings are those given to the
size
command (which default to0
,0
, andtrue
, respectively). The transformation that would currently be used to fit a picturepic
to a frame is returned by the member functionpic.calculateTransform()
.In certain cases (e.g. 2D graphs) where only an approximate size estimate for
pic
is available, the picture fitting routineframe pic.scale(real xsize=this.xsize, real ysize=this.ysize, bool keepAspect=this.keepAspect);
(which scales the resulting frame, including labels and fixed-size objects) will enforce perfect compliance with the requested size specification, but should not normally be required.
The default page orientation is
Portrait
; this may be modified by changing the variableorientation
. To output in landscape mode, simply setorientation=Landscape
or issue the commandshipout(Landscape);
To rotate the page by -90 degrees, use the orientation
Seascape
. To rotate the page by 180 degrees, use the orientationUpsideDown
.To draw a bounding box with margins around a picture, fit the picture to a frame using the function
frame bbox(picture pic=currentpicture, real xmargin=0, real ymargin=xmargin, pen p=currentpen, filltype filltype=NoFill);
Here
filltype
specifies one of the following fill types:-
FillDraw
Fill the interior and draw the boundary.
-
FillDraw(real xmargin=0, real ymargin=xmargin, pen fillpen=nullpen,
pen drawpen=nullpen);
Iffillpen
isnullpen
, fill with the drawing pen; otherwise fill with penfillpen
. Ifdrawpen
isnullpen
, draw the boundary withfillpen
; otherwise withdrawpen
. An optional margin ofxmargin
andymargin
can be specified.-
Fill
Fill the interior.
-
Fill(real xmargin=0, real ymargin=xmargin, pen p=nullpen)
If
p
isnullpen
, fill with the drawing pen; otherwise fill with penp
. An optional margin ofxmargin
andymargin
can be specified.-
NoFill
Do not fill.
-
Draw
Draw only the boundary.
-
Draw(real xmargin=0, real ymargin=xmargin, pen p=nullpen)
If
p
isnullpen
, draw the boundary with the drawing pen; otherwise draw with penp
. An optional margin ofxmargin
andymargin
can be specified.-
UnFill
Clip the region.
-
UnFill(real xmargin=0, real ymargin=xmargin)
Clip the region and surrounding margins
xmargin
andymargin
.-
RadialShade(pen penc, pen penr)
Fill varying radially from
penc
at the center of the bounding box topenr
at the edge.
For example, to draw a bounding box around a picture with a 0.25 cm margin and output the resulting frame, use the command:
shipout(bbox(0.25cm));
A
picture
may be fit to a frame with the background color of penp
with the functionbbox(p,Fill)
.The functions
pair min(picture pic, user=false); pair max(picture pic, user=false); pair size(picture pic, user=false);
calculate the
PostScript
bounds that picturepic
would have if it were currently fit to a frame using its default size specification. Ifuser
isfalse
the returned value is inPostScript
coordinates, otherwise it is in user coordinates.The function
pair point(picture pic=currentpicture, pair dir, bool user=true);
is a convenient way of determining the point on the bounding box of
pic
in the directiondir
relative to its center, ignoring the contributions from fixed-size objects (such as labels and arrowheads). Ifuser
istrue
the returned value is in user coordinates, otherwise it is inPostScript
coordinates.The function
pair truepoint(picture pic=currentpicture, pair dir, bool user=true);
is identical to
point
, except that it also accounts for fixed-size objects, using the scaling transform that picturepic
would have if currently fit to a frame using its default size specification. Ifuser
istrue
the returned value is in user coordinates, otherwise it is inPostScript
coordinates.Sometimes it is useful to draw objects on separate pictures and add one picture to another using the
add
function:void add(picture src, bool group=true, filltype filltype=NoFill, bool above=true); void add(picture dest, picture src, bool group=true, filltype filltype=NoFill, bool above=true);
The first example adds
src
tocurrentpicture
; the second one addssrc
todest
. Thegroup
option specifies whether or not the graphical user interfacexasy
should treat all of the elements ofsrc
as a single entity (see section Graphical User Interface),filltype
requests optional background filling or clipping, andabove
specifies whether to addsrc
above or below existing objects.There are also routines to add a picture or frame
src
specified in postscript coordinates to another picturedest
(orcurrentpicture
) about the user coordinateposition
:void add(picture src, pair position, bool group=true, filltype filltype=NoFill, bool above=true); void add(picture dest, picture src, pair position, bool group=true, filltype filltype=NoFill, bool above=true); void add(picture dest=currentpicture, frame src, pair position=0, bool group=true, filltype filltype=NoFill, bool above=true); void add(picture src, pair position, pair align, bool group=true, filltype filltype=NoFill, bool above=true); void add(picture dest, picture src, pair position, pair align, bool group=true, filltype filltype=NoFill, bool above=true); void add(picture dest=currentpicture, frame src, pair position, pair align, bool group=true, filltype filltype=NoFill, bool above=true);
The optional
align
argument in the last three forms specifies a direction to use for aligning the frame, in a manner analogous to thealign
argument oflabel
(see section label). However, one key difference is that whenalign
is not specified, labels are centered, whereas frames and pictures are aligned so that their origin is atposition
. Illustrations of frame alignment can be found in the examples errorbars and image. If you want to align three or more subpictures, group them two at a time:picture pic1; real size=50; size(pic1,size); fill(pic1,(0,0)--(50,100)--(100,0)--cycle,red); picture pic2; size(pic2,size); fill(pic2,unitcircle,green); picture pic3; size(pic3,size); fill(pic3,unitsquare,blue); picture pic; add(pic,pic1.fit(),(0,0),N); add(pic,pic2.fit(),(0,0),10S); add(pic.fit(),(0,0),N); add(pic3.fit(),(0,0),10S);
Alternatively, one can use
attach
to automatically increase the size of picturedest
to accommodate adding a framesrc
about the user coordinateposition
:void attach(picture dest=currentpicture, frame src, pair position=0, bool group=true, filltype filltype=NoFill, bool above=true); void attach(picture dest=currentpicture, frame src, pair position, pair align, bool group=true, filltype filltype=NoFill, bool above=true);
To erase the contents of a picture (but not the size specification), use the function
void erase(picture pic=currentpicture);
To save a snapshot of
currentpicture
,currentpen
, andcurrentprojection
, use the functionsave()
.To restore a snapshot of
currentpicture
,currentpen
, andcurrentprojection
, use the functionrestore()
.Many further examples of picture and frame operations are provided in the base module
plain
.It is possible to insert verbatim
PostScript
commands in a picture with one of the routinesvoid postscript(picture pic=currentpicture, string s); void postscript(picture pic=currentpicture, string s, pair min, pair max)
Here
min
andmax
can be used to specify explicit bounds associated with the resultingPostScript
code.Verbatim TeX commands can be inserted in the intermediate
LaTeX
output file with one of the functionsvoid tex(picture pic=currentpicture, string s); void tex(picture pic=currentpicture, string s, pair min, pair max)
Here
min
andmax
can be used to specify explicit bounds associated with the resultingTeX
code.To issue a global TeX command (such as a TeX macro definition) in the TeX preamble (valid for the remainder of the top-level module) use:
void texpreamble(string s);
The TeX environment can be reset to its initial state, clearing all macro definitions, with the function
void texreset();
The routine
void usepackage(string s, string options="");
provides a convenient abbreviation for
texpreamble("\usepackage["+options+"]{"+s+"}");
that can be used for importing
LaTeX
packages.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |