vectorgraphics / asymptote

2D & 3D TeX-Aware Vector Graphics Language
https://asymptote.sourceforge.io/
GNU General Public License v3.0
556 stars 93 forks source link

A bit problem with frame pic.fit(); #236

Closed justonlyasy closed 3 years ago

justonlyasy commented 3 years ago

Test code, at http://asymptote.ualberta.ca/

 import x11colors;
 import graph; 
 size(300);
      defaultpen(fontsize(6));

      currentlight.background = lightcyan; 

      real f(real x){return 1 + cos(x);}
      path q = polargraph(f,0,2*pi,350);
      filldraw(q--cycle,lightgreen,MidnightBlue);
      real x = 2.4; 
      draw((-x,0)--(x,0),Arrows());
      draw((0,-x)--(0,x),Arrows());

      int k = 6;
      int n = 20; 
      fill(2*k/n*expi(2*pi*5/40)--2*(k+1)/n*expi(2*pi*5/40)--
      2*(k+1)/n*expi(2*pi*4/40)--2*k/n*expi(2*pi*4/40)--cycle,lightred);

      int n = 40;
      real th, r;
      path p;
      real[][] tms;

      for(int i=0; i<n; ++i){
        th = 2*pi*i/n; 
        draw((0,0)--f(th)*expi(th));
      }

      int n = 20;
      for(int i=0; i<n; ++i){
        r = 2*i/n; 
        p = circle((0,0), r);
        tms = intersections(p,q);
        if (tms.length > 1){
          draw(subpath(p,0,tms[0][0]),MidnightBlue);
          draw(subpath(p,tms[1][0],length(p)),MidnightBlue);
        }
      }

frame myframe=currentpicture.fit();
picture pic;
size(pic,300);
path myclippath = box((0.1,0.1),(0.3,0.3));
draw(myclippath);
path bigclippath = scale(4) * myclippath;

add(pic,scale(4) * myframe);
layer(pic);
clip(pic,bigclippath);
draw(pic,bigclippath);

transform t=shift(-3,1);
// transform t=shift(-2,1);
add(t*pic);

pair A=point(t*pic,dir(-45));
draw(A--(min(myclippath).x,max(myclippath).y),BeginArrow);
label(scale(2)*"$f(x)=1 + cos(x)$",(1,1.5));

You can see the difference between transform t=shift(-3,1); and transform t=shift(-2,1);. How can I use any transform but preserving contents of the clip picture?

johncbowman commented 3 years ago

I'll look into it. Don't forget the \ on \cos.

justonlyasy commented 3 years ago

Another example of this, from https://asymptote.sourceforge.io/gallery/xsin1x.asy.

If the center is (0,0), no problem !? (as I see).

import graph;
size(15cm);

real f(real x) {return (x != 0.0) ? x * sin(1.0 / x) : 0.0;}
pair F(real x) {return (x,f(x));}

xaxis("$x$",red,Arrow);
yaxis("$y$",red,Arrow);
draw(graph(f,-1.2/pi,1.2/pi,1000));
label("$x\sin\frac{1}{x}$",F(1.1/pi),NW);

label("$0.15$",(0,.15),dir(180));
label("$-0.15$",(0,-.15),dir(180));
label("$0.15$",(.15,0),dir(-90));
label("$-0.15$",(-.15,0),dir(-90));
label("$0.3$",(.3,0),dir(-90));
label("$-0.3$",(-.3,0),dir(-90));

frame myframe=currentpicture.fit();
picture pic1,pic2;
size(pic1,15cm);
size(pic2,15cm);
path myclippath1 = box(-(0.02,0.02),(0.02,0.02));
path myclippath2 = circle((0.03,0.02),0.022);
draw(myclippath1,green);
draw(myclippath2,green);

path bigclippath1 = scale(4) * myclippath1;
path bigclippath2 = scale(4) * myclippath2;

add(pic1,scale(4) * myframe);
add(pic2,scale(4) * myframe);
layer(pic1);
layer(pic2);

clip(pic1,bigclippath1);
draw(pic1,bigclippath1,red);
clip(pic2,bigclippath2);
draw(pic2,bigclippath2,red);

transform t1=shift(-0.1,0.2);
transform t2=shift(0.1,0.2);
add(t1*pic1);
add(t2*pic2);

pair A=point(t1*pic1,dir(-60));
draw(Label("text",LeftSide,Rotate(dir(A--(min(myclippath1).x,max(myclippath1).y)))),
     A--(min(myclippath1).x,max(myclippath1).y),blue,BeginArrow);
pair B=point(t2*pic2,dir(-120));
draw(Label("text",LeftSide,Rotate(dir(reverse(B--max(myclippath2))))),
     B--max(myclippath2),blue,BeginArrow);

transform t2=shift(0.1,0.2); is different with transform t2=shift(0.2,0.2);

johncbowman commented 3 years ago

If you use the deferred drawing technique illustrated in xsin1x.asy, you shouldn't have problems with the picture resizing like this. See the first article here:

https://asymptote.sourceforge.io/articles/

justonlyasy commented 3 years ago

I am still not familiar with the deferred drawing technique, so you can please post an illustration about the first example using this technique. Actually, I have asked two questions related to this problem,

  1. https://tex.stackexchange.com/q/583732/219419, but Charles Staats uses clip.
  2. here. but no one support with the complete code.

Additional, the example in this question is inspired from this (he also uses clip, however, there is a problem about this code) and this (Charles Staats also uses clip).

asmwarrior commented 3 years ago

There are many examples in O.G.'s answers, see here: What does the align argument do in this Asymptote code? - TeX - LaTeX Stack Exchange