physics: a module for Asymptote

Jaime E. Villate
University of Porto, Portugal
(December 9, 2023)

1 Introduction

This manual describes a module called physics which adds some functionalities to Asymptote specially aimed at making the kind of illustrations that are found in Physics textbooks, as the ones shown in figure 1

Some images made with Asymptote and the module physics. Some images made with Asymptote and the module physics.
Figure 1: Some images made with Asymptote and the module physics.

2 Usage

To use this module you must have a copy of its source file physics.asy and import it, as in the following two examples, which draw the two images shown in figure 1.

Example 1

import physics;
picture c;
cylinder(c,3,50,0.6,hsv(36,0.4,1));
add(shift(-50,0)*rotate(-90)*c);
draw(shift(-10,0)*scale(30,50)*arc((0,0),1,0,360),blue,
     ArcArrow(HookHead,2,position=1.5));
label("C",(-10,50),N);
filldraw(scale(15,25)*unitcircle,hsv(36,0.3,0.9),black+0.3);
for(int a=90; a<280; a+=20)
  fieldline(scale(12,20)*dir(a)--scale(12,20)*dir(a)+(10,0),
            rgb(0.4,0,0.4)+0.4,0.6);
filldraw(shift(10,0)*scale(15,25)*unitcircle,hsv(36,0.3,0.9),
         black+0.3);
add(shift(10,0)*rotate(-90)*c);
label("$\vec{E}$",(5,-25),SW);
vector("$I$",(-45,-7),25,0,S,red);
vector("$I$",(20,-7),25,0,S,red);
Example 2

import physics;
pair[]
  z={(0,0),(60,0),(120,0),(0,70),(60,70),(120,70),(120,30)};
draw(z[0]--z[2]^^z[3]--z[4]);
resistor("3.2 k$\Omega$",z[0],z[3],E);
capacitor("1.2 F",z[1],z[4],E);
resistor("230 $\Omega$",z[5],z[6],E);
emfn("9 V",z[6],z[2],E);
openswitch(z[4],z[5]);
vgaugep("$\Delta V$",(-10,10),(-10,60));

3 Functions reference

3.1 Drawing

void vector(picture pic=currentpicture, Label s="",
            explicit pair orig, real mag, real ang,
            align align=NoAlign, pen p=currentpen)
void vector(picture pic=currentpicture, Label s="",
            explicit pair orig, explicit pair dest,
            align align=NoAlign, pen p=currentpen)

Draws a vector using pen p, starting at point orig and ending at point dest or with magnitude mag and in the direction ang, starting from point orig. Label s will be typed aligned according to align. Unlike draw, the current pen will be used for type the label, rather than the vector pen p.

void vectorin(picture pic=currentpicture, Label s="",
              explicit pair orig, align align=NoAlign,
              pen p=currentpen)

Draws a vector going into the figure’s plane, using pen p, at point orig. Label s will be typed aligned according to align, using the current pen rather than the vector pen p.

void vectorout(picture pic=currentpicture, Label s="",
               explicit pair orig, align align=NoAlign,
               pen p=currentpen)

Draws a vector coming out of the figure’s plane, using pen p, at point orig. Label s will be typed aligned according to align, using the current pen rather than the vector pen p.

void fieldline(picture pic=currentpicture, Label L="", path g,
               align align=NoAlign, pen p=currentpen,
               real pos=0.5)

Draws the curve defined by the path g, using pen p, and places and arrow at position pos relative to its length; the default for pos is 0.5, which means half of the total length. A label L can be added to the curve, from its center, in the direction align.

pen darkerpen (pen p=currentpen, real f)

Returns a pen with the same hue and saturation as pen p, but with value t times the value of p.

3.2 3D isometric into 2D

pair isometric(real x, real y, real z)

Projects the point with coordinates x, y and z onto the xy plane, using isometric projection.

guide boxy(pair o=(0,0),real dx, real dz)
guide boxz(pair o=(0,0),real dx, real dy)
guide boxx(pair o=(0,0),real dy, real dz)

Returns a path describing a rectangular box projected into one of the 3 Cartesian planes, using isometric projection.

3.3 Shading

void centershade(picture pic=currentpicture, path p,
                 bool stroke=false, pen in, pen out,
                 pen drawpen=currentpen)

Creates a circular shading pattern inside the path p, starting with pen in and ending with pen out. I stroke is true, the boundary of p is drawn using pen drawpen.

void sphere(picture pic=currentpicture, real r=1, pair c=(0,0),
            pen p=currentpen, pen q=darkerpen(p,0.25))

Draws a shaded sphere of radius r, centered at t, whose darker part will have the color of pen q and brightest spot (to the right and above the center) with the color of pen p.

void cylinder(picture pic=currentpicture, real r=1, real h=2,
         real f=0.3, pair c=(0,0), pen p=currentpen,
         pen q=darkerpen(p,0.4))

Draws a shaded cylinder with circular base of radius r and height (in the vertical direction) h; the circular base will be centered a c. The shading will have brightest color given by pen p and darkest color given by pen q. The factor f, between 0 and 1, is the foreshortening of the cylinder circular caps.

void hollow_cylinder(picture pic=currentpicture, real r=1,
                     real h=2, real f=0.3, pair c=(0,0),
                     pen p=currentpen, pen q=darkerpen(p,0.4))

Draws a shaded hollow cylinder with circular base of radius r and height (in the vertical direction) h; the circular base will be centered a c. The shading will have brightest color given by pen p and darkest color given by pen q. The factor f, between 0 and 1, is the foreshortening of the cylinder circular ends.

void ring(picture pic=currentpicture, real r1=1, real r2=0.5,
          real h=2, real f=0.3, pair c=(0,0), pen p=currentpen)

Draws a shaded cylindrical ring with outer radius r1, inner radius r2 and height (in the vertical direction) h; the circular base will be centered a c. The factor f, between 0 and 1, is the foreshortening of the cylinder circular caps.

void torus(picture pic=currentpicture, real r1=1, real r2=0.5,
           pair c=(0,0), pen p=currentpen)

Draws a shaded torus with inner radius r1 and outer radius r2, centered at point c.

void torus_arc(picture pic=currentpicture, real r1, real r2,
           real ang1, real ang2, pair c=(0,0), pen p=currentpen)

Draws a shaded torus arc with inner radius r1 and outer radius r2, centered at point c and extending from angle ang1 to angle ang2.

void toroidal_cylinder(picture pic=currentpicture, real r1,
                       real r2, real ang1, real ang2,
                       pair c=(0,0), pen p=currentpen,
                       bool cap=true)

3.4 Circuit diagrams

void resistor(picture pic=currentpicture, Label s="",
              explicit pair orig, explicit pair dest, pair d=N,
              pen p=currentpen)
void resistor(picture pic=currentpicture, Label s="",
              explicit pair orig, real mag, real ang, pair d=N,
              pen p=currentpen)

Draws the symbol for a resistor, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. If label s is given, it will be added near the middle, in the direction d.

void inductor(picture pic=currentpicture, Label s="",
              explicit pair orig, explicit pair dest, pair d=N,
              pen p=currentpen)
void inductor(picture pic=currentpicture, Label s="",
              explicit pair orig, real mag, real ang, pair d=N,
              pen p=currentpen)

Draws the symbol for an inductor, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. If label s is given, it will be added near the middle, in the direction d.

void capacitor(picture pic=currentpicture, Label s="",
               explicit pair orig, explicit pair dest, pair d=N,
               pen p=currentpen)
void capacitor(picture pic=currentpicture, Label s="",
               explicit pair orig, real mag, real ang, pair d=N,
               pen p=currentpen)

Draws the symbol for a capacitor, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. If label s is given, it will be added near the middle, in the direction d.

void diode(picture pic=currentpicture, Label s="",
           explicit pair orig, explicit pair dest, pair d=N,
           pen p=currentpen)
void diode(picture pic=currentpicture, Label s="",
           explicit pair orig, real mag, real ang, pair d=N,
           pen p=currentpen)

Draws the symbol for a diode, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. If label s is given, it will be added near the middle, in the direction d.

void LED(picture pic=currentpicture, Label s="",
         explicit pair orig, explicit pair dest, pair d=N,
         pen p=currentpen)
void LED(picture pic=currentpicture, Label s="",
         explicit pair orig, real mag, real ang, pair d=N,
         pen p=currentpen)

Draws the symbol for a LED, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. If label s is given, it will be added near the middle, in the direction d.

void openswitch(picture pic=currentpicture, Label s="",
                explicit pair orig, explicit pair dest,
                pair d=N, pen p=currentpen)
void openswitch(picture pic=currentpicture, Label s="",
                explicit pair orig, real mag, real ang,
                pair d=N, pen p=currentpen)

Draws the symbol for an open switch, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. If label s is given, it will be added near the middle, in the direction d.

void closedswitch(picture pic=currentpicture, Label s="",
                  explicit pair orig, explicit pair dest,
                  pair d=N, pen p=currentpen)
void closedswitch(picture pic=currentpicture, Label s="",
                  explicit pair orig, real mag, real ang,
                  pair d=N, pen p=currentpen)

Draws the symbol for a closed switch, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. If label s is given, it will be added near the middle, in the direction d.

void ground(picture pic=currentpicture, explicit pair orig,
            explicit pair dest, pen p=currentpen)
void ground(picture pic=currentpicture, explicit pair orig,
            real mag, real ang, pen p=currentpen)

Draws the symbol for a ground connection, using pen p, from point orig to point dest, or from point orig with length mag in the direction of ang.

void impedance(picture pic=currentpicture, Label s="",
               explicit pair orig, explicit pair dest, pair d=N,
               pen p=currentpen)
void impedance(picture pic=currentpicture, Label s="",
               explicit pair orig, real mag, real ang, pair d=N,
               pen p=currentpen)

Draws the symbol for a generic impedance, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. If label s is given, it will be added near the middle, in the direction d.

void emfp(picture pic=currentpicture, Label s="",
          explicit pair orig, explicit pair dest, pair d=N,
          pen p=currentpen)
void emfp(picture pic=currentpicture, Label s="",
          explicit pair orig, real mag, real ang, pair d=N,
          pen p=currentpen)

Draws the symbol for an ideal voltage source, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. The positive electrode will be closer to the initial point and the negative electrode closer to the final point. If label s is given, it will be added near the middle, in the direction d.

void emfn(picture pic=currentpicture, Label s="",
          explicit pair orig, explicit pair dest, pair d=N,
          pen p=currentpen)
void emfn(picture pic=currentpicture, Label s="",
          explicit pair orig, real mag, real ang, pair d=N,
          pen p=currentpen)

Draws the symbol for an ideal voltage source, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. The negative electrode will be closer to the initial point and the positive electrode closer to the final point. If label s is given, it will be added near the middle, in the direction d.

void vsourcep(picture pic=currentpicture, Label s="$V$",
              explicit pair orig, explicit pair dest,
              pen p=currentpen)
void vsourcep(picture pic=currentpicture, Label s="$V$",
              explicit pair orig, real mag, real ang,
              pen p=currentpen)

Draws a symbol for a voltage source, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. The symbol consists of a circle with labels inside. The positive electrode will be closer to the initial point and the negative electrode closer to the final point.

void vsourcen(picture pic=currentpicture, Label s="$V$",
              explicit pair orig, explicit pair dest,
              pen p=currentpen)
void vsourcen(picture pic=currentpicture, Label s="$V$",
              explicit pair orig, real mag, real ang,
              pen p=currentpen)

Draws a symbol for a voltage source, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. The symbol consists of a circle with labels inside. The positive electrode will be closer to the initial point and the negative electrode closer to the final point.

void isource(picture pic=currentpicture, Label s="",
             explicit pair orig, explicit pair dest, pair d=N,
             pen p=currentpen)
void isource(picture pic=currentpicture, Label s="",
             explicit pair orig, real mag, real ang, pair d=N,
             pen p=currentpen)

Draws the symbol for an ideal current source, using pen p, from point orig to point dest, or from point orig and length mag in the direction of ang. The current points from the initial point to the final point. If label s is given, it will be added near the middle, in the direction d.

void acsource(picture pic=currentpicture, Label s="",
              explicit pair orig, explicit pair dest, pair d=N,
              pen p=currentpen)
void acsource(picture pic=currentpicture, Label s="",
              explicit pair orig, real mag, real ang, pair d=N,
              pen p=currentpen)

Draws the symbol for an alternating voltage source, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. If label s is given, it will be added near it, in the direction d.

void ammeter(picture pic=currentpicture, explicit pair orig,
             explicit pair dest, pen p=currentpen)
void ammeter(picture pic=currentpicture, explicit pair orig,
             real mag, real ang, pen p=currentpen)

Draws the symbol for an ammeter, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang.

void voltmeter(picture pic=currentpicture, explicit pair orig,
               explicit pair dest, pen p=currentpen)
void voltmeter(picture pic=currentpicture, explicit pair orig,
               real mag, real ang, pen p=currentpen)

Draws the symbol for a voltmeter, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang.

void meter(picture pic=currentpicture, Label s,
           explicit pair orig, explicit pair dest,
           pen p=currentpen)
void meter(picture pic=currentpicture, Label s,
           explicit pair orig, real mag, real ang,
           pen p=currentpen)

Draws the symbol for a meter, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. The meter will be a circle with label s inside it.

void vgaugep(picture pic=currentpicture, Label s="$V$",
             explicit pair orig, real mag, real ang,
             pen p=currentpen)
void vgaugep(picture pic=currentpicture, Label s="$V$",
             explicit pair orig, explicit pair dest,
             pen p=currentpen)

Draws a symbol for a voltage difference, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. Label s will be displayed in between two rounded arrows pointing to the points. The first point is label with a positive sign and the second one with a negative sign.

void vgaugen(picture pic=currentpicture, Label s="$V$",
             explicit pair orig, explicit pair dest,
             pen p=currentpen)
void vgaugen(picture pic=currentpicture, Label s="$V$",
             explicit pair orig, real mag, real ang,
             pen p=currentpen)

Draws a symbol for a voltage difference, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. Label s will be displayed in between two rounded arrows pointing to the points. The first point is label with a negative sign and the second one with a positive sign.

void acgauge(picture pic=currentpicture, Label s="$V$",
             explicit pair orig, real mag, real ang,
             pen p=currentpen)
void acgauge(picture pic=currentpicture, Label s="$V$",
             explicit pair orig, explicit pair dest,
             pen p=currentpen)

Draws the symbol for a resistor, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. If label s is given, it will be added near the middle, in the direction d.

void cccc(picture pic=currentpicture, Label s="", path[] g,
          explicit pair orig, real mag, real ang, pair d,
          pen p=currentpen)

Draws the symbol for a generic device define by path g, using pen p, between points orig and dest, or from point orig and length mag in the direction of ang. If label s is given, it will be added near the middle, in the direction d.