Autograph

Revision as of 14:58, 2 September 2011 by PythonNut (talk | contribs) (Examples)
Asymptote (Vector Graphics Language)
Getting Started - Basics - Drawing - Labeling - Filling - Useful functions - Examples - Macros and Packages

Help - Reference - Advanced Asymptote - 3D Graphics - CSE5 Package - How to

Autograph

[asy] import contour; import graph; real f(real x) { return x^2-1.333; } draw(graph(f,-1.5,1.5),blue+linewidth(0.5),Arrows); size(75); real f(real x, real y) {return abs(x^3) + abs(y^3);} draw(contour(f,(-6,-6),(6,6), new real[] {5}),red+linewidth(1)); label("$Auto\;Graph$",(0,0.3),red); label("$V-4\beta$",(0,-0.3),red); dot((1.5,0),red); dot((0,1.5),red); dot((0,-1.5),red); dot((-1.5,0),red);[/asy] Autograph is a piece of code written in Asymptote that graphs functions. The user must only set the function, and optionally

  • the x and y maximums and minimums
  • the color of the graph line
  • the density of the ticks
  • whether to show the grid or not
  • wether to mark lattice points or not
  • the ratio of x to y
  • the width of the line

It is targeted to beginners in asymptote who don't want to learn all of it, but still want to make nice graphs.

This code is currently developed by PythonNut, a AoPS user. The changes are kept on his blog.

Examples

Some pretty graphs: [asy] /* AUTO-GRAPH V-4 beta by PythonNut*/  /* Customizations: feel free to edit */ import math; import graph; /* x maximum and minimum */ int X_max = 10; int X_min =-10; /* y maximum and minimum */ int Y_max = 10; int Y_min = -10; /* linewidth */ real line_width = 0.75; /* graph color */ pen graph_color = heavygreen; /* special */ bool mark_lattice = true; bool show_grid = true; real X_tick_density = 1; real Y_tick_density = 1; real ratio = 1; real resolution = 0.0001; int size = 300; /* graph function */ real f(real x)    {    return 2x^3-6x^2+3x-1; /* type function to be graphed here */ }  /* The Code. Do not disturb unless you know what you are doing */  size(size);unitsize(size*ratio,size);Label l;l.p=fontsize(6); xaxis("$x$",X_min,X_max,Ticks(l,X_tick_density,(X_tick_density/2),NoZero),Arrows); yaxis("$y$",Y_min,Y_max,Ticks(l,Y_tick_density,(Y_tick_density/2),NoZero),Arrows);// if (show_grid){add(shift(X_min,Y_min)*grid(X_max-X_min,Y_max-Y_min));} real temp = X_min; while (! (f(temp) <= Y_max && f(temp) >= Y_min && temp < X_max)) {temp += resolution;} real Temp1 = temp; while (f(Temp1) <= Y_max && f(Temp1) >= Y_min && Temp1 < X_max) {Temp1 += resolution;} real Temp2 = temp; while (f(Temp2) <= Y_max && f(Temp2) >= Y_min && Temp2 > X_min) {Temp2 -= resolution;} draw(graph(f,Temp2,Temp1,n=2400),graph_color+linewidth(line_width),Arrows); temp = Temp1+resolution; while (temp <= X_max) {while (! (f(temp) <= Y_max && f(temp) >= Y_min) && temp < X_max) {temp += resolution;} real Temp1 = temp; while (f(Temp1) <= Y_max && f(Temp1) >= Y_min && Temp1 < X_max) {Temp1 += resolution;} real Temp2 = temp; while (f(Temp2) <= Y_max && f(Temp2) >= Y_min && Temp2 > X_min) {Temp2 -= resolution;} if (Temp1 <= X_max) {draw(graph(f,Temp2,Temp1,n=2400),graph_color+linewidth(line_width),Arrows);} temp = Temp1+resolution; continue;} temp = X_max; while (f(temp) <= Y_max && f(temp) >= Y_min && temp >= X_min) {temp -= resolution;} if (f(X_max) <= Y_max && f(X_max) >= Y_min) {draw(graph(f,temp,X_max,n=2400),graph_color+linewidth(line_width),Arrows);} if (mark_lattice) {for (real temp = X_min; temp <= X_max; ++temp) {if (f(temp)%1==0 && f(temp)<=Y_max && f(temp)>=Y_min) {dot((temp,f(temp)),graph_color+linewidth(line_width*4));}}} dot((0,0)); /* Property of "PythonNut" 2011*/ [/asy] [asy] /* AUTO-GRAPH V-4 beta by PythonNut*/  /* Customizations: feel free to edit */ import math; import graph; /* x maximum and minimum */ int X_max = 10; int X_min =-10; /* y maximum and minimum */ int Y_max = 1; int Y_min = -1; /* linewidth */ real line_width = 0.75; /* graph color */ pen graph_color = blue; /* special */ bool mark_lattice = false; bool show_grid = true; real X_tick_density = 1; real Y_tick_density = 1; real ratio = 1; real resolution = 0.0001; int size = 300; /* graph function */ real f(real x)    {    return sin(x); /* type function to be graphed here */ }  /* The Code. Do not disturb unless you know what you are doing */  size(size);unitsize(size*ratio,size);Label l;l.p=fontsize(6); xaxis("$x$",X_min,X_max,Ticks(l,X_tick_density,(X_tick_density/2),NoZero),Arrows); yaxis("$y$",Y_min,Y_max,Ticks(l,Y_tick_density,(Y_tick_density/2),NoZero),Arrows);// if (show_grid){add(shift(X_min,Y_min)*grid(X_max-X_min,Y_max-Y_min));} real temp = X_min; while (! (f(temp) <= Y_max && f(temp) >= Y_min && temp < X_max)) {temp += resolution;} real Temp1 = temp; while (f(Temp1) <= Y_max && f(Temp1) >= Y_min && Temp1 < X_max) {Temp1 += resolution;} real Temp2 = temp; while (f(Temp2) <= Y_max && f(Temp2) >= Y_min && Temp2 > X_min) {Temp2 -= resolution;} draw(graph(f,Temp2,Temp1,n=2400),graph_color+linewidth(line_width),Arrows); temp = Temp1+resolution; while (temp <= X_max) {while (! (f(temp) <= Y_max && f(temp) >= Y_min) && temp < X_max) {temp += resolution;} real Temp1 = temp; while (f(Temp1) <= Y_max && f(Temp1) >= Y_min && Temp1 < X_max) {Temp1 += resolution;} real Temp2 = temp; while (f(Temp2) <= Y_max && f(Temp2) >= Y_min && Temp2 > X_min) {Temp2 -= resolution;} if (Temp1 <= X_max) {draw(graph(f,Temp2,Temp1,n=2400),graph_color+linewidth(line_width),Arrows);} temp = Temp1+resolution; continue;} temp = X_max; while (f(temp) <= Y_max && f(temp) >= Y_min && temp >= X_min) {temp -= resolution;} if (f(X_max) <= Y_max && f(X_max) >= Y_min) {draw(graph(f,temp,X_max,n=2400),graph_color+linewidth(line_width),Arrows);} if (mark_lattice) {for (real temp = X_min; temp <= X_max; ++temp) {if (f(temp)%1==0 && f(temp)<=Y_max && f(temp)>=Y_min) {dot((temp,f(temp)),graph_color+linewidth(line_width*4));}}} dot((0,0)); /* Property of "PythonNut" 2011*/ [/asy] [asy] /* AUTO-GRAPH V-4 beta by PythonNut*/  /* Customizations: feel free to edit */ import math; import graph; /* x maximum and minimum */ int X_max = 8; int X_min =-8; /* y maximum and minimum */ int Y_max = 16; int Y_min = 0; /* linewidth */ real line_width = 0.75; /* graph color */ pen graph_color = heavygreen; /* special */ bool mark_lattice = true; bool show_grid = true; real X_tick_density = 2; real Y_tick_density = 1; real ratio = 1; real resolution = 0.0001; int size = 300; /* graph function */ real f(real x)    {    return x^2; /* type function to be graphed here */ }  /* The Code. Do not disturb unless you know what you are doing */  size(size);unitsize(size*ratio,size);Label l;l.p=fontsize(6); xaxis("$x$",X_min,X_max,Ticks(l,X_tick_density,(X_tick_density/2),NoZero),Arrows); yaxis("$y$",Y_min,Y_max,Ticks(l,Y_tick_density,(Y_tick_density/2),NoZero),Arrows);// if (show_grid){add(shift(X_min,Y_min)*grid(X_max-X_min,Y_max-Y_min));} real temp = X_min; while (! (f(temp) <= Y_max && f(temp) >= Y_min && temp < X_max)) {temp += resolution;} real Temp1 = temp; while (f(Temp1) <= Y_max && f(Temp1) >= Y_min && Temp1 < X_max) {Temp1 += resolution;} real Temp2 = temp; while (f(Temp2) <= Y_max && f(Temp2) >= Y_min && Temp2 > X_min) {Temp2 -= resolution;} draw(graph(f,Temp2,Temp1,n=2400),graph_color+linewidth(line_width),Arrows); temp = Temp1+resolution; while (temp <= X_max) {while (! (f(temp) <= Y_max && f(temp) >= Y_min) && temp < X_max) {temp += resolution;} real Temp1 = temp; while (f(Temp1) <= Y_max && f(Temp1) >= Y_min && Temp1 < X_max) {Temp1 += resolution;} real Temp2 = temp; while (f(Temp2) <= Y_max && f(Temp2) >= Y_min && Temp2 > X_min) {Temp2 -= resolution;} if (Temp1 <= X_max) {draw(graph(f,Temp2,Temp1,n=2400),graph_color+linewidth(line_width),Arrows);} temp = Temp1+resolution; continue;} temp = X_max; while (f(temp) <= Y_max && f(temp) >= Y_min && temp >= X_min) {temp -= resolution;} if (f(X_max) <= Y_max && f(X_max) >= Y_min) {draw(graph(f,temp,X_max,n=2400),graph_color+linewidth(line_width),Arrows);} if (mark_lattice) {for (real temp = X_min; temp <= X_max; ++temp) {if (f(temp)%1==0 && f(temp)<=Y_max && f(temp)>=Y_min) {dot((temp,f(temp)),graph_color+linewidth(line_width*4));}}} dot((0,0)); [/asy]


/*
AUTO-GRAPH V-5 alpha by PythonNut. Please do not plagerize, it's illegal :)
Customizations: feel free to edit
graph function */

real f(real x){
   return 5*sin(x); // type function to be graphed here
}

/* x maximum and minimum */
int X_max = 10; int X_min = -10;
/* y maximum and minimum */
int Y_max = 10; int Y_min = -10;

/* toggle features */
bool mark_lattice = true;
bool show_grid = true;
bool mark_solutions = true;
bool integral = true;
bool maxima_and_minima = true;
real[] Dpoints = {};

/* other options */
int size = 500;
real line_width = 0.75;
pen color = heavygreen;
pen opacity = opacity(0.9);
real X_density = 1; // density of ticks on x axis
real Y_density = 1; // density of ticks on y axis
real ratio = 1; // ratio of x and y axes
real res = 0.00005; //parsing resolution
pen fill = palegreen+linewidth(0.75)+opacity(0.8); // fill color
pen aux  = linewidth(0.75)+opacity(0.9); // auxillary color
pen[] colors = {blue,red,green,pink,yellow,purple,magenta,orange,cyan}; // color cycle
real Dres = 0.00000001; // derivative resolution
/* The Code. Do not disturb unless you know what you are doing */
import math;
import graph;
size(size);
unitsize(size*ratio,size);
Label l;
l.p=fontsize(6);
path s=(X_max,0)--(X_min,0);
pen GRAPH = opacity+color+linewidth(line_width);
real temp = X_min;
real x_at = 0;
real y_at = 0;
if (Y_max < 0){y_at=Y_max;}
if (Y_min > 0){y_at=Y_min;}
if (X_max < 0){x_at=X_max;}
if (X_min > 0){x_at=X_min;}

path graph(real f, real from, real to){
   real temp = from;
   path out;
   while (temp <= to){
      if (f(temp) == f(temp)-1){
         out = out--graph(f,temp,temp+res,n=2400);
      }
      temp += res;
   }
   return out;
}

while (! (f(temp) <= Y_max && f(temp) >= Y_min && temp < X_max)){
   temp += res;}

if (integral){
   if (f(temp-res) > Y_max){
      filldraw( (X_min,y_at)--(X_min,Y_max)--(temp,Y_max)--(temp,y_at)--cycle , fill,fill);
   }
   if (f(temp-res) < Y_min){
      filldraw( (X_min,y_at)--(X_min,Y_min)--(temp,Y_min)--(temp,y_at)--cycle , fill,fill);
   }
}

real max = temp;

while (f(max) <= Y_max && f(max) >= Y_min && max < X_max){
   max += res;}

real min = temp;

while (f(min) <= Y_max && f(min) >= Y_min && min > X_min){
   min -= res;}

path graph = graph(f,min,max);

if (integral){
   filldraw((min,y_at)--graph--(max,y_at)--cycle,fill,fill);}
draw(graph,GRAPH,Arrows);
temp = max+res;

while (temp <= X_max){
   max=temp;
   while (! (f(temp) <= Y_max && f(temp) >= Y_min) && temp < X_max){
      temp += res;}
   if (integral){
      if (f(temp-res) > Y_max){
         filldraw( (max,y_at)--(max,Y_max)--(temp,Y_max)--(temp,y_at)--cycle , fill,fill);
      }
      if (f(temp-res) < Y_min){
         filldraw( (max,y_at)--(max,Y_min)--(temp,Y_min)--(temp,y_at)--cycle , fill,fill);
      }
   }
   real max = temp;
   while (f(max) <= Y_max && f(max) >= Y_min && max < X_max){
      max += res;}
   real min = temp;
   while (f(min) <= Y_max && f(min) >= Y_min && min > X_min){
      min -= res;}
   if (max <= X_max){
      path graph = graph(f,min,max);
      if (integral){
         filldraw((min,y_at)--graph--(max,y_at)--cycle,fill,fill);}
      draw(graph,GRAPH,Arrows);}
         if (mark_solutions){
            dot(intersectionpoints(s,graph),green);
         }
   temp = max+res;
   continue;
}
   
if (integral){
   if (f(temp-res) > Y_max){
      filldraw( (temp,y_at)--(temp,Y_max)--(X_max,Y_max)--(X_max,y_at)--cycle , fill,fill);
   }
   if (f(temp-res) < Y_min){
      filldraw( (temp,y_at)--(temp,Y_min)--(X_max,Y_min)--(X_max,y_at)--cycle , fill,fill);
   }
}
temp = X_max;

while (f(temp) <= Y_max && f(temp) >= Y_min && temp >= X_min){
   temp -= res;}

if (f(X_max) <= Y_max && f(X_max) >= Y_min){
   path graph2 = graph(f,temp,X_max);
   if (integral){
      filldraw( (temp,y_at)--graph2--(X_max,y_at)--cycle,fill,fill );
   }
   draw(graph2,GRAPH,Arrows);
   if (mark_solutions){
      dot(intersectionpoints(s,graph2),green);
   }
}
   

if (mark_lattice){
   for (real temp = X_min; temp <= X_max; ++temp){
      if (f(temp)%1==0 && f(temp)<=Y_max && f(temp)>=Y_min){
         dot((temp,f(temp)),color+linewidth(line_width*4));
         
      }
   }
}

if (maxima_and_minima){
   real temp = X_min;
   real min  = X_max;
   real max  = X_min;
   
   while (temp <= X_max){
      if (f(temp) >= max){
         max=f(temp);
      }
      if (f(temp) <= min){
         min=f(temp);
      }
      temp+=res;
   }
   if (max < X_max && max > X_min){
      draw((X_min,max)--(X_max,max),aux+blue);
   }
   if (min > X_min && min < X_max){
      draw((X_min,min)--(X_max,min),aux+blue);
   }
}

if (Dpoints.length != 0){
   for (int i = 0; i < Dpoints.length; ++i){
      real Dpoint=Dpoints[i];
      real h(real x){
         real temp = -(f(Dpoint)-f(Dpoint+Dres))/Dres*x;
         temp += (f(Dpoint)-f(Dpoint+Dres))/Dres*Dpoint;
         temp += f(Dpoint);
         return temp;
      }
      draw((Dpoint,f(Dpoint))--(Dpoint,0),darkgrey+dashed);
      dot((Dpoint,f(Dpoint)),linewidth(3)+colors[i%colors.length]);
      draw(graph(h,X_min-0.2,X_max+0.2),aux+colors[i%colors.length]);
   }
}
clip((X_min-0.2,Y_min-0.2)--(X_min-0.2,Y_max+0.2)--(X_max+0.2,Y_max+0.2)--(X_max+0.2,Y_min-0.2)--cycle);

picture main;
xequals("$y$",x_at,Y_min,Y_max,Ticks(l,Y_density,(Y_density/2),NoZero),Arrows);
yequals("$x$",y_at,X_min,X_max,Ticks(l,X_density,(X_density/2),NoZero),Arrows);

if (show_grid){
   add(shift(X_min,Y_min)*grid(X_max-X_min,Y_max-Y_min));
}
add(main);
dot((x_at,Y_at));
/* Property of "PythonNut" 2011*/
 (Error making remote request. Unknown error_msg)

The code

The code is:

/* AUTO-GRAPH V-4 beta by PythonNut*/

/* Customizations: feel free to edit */
import math;
import graph;
/* x maximum and minimum */
int X_max = 10;
int X_min =-10;
/* y maximum and minimum */
int Y_max = 10;
int Y_min = -10;
/* linewidth */
real line_width = 0.75;
/* graph color */
pen graph_color = heavygreen;
/* special */
bool mark_lattice = false;
bool show_grid = true;
real X_tick_density = 1;
real Y_tick_density = 1;
real ratio = 1;
real resolution = 0.0001;
int size = 300;
/* graph function */
real f(real x)
   {
   return 2x^3-6x^2+3x-1; /* type function to be graphed here */
}

/* The Code. Do not disturb unless you know what you are doing */
bool ib(real t){ return (Y_min <= f(t) && f(t) <= Y_max); }

size(size);unitsize(size*ratio,size);Label l;l.p=fontsize(6);
xaxis("$x$",X_min,X_max,Ticks(l,X_tick_density,(X_tick_density/2),NoZero),Arrows);
yaxis("$y$",Y_min,Y_max,Ticks(l,Y_tick_density,(Y_tick_density/2),NoZero),Arrows);//
if (show_grid){add(shift(X_min,Y_min)*grid(X_max-X_min,Y_max-Y_min));}

real t, T1, T2;

for (T1 = X_min ; T1 <= X_max ; T1 += resolution){
    while (! ib(T1) && T1 <= X_max){T1 += resolution;}
    if(T1 > X_max){break;}
    T2 = T1; 
    while (  ib(T1) && T1 <= X_max){T1 += resolution;}
    T1 -= resolution;
    draw(graph(f,T2,T1,n=2400),graph_color+linewidth(line_width),Arrows);
}

if (mark_lattice){
    for (t = X_min; t <= X_max; ++t){
        if (f(t)%1==0 && ib(t)){
            dot((t,f(t)),graph_color+linewidth(line_width*4));
        }
    }
}
dot((0,0));

Autograph Micro

[asy] import contour; import graph; real f(real x) { return x^2-1.333; } draw(graph(f,-1.5,1.5),blue+linewidth(0.5),Arrows); size(75); real f(real x, real y) {return abs(x^3) + abs(y^3);} draw(contour(f,(-6,-6),(6,6), new real[] {5}),red+linewidth(1)); label("$Auto\;Graph$",(0,0.3),red); label("$micro$",(0,-0.3),red); dot((1.5,0),red); dot((0,1.5),red); dot((0,-1.5),red); dot((-1.5,0),red);[/asy] Autograph micro is a version of autograph made to fit in the AoPS classroom character limit.

[asy]int X=10,x=-10,Y=10,y=-10;
real f(real x){return 11*sin(x);}
size(300); Label l;l.p=fontsize(6);dot((0,0));
xaxis(x,X,Ticks(l,1.0,0.5));yaxis(y,Y,Ticks(l,1.0,0.5));
add(shift(x,y)*grid(X-x,Y-y));
draw(graph(f,x,X,n=2400),heavygreen+linewidth(1),Arrows);
clip((x,y)--(x,Y)--(X,Y)--(X,y)--cycle);[/asy]

Autograph implicit

[asy] import contour; import graph; real f(real x) { return x^2-1.333; } draw(graph(f,-1.5,1.5),blue+linewidth(0.5),Arrows); size(75); real f(real x, real y) {return abs(x^3) + abs(y^3);} draw(contour(f,(-6,-6),(6,6), new real[] {5}),red+linewidth(1)); label("$Auto\;Graph$",(0,0.3),red); label("$implict$",(0,-0.3),red); dot((1.5,0),red); dot((0,1.5),red); dot((0,-1.5),red); dot((-1.5,0),red);[/asy] Autograph implicit Is a version of Autograph used to graph implicit functions such as $x^{13}+5\cdot x^5\cdot y^3+x\cdot y^5+y^5=2.5$ [asy] /* AUTO-GRAPH implict by PythonNut*/ /* Customizations: feel free to edit */ import math; import graph; import contour; /* x maximum and minimum */ int X_max = 10; int X_min =-10; /* y maximum and minimum */ int Y_max = 10; int Y_min = -10; /* graph color */ pen graph_line = heavygreen+linewidth(0.75); /* special */ bool show_grid = true; real X_tick_density = 1; real Y_tick_density = 1; real ratio = 1; int size = 300; real value = 2.5; /* value to set to */ /* graph function */ real f(real x, real y) { return x^13+5*x^5*y^3+x*y^5+y^5; /* type function to be graphed here */ } /* The Code. Do not disturb unless you know what you are doing */ size(size);unitsize(size*ratio,size);Label l;l.p=fontsize(6); xaxis("$x$",X_min,X_max,Ticks(l,X_tick_density,(X_tick_density/2),NoZero),Arrows); yaxis("$y$",Y_min,Y_max,Ticks(l,Y_tick_density,(Y_tick_density/2),NoZero),Arrows);// if (show_grid){add(shift(X_min,Y_min)*grid(X_max-X_min,Y_max-Y_min));} draw(contour(f,(X_min,Y_min),(X_max,Y_max), new real[] {value}),graph_line);[/asy] The code is

[asy]
/* AUTO-GRAPH implict by PythonNut*/
/* Customizations: feel free to edit */
import math;
import graph;
import contour;
/* x maximum and minimum */
int X_max = 10;
int X_min =-10;
/* y maximum and minimum */
int Y_max = 10;
int Y_min = -10;
/* graph color */
pen graph_line = heavygreen+linewidth(0.75);
/* special */
bool show_grid = true;
real X_tick_density = 1;
real Y_tick_density = 1;
real ratio = 1;
int size = 300;
real value = 2.5; /* value to set to */
/* graph function */
real f(real x, real y)
{
return x^13+5*x^5*y^3+x*y^5+y^5; /* type function to be graphed here */
}
/* The Code. Do not disturb unless you know what you are doing */
size(size);unitsize(size*ratio,size);Label l;l.p=fontsize(6);
xaxis("$x$",X_min,X_max,Ticks(l,X_tick_density,(X_tick_density/2),NoZero),Arrows);
yaxis("$y$",Y_min,Y_max,Ticks(l,Y_tick_density,(Y_tick_density/2),NoZero),Arrows);//
if (show_grid){add(shift(X_min,Y_min)*grid(X_max-X_min,Y_max-Y_min));}
draw(contour(f,(X_min,Y_min),(X_max,Y_max), new real[] {value}),graph_line);[/asy]