在 bbox 框架顶部添加贴纸

在 bbox 框架顶部添加贴纸

我正在使用以下渐近线代码。

size(6cm,4cm,false);
import graph;

real f(real x) {return sqrt(x);}
real xmin=0, xmax=50, xStep = 5, xstep = 0;
real ymin=0, ymax=7, yStep = 1, ystep = 0;

draw(graph(f,xmin,xmax,n=200),1bp+blue);

xlimits(xmin,xmax,crop=true); 
ylimits(ymin,ymax,crop=true); 

xaxis(BottomTop,nullpen,Ticks("%",extend=true,Step=xStep,step=xstep,pTick=mediumgrey,ptick=lightgrey));
yaxis(LeftRight,nullpen,Ticks("%",extend=true,Step=yStep,step=ystep,pTick=mediumgrey,ptick=lightgrey));

ticks Tx = Ticks(Label(fontsize(5pt)),Step=xStep,step=xstep,pTick=black,ptick=black,NoZero,Size=2,size=1);
xaxis(Tx,Arrow(size=3),above=true);

ticks Ty = Ticks(shift(0,1)*Label(fontsize(5pt)),Step=yStep,step=ystep,pTick=black,ptick=black,NoZero,Size=2,size=1);
yaxis(Ty,Arrow(size=3),above=true);

shipout(bbox(xmargin=1mm,red+1bp));

图形

我需要在图片的左上角放一些东西(里面有数字的小贴纸)。我试了几种方法,但都没有成功。

答案1

这是一种方法。我修改了您的代码,在绘制边界框之后但在发货之前添加一个带框的标签。

我通过反复试验找到了盒子和标签。例如,我不知道为什么“ySticker”变量必须这么大。

size(6cm,4cm,false);
import graph;

real f(real x) {return sqrt(x);}
real xmin=0, xmax=50, xStep = 5, xstep = 0;
real ymin=0, ymax=7, yStep = 1, ystep = 0;

draw(graph(f,xmin,xmax,n=200),1bp+blue);

xlimits(xmin,xmax,crop=true); 
ylimits(ymin,ymax,crop=true); 

xaxis(BottomTop,nullpen,Ticks("%",extend=true,Step=xStep,step=xstep,pTick=mediumgrey,ptick=lightgrey));
yaxis(LeftRight,nullpen,Ticks("%",extend=true,Step=yStep,step=ystep,pTick=mediumgrey,ptick=lightgrey));

ticks Tx = Ticks(Label(fontsize(5pt)),Step=xStep,step=xstep,pTick=black,ptick=black,NoZero,Size=2,size=1);
xaxis(Tx,Arrow(size=3),above=true);

ticks Ty = Ticks(shift(0,1)*Label(fontsize(5pt)),Step=yStep,step=ystep,pTick=black,ptick=black,NoZero,Size=2,size=1);
yaxis(Ty,Arrow(size=3),above=true);

frame f = bbox(xmargin=1mm,red+1bp,Draw);

real xSticker = 0.0;
real ySticker = 110.0;
filldraw(f, shift(xSticker,ySticker)*scale(15.0,8.0)*shift(-0.5,-0.5)*unitsquare, white, black);
label(f, scale(0.5)*"test", (xSticker,ySticker));

shipout(f);

在此处输入图片描述

答案2

我在这里提出了两种解决方案,无需对贴纸位置的值进行实验。第一个解决方案是使用框架。如果是ff包含图形+边界框的框架,则函数max(ff)允许min(ff)获取边界框的坐标(回想一下,请参阅文档,框架是用于在 PostScript 坐标中绘制的画布)。因此,添加贴纸的点的坐标是众所周知的,在 x 和 y 坐标中添加 5bp 就足够了。它避免了找到合适的值,ySticker并且是使用的机会align。请查看代码

  size(6cm,4cm,false);
  import graph;

  real f(real x) {return sqrt(x);}
  real xmin=0, xmax=50, xStep = 5, xstep = 0;
  real ymin=0, ymax=7, yStep = 1, ystep = 0;

  draw(graph(f,xmin,xmax,n=200),1bp+blue);

  xlimits(xmin,xmax,crop=true); 
  ylimits(ymin,ymax,crop=true); 

  xaxis(BottomTop,nullpen,Ticks("%",extend=true,Step=xStep,step=xstep,pTick=mediumgrey,ptick=lightgrey));
  yaxis(LeftRight,nullpen,Ticks("%",extend=true,Step=yStep,step=ystep,pTick=mediumgrey,ptick=lightgrey));

  ticks Tx = Ticks(Label(fontsize(5pt)),Step=xStep,step=xstep,pTick=black,ptick=black,NoZero,Size=2,size=1);
  xaxis(0,50,Tx,Arrow(size=3),above=true);

  ticks Ty = Ticks(shift(0,1)*Label(fontsize(5pt)),Step=yStep,step=ystep,pTick=black,ptick=black,NoZero,Size=2,size=1);
  yaxis(0,7,Ty,Arrow(size=3),above=true);


  frame ff = bbox(xmargin=1mm,red+1bp,Draw);

  frame sticker;
  (box(sticker,scale(.5)*"test test test"));


  frame fin;
  add(fin,ff);
  pair sposm =min(ff);
  pair sposM= max(ff);
  add(ff,shift((min(ff).x+5bp,max(ff).y+5bp))*(align(sticker,Align)));
  add(ff,shift(sposM.x,sposM.y)*(align(sticker,(-Align.x,Align.y))));
  shipout(ff);

结果 在此处输入图片描述

第二种解决方案是创建一个pic1包含图形和提供框架的边界框的图片ff。然后创建一个(当前)图片,首先添加ff。为了将标签添加到正确的位置,我使用了truepoint允许将点放置在图片边界上的函数。这是一个特定的函数,因为图片是一个高级结构,尺寸约束是在最后执行的,边界可以因线而异。我添加了两个不同的align测试。请查看代码

  picture pic1;
  size(pic1,6cm,4cm,false);
  import graph;

  real f(real x) {return sqrt(x);}
  real xmin=0, xmax=50, xStep = 5, xstep = 0;
  real ymin=0, ymax=7, yStep = 1, ystep = 0;

  draw(pic1,graph(f,xmin,xmax,n=200),1bp+blue);

  xlimits(pic1,xmin,xmax,crop=true); 
  ylimits(pic1,ymin,ymax,crop=true); 

  xaxis(pic1,BottomTop,nullpen,Ticks("%",extend=true,Step=xStep,step=xstep,pTick=mediumgrey,ptick=lightgrey));
  yaxis(pic1,LeftRight,nullpen,Ticks("%",extend=true,Step=yStep,step=ystep,pTick=mediumgrey,ptick=lightgrey));

  ticks Tx = Ticks(Label(fontsize(5pt)),Step=xStep,step=xstep,pTick=black,ptick=black,NoZero,Size=2,size=1);
  xaxis(pic1,0,50,Tx,Arrow(size=3),above=true);

  ticks Ty = Ticks(shift(0,1)*Label(fontsize(5pt)),Step=yStep,step=ystep,pTick=black,ptick=black,NoZero,Size=2,size=1);
  yaxis(pic1,0,7,Ty,Arrow(size=3),above=true);


  frame ff = bbox(pic1,xmargin=1mm,red+1bp,Draw);

  add(ff);
  frame sticker;
  (box(sticker,scale(.5)*"test test test"));
  add(align(sticker,N+E),truepoint(W+N));
  add(align(sticker,-Align),truepoint(S+E));
  dot(truepoint(W));
  dot(truepoint(E));
  dot(truepoint(S));

和图片 在此处输入图片描述

相关内容