| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] | 
5.5 Frames and pictures
-  frame
- 
Frames are canvases for drawing in PostScriptcoordinates. 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)returnstrueonly if the framefis 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 framesrcmay be appended to framedestwith the commandvoid add(frame dest, frame src); or prepended with void prepend(frame dest, frame src); A frame obtained by aligning frame fin the directionalign, in a manner analogous to thealignargument 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 enveloperoutinespath 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 plainthat 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 sizeroutine specifies the dimensions of the desired picture:void size(picture pic=currentpicture, real x, real y=x, bool keepAspect=Aspect);If the xandysizes are both 0, user coordinates will be interpreted asPostScriptcoordinates. In this case, the transform mappingpicto the final output frame isidentity().If exactly one of xoryis 0, no size restriction is imposed in that direction; it will be scaled the same as the other direction.If keepAspectis set toAspectortrue, the picture will be scaled with its aspect ratio preserved such that the final width is no more thanxand the final height is no more thany.If keepAspectis set toIgnoreAspectorfalse, the picture will be scaled in both directions so that the final width isxand the height isy.To make the user coordinates of picture picrepresent multiples ofxunits in the x direction andyunits in the y direction, usevoid unitsize(picture pic=currentpicture, real x, real y=x); When nonzero, these xandyvalues 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 widthxsizeand 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 picto 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 PostScriptimage 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 previousshipoutcommands have been executed.A picture piccan 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 sizecommand (which default to0,0, andtrue, respectively). The transformation that would currently be used to fit a picturepicto a frame is returned by the member functionpic.calculateTransform().In certain cases (e.g. 2D graphs) where only an approximate size estimate for picis 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=Landscapeor 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 filltypespecifies 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);If- fillpenis- nullpen, fill with the drawing pen; otherwise fill with pen- fillpen. If- drawpenis- nullpen, draw the boundary with- fillpen; otherwise with- drawpen. An optional margin of- xmarginand- ymargincan be specified.
-  Fill
- Fill the interior. 
-  Fill(real xmargin=0, real ymargin=xmargin, pen p=nullpen)
- If - pis- nullpen, fill with the drawing pen; otherwise fill with pen- p. An optional margin of- xmarginand- ymargincan be specified.
-  NoFill
- Do not fill. 
-  Draw
- Draw only the boundary. 
-  Draw(real xmargin=0, real ymargin=xmargin, pen p=nullpen)
- If - pis- nullpen, draw the boundary with the drawing pen; otherwise draw with pen- p. An optional margin of- xmarginand- ymargincan be specified.
-  UnFill
- Clip the region. 
-  UnFill(real xmargin=0, real ymargin=xmargin)
- Clip the region and surrounding margins - xmarginand- ymargin.
-  RadialShade(pen penc, pen penr)
- Fill varying radially from - pencat the center of the bounding box to- penrat 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 picturemay be fit to a frame with the background color of penpwith 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 PostScriptbounds that picturepicwould have if it were currently fit to a frame using its default size specification. Ifuserisfalsethe returned value is inPostScriptcoordinates, 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 picin the directiondirrelative to its center, ignoring the contributions from fixed-size objects (such as labels and arrowheads). Ifuseristruethe returned value is in user coordinates, otherwise it is inPostScriptcoordinates.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 picturepicwould have if currently fit to a frame using its default size specification. Ifuseristruethe returned value is in user coordinates, otherwise it is inPostScriptcoordinates.Sometimes it is useful to draw objects on separate pictures and add one picture to another using the addfunction: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 srctocurrentpicture; the second one addssrctodest. Thegroupoption specifies whether or not the graphical user interfacexasyshould treat all of the elements ofsrcas a single entity (see section Graphical User Interface),filltyperequests optional background filling or clipping, andabovespecifies whether to addsrcabove or below existing objects.There are also routines to add a picture or frame srcspecified 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 alignargument in the last three forms specifies a direction to use for aligning the frame, in a manner analogous to thealignargument oflabel(see section label). However, one key difference is that whenalignis 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 attachto automatically increase the size of picturedestto accommodate adding a framesrcabout 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 PostScriptcommands 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 minandmaxcan be used to specify explicit bounds associated with the resultingPostScriptcode.Verbatim TeX commands can be inserted in the intermediate LaTeXoutput file with one of the functionsvoid tex(picture pic=currentpicture, string s); void tex(picture pic=currentpicture, string s, pair min, pair max) Here minandmaxcan be used to specify explicit bounds associated with the resultingTeXcode.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 LaTeXpackages.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] | 
