From e0c6872cf40896c7be36b11dcc744620f10adf1d Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Mon, 2 Sep 2019 13:46:59 +0900 Subject: Initial commit --- graphics/asymptote/doc/extra/intro.asy | 959 +++++++++++++++++++++++++++++++++ 1 file changed, 959 insertions(+) create mode 100644 graphics/asymptote/doc/extra/intro.asy (limited to 'graphics/asymptote/doc/extra') diff --git a/graphics/asymptote/doc/extra/intro.asy b/graphics/asymptote/doc/extra/intro.asy new file mode 100644 index 0000000000..68aa7e0347 --- /dev/null +++ b/graphics/asymptote/doc/extra/intro.asy @@ -0,0 +1,959 @@ +orientation=Landscape; + +settings.tex="pdflatex"; + +import slide; +import three; +import animate; + +bool long=true; + +usepackage("mflogo"); + +usersetting(); + +viewportsize=pagewidth-2pagemargin; + +// To generate bibliographic references: +// asy -k intro +// bibtex intro_ +// asy -k intro +bibliographystyle("alpha"); + +itempen=fontsize(22pt); +defaultpen(itempen); +viewportmargin=(2,2); + +titlepage(long ? "Asymptote: The Vector Graphics Language" : + "Interactive TeX-Aware 3D Vector Graphics", + "John Bowman and Andy Hammerlindl", +"Department of Mathematical and Statistical Sciences\\ + University of Alberta\\ +%and Instituto Nacional de Matem\'atica Pura e Aplicada (IMPA) +\medskip\Green{Collaborators: Orest Shardt, Michail Vidiassov}", +"June 30, 2010", +"http://asymptote.sf.net/intro.pdf"); + +title("History"); +item("1979: \TeX\ and \MF\ (Knuth)"); +item("1986: 2D B\'ezier control point selection (Hobby)"); +item("1989: MetaPost (Hobby)"); +item("2004: Asymptote"); +subitem("2004: initial public release (Hammerlindl, Bowman, \& Prince)"); +subitem("2005: 3D B\'ezier control point selection (Bowman)"); +subitem("2008: 3D interactive \TeX\ within PDF files (Shardt \& Bowman)"); +subitem("2009: 3D billboard labels that always face camera (Bowman)"); +subitem("2010: 3D PDF enhancements (Vidiassov \& Bowman)"); + +title("Statistics (as of June, 2010)"); +item("Runs under Linux/UNIX, Mac OS X, Microsoft Windows."); +item("4000 downloads/month from primary\hfill\\ + {\tt asymptote.sourceforge.net} site alone."); +item("80\ 000 lines of low-level C++ code."); +item("36\ 000 lines of high-level Asymptote code."); + +if(long) { +title("Vector Graphics"); +item("Raster graphics assign colors to a grid of pixels."); +figure("pixel.pdf"); +item("Vector graphics are graphics which still maintain their look when + inspected at arbitrarily small scales."); +asyfigure(asywrite(" +picture pic; + +path zoombox(real h) { + return box((-h,-h/2),(min(10,h),min(10,h)/2)); +} + +frame zoom(real h, real next=0) { + frame f; + draw(f, (0,-100){W}..{E}(0,0), Arrow); + clip(f, zoombox(h)); + if(next > 0) + draw(f, zoombox(next)); + + return scale(100/h)*f; +} + +add(zoom(100), (0,0)); +add(zoom(10), (200,0)); +add(zoom(1), (400,0)); +")); +} + +title("Cartesian Coordinates"); + +item("Asymptote's graphical capabilities are based on four primitive + commands: {\tt draw}, {\tt label}, {\tt fill}, {\tt clip} \cite{Bowman08}"); + +asyfilecode("diagonal"); +item("units are {\tt PostScript} {\it big points\/} (1 {\tt bp} = +1/72 {\tt inch})"); +item("{\tt --} means join the points with a linear segment to create +a {\it path}"); + +item("{\it cyclic\/} path:"); + +asycode(" +draw((0,0)--(100,0)--(100,100)--(0,100)--cycle); +"); + +title("Scaling to a Given Size"); + +item("{\tt PostScript} units are often inconvenient."); + +item("Instead, scale user coordinates to a specified final size:"); + +asyfilecode("square"); + +item("One can also specify the size in {\tt cm}:"); + +asycode(" +size(3cm,3cm); +draw(unitsquare); +"); + +title("Labels"); + +item("Adding and aligning \LaTeX\ labels is easy:"); + +asycode(preamble="defaultpen(fontsize("+string(fontsize(itempen))+"));", +"size(6cm); +draw(unitsquare); +label(\"$A$\",(0,0),SW); +label(\"$B$\",(1,0),SE); +label(\"$C$\",(1,1),NE); +label(\"$D$\",(0,1),NW); +"); + +title("2D B\'ezier Splines"); + +item("Using {\tt ..} instead of {\tt --} specifies a {\it B\'ezier cubic +spline}:"); + +code(" +draw(z0 .. controls c0 and c1 .. z1,blue); +"); +asyfigure(asywrite("defaultpen(fontsize("+string(fontsize(itempen))+")); +size(0,7cm); +pair z0=(0,0); +pair c0=(1,1); +pair c1=(2,1); +pair z1=(3,0); +draw(z0..controls c0 and c1 .. z1,blue); +draw(z0--c0--c1--z1,dashed); +dot(\"$z_0$\",z0,W,red); +dot(\"$c_0$\",c0,NW,red); +dot(\"$c_1$\",c1,NE,red); +dot(\"$z_1$\",z1,red); +")); + +equation("(1-t)^3 z_0+3t(1-t)^2 c_0+3t^2(1-t) c_1+t^3 z_1, \qquad t\in [0,1]."); + +title("Smooth Paths"); + +item("Asymptote can choose control points for you, using the algorithms of +Hobby and Knuth \cite{Hobby86,Knuth86b}:"); + +string bean=" +pair[] z={(0,0), (0,1), (2,1), (2,0), (1,0)}; +"; + +asycode(preamble="size(130,0);",bean+" +draw(z[0]..z[1]..z[2]..z[3]..z[4]..cycle, + grey+linewidth(5)); +dot(z,linewidth(7)); +"); + +item("First, linear equations involving the curvature are solved to find the + direction through each knot. Then, control points along those directions + are chosen:"); + +asyfigure(asywrite(preamble="size(130,0);",bean+" +path p=z[0]..z[1]..z[2]..z[3]..z[4]..cycle; + +dot(z); +draw(p,lightgrey+linewidth(5)); +dot(z); + +picture output; +save(); +for(int i=0; i0$ and a shift $b$ so that all of the + coordinates when transformed will lie in the interval $[0,S]$."); + +item("That is, if $u$ and $t$ are the user and truesize components:"); +equation("0\le au+t+b \le S."); + +item("Maximize the variable $a$ subject to a number of inequalities."); + +item("Use the simplex method to solve the resulting linear programming problem."); + +if(long) { +title("Sizing"); +item("Every addition of a coordinate $(t,u)$ adds two restrictions"); +equation("au+t+b\ge 0,"); +equation("au+t+b\le S,"); +remark("and each drawing component adds two coordinates."); +item("A figure could easily produce thousands of restrictions, making the + simplex method impractical."); + +item("Most of these restrictions are redundant, however. For instance, with + concentric circles, only the largest circle needs to be accounted for."); +asyfigure(asywrite(" +import palette; +size(160,0); +pen[] p=Rainbow(NColors=11); +for(int i=1; i<10; ++i) { + draw(scale(i)*unitcircle, p[i]+linewidth(2)); +} +")); + +title("Redundant Restrictions"); +item("In general, if $u\le u'$ and $t\le t'$ then"); +equation("au+t+b\le au'+t'+b"); +remark("for all choices of $a>0$ and $b$, so"); +equation("0\le au+t+b\le au'+t'+b\le S."); +item("This defines a partial ordering on coordinates. When sizing a picture, + the program first computes which coordinates are maximal (or minimal) and + only sends effective constraints to the simplex algorithm."); +item("In practice, the linear programming problem will have less than a dozen + restraints."); +item("All picture sizing is implemented in Asymptote code."); +} + +title("Infinite Lines"); +item("Deferred drawing allows us to draw infinite lines."); +code("drawline(P, Q);"); + +asyfigure("elliptic","height=12cm"); + +title("Helpful Math Notation"); + +item("Integer division returns a {\tt real}. Use {\tt quotient} for an integer + result:"); +code("3/4 == 0.75 quotient(3,4) == 0"); + +item("Caret for real and integer exponentiation:"); +code("2^3 2.7^3 2.7^3.2"); + +item("Many expressions can be implicitly scaled by a numeric constant:"); +code("2pi 10cm 2x^2 3sin(x) 2(a+b)"); + +item("Pairs are complex numbers:"); +code("(0,1)*(0,1) == (-1,0)"); + +title("Function Calls"); + +item("Functions can take default arguments in any position. Arguments are + matched to the first possible location:"); +string unitsize="unitsize(0.65cm);"; +string preamble="void drawEllipse(real xsize=1, real ysize=xsize, pen p=blue) { + draw(xscale(xsize)*yscale(ysize)*unitcircle, p); +} +"; + +asycode(preamble=unitsize,preamble+" +drawEllipse(2); +drawEllipse(red); +"); + +item("Arguments can be given by name:"); +asycode(preamble=unitsize+preamble," +drawEllipse(xsize=2, ysize=1); +drawEllipse(ysize=2, xsize=3, green); +"); + +if(long) { +title("Rest Arguments"); +item("Rest arguments allow one to write a function that takes an arbitrary + number of arguments:"); +code(" +int sum(... int[] nums) { + int total=0; + for(int i=0; i < nums.length; ++i) + total += nums[i]; + return total; +} + +sum(1,2,3,4); // returns 10 +sum(); // returns 0 +sum(1,2,3 ... new int[] {4,5,6}); // returns 21 + +int subtract(int start ... int[] subs) { + return start - sum(... subs); +} +"); +} + +title("High-Order Functions"); + +item("Functions are first-class values. They can be passed to other + functions:"); +code("import graph; +real f(real x) { + return x*sin(10x); +} +draw(graph(f,-3,3,300),red);"); +asyfigure(asywrite(" +import graph; +size(300,0); +real f(real x) { + return x*sin(10x); +} +draw(graph(f,-3,3,300),red); +")); + +if(long) { +title("Higher-Order Functions"); +item("Functions can return functions:"); +equation("f_n(x)=n\sin\left(\frac{x}{n}\right)."); +skip(); +string preamble=" +import graph; +size(300,0); +"; +string graphfunc2=" +typedef real func(real); +func f(int n) { + real fn(real x) { + return n*sin(x/n); + } + return fn; +} + +func f1=f(1); +real y=f1(pi); + +for(int i=1; i<=5; ++i) + draw(graph(f(i),-10,10),red); +"; +code(graphfunc2); +string name=asywrite(graphfunc2,preamble=preamble); +asy(nativeformat(),name+".asy"); +label(graphic(name+"."+nativeformat()),(0.5,0), + Fill(figureborder,figuremattpen)); + +title("Anonymous Functions"); + +item("Create new functions with {\tt new}:"); +code(" +path p=graph(new real (real x) { return x*sin(10x); },-3,3,red); + +func f(int n) { + return new real (real x) { return n*sin(x/n); }; +}"); + +item("Function definitions are just syntactic sugar for assigning function +objects to variables."); +code(" +real square(real x) { + return x^2; +} +"); + +remark("is equivalent to"); +code(" +real square(real x); +square=new real (real x) { + return x^2; +}; +"); + +title("Structures"); + +item("As in other languages, structures group together data."); +code(" +struct Person { + string firstname, lastname; + int age; +} +Person bob=new Person; +bob.firstname=\"Bob\"; +bob.lastname=\"Chesterton\"; +bob.age=24; +"); + +item("Any code in the structure body will be executed every time a new structure + is allocated..."); +code(" +struct Person { + write(\"Making a person.\"); + string firstname, lastname; + int age=18; +} +Person eve=new Person; // Writes \"Making a person.\" +write(eve.age); // Writes 18. +"); + +title("Modules"); + +item("Function and structure definitions can be grouped into modules:"); +code(" +// powers.asy +real square(real x) { return x^2; } +real cube(real x) { return x^3; } +"); +remark("and imported:"); +code(" +import powers; +real eight=cube(2.0); +draw(graph(powers.square, -1, 1)); +"); +} + +title("Object-Oriented Programming"); +item("Functions are defined for each instance of a structure."); +code(" +struct Quadratic { + real a,b,c; + real discriminant() { + return b^2-4*a*c; + } + real eval(real x) { + return a*x^2 + b*x + c; + } +} +"); + +item("This allows us to construct ``methods'' which are just normal functions + declared in the environment of a particular object:"); +code(" +Quadratic poly=new Quadratic; +poly.a=-1; poly.b=1; poly.c=2; + +real f(real x)=poly.eval; +real y=f(2); +draw(graph(poly.eval, -5, 5)); +"); + +title("Specialization"); + +item("Can create specialized objects just by redefining methods:"); +code(" +struct Shape { + void draw(); + real area(); +} + +Shape rectangle(real w, real h) { + Shape s=new Shape; + s.draw = new void () { + fill((0,0)--(w,0)--(w,h)--(0,h)--cycle); }; + s.area = new real () { return w*h; }; + return s; +} + +Shape circle(real radius) { + Shape s=new Shape; + s.draw = new void () { fill(scale(radius)*unitcircle); }; + s.area = new real () { return pi*radius^2; } + return s; +} +"); + +title("Overloading"); +item("Consider the code:"); +code(" +int x1=2; +int x2() { + return 7; +} +int x3(int y) { + return 2y; +} + +write(x1+x2()); // Writes 9. +write(x3(x1)+x2()); // Writes 11. +"); + +title("Overloading"); +item("{\tt x1}, {\tt x2}, and {\tt x3} are never used in the same context, so + they can all be renamed {\tt x} without ambiguity:"); +code(" +int x=2; +int x() { + return 7; +} +int x(int y) { + return 2y; +} + +write(x+x()); // Writes 9. +write(x(x)+x()); // Writes 11. +"); + +item("Function definitions are just variable definitions, but variables are + distinguished by their signatures to allow overloading."); + +title("Operators"); +item("Operators are just syntactic sugar for functions, and can be addressed or + defined as functions with the {\tt operator} keyword."); +code(" +int add(int x, int y)=operator +; +write(add(2,3)); // Writes 5. + +// Don't try this at home. +int operator +(int x, int y) { + return add(2x,y); +} +write(2+3); // Writes 7. +"); +item("This allows operators to be defined for new types."); + +title("Operators"); +item("Operators for constructing paths are also functions:"); +code("a.. controls b and c .. d--e"); +remark("is equivalent to"); +code( + "operator --(operator ..(a, operator controls(b,c), d), e)"); +item("This allowed us to redefine all of the path operators for 3D paths."); + +title("Summary"); + +item("Asymptote:"); +subitem("uses IEEE floating point numerics;"); +subitem("uses C++/Java-like syntax;"); +subitem("supports deferred drawing for automatic picture sizing;"); +subitem("supports Grayscale, RGB, CMYK, and HSV colour spaces;"); +subitem("supports PostScript shading, pattern fills, and function shading;"); +subitem("can fill nonsimply connected regions;"); +subitem("generalizes MetaPost path construction algorithms to 3D;"); +subitem("lifts \TeX\ to 3D;"); +subitem("supports 3D billboard labels and PDF grouping."); + +bibliography("../examples/refs"); + +viewportmargin=(2,2); +viewportsize=0; +defaultpen(0.5); +title("\mbox{Asymptote: 2D \& 3D Vector Graphics Language}"); +asyinclude("../examples/logo3"); +skip(); +center("\tt http://asymptote.sf.net"); +center("(freely available under the LGPL license)"); + +// LocalWords: pdflatex mflogo viewportsize pagewidth pagemargin goysr bibtex +// LocalWords: itempen defaultrender medskip Orest Shardt Vidiassov MF ezier +// LocalWords: Hammerlindl MetaPost PDF hfill LGPL pdf asywrite zoombox LaTeX +// LocalWords: asyfilecode PostScript asycode unitsquare beziercurve grey bw +// LocalWords: lightgrey zerowinding evenodd sw unitsize drawEllipse nums fn +// LocalWords: frac graphfunc func nativeformat figureborder figuremattpen bt +// LocalWords: firstname lastname eval eetomumu binarytree filecode datagraph +// LocalWords: lineargraph filegraph loggraph secondaryaxis imagecontour ij +// LocalWords: tridiagonal Hobbydir nonumber Hobbycontrol th viewportmargin +// LocalWords: asyinclude dotpen wheelpoint yequals xaxis yaxis cardsize mc +// LocalWords: polargraph filldraw addPoint lightblue truesize le au NColors +// LocalWords: drawline unityroot mult oct intang IEEE numerics HSV colour +// LocalWords: nonsimply -- cgit v1.2.3