graph3 ¶This module implements three-dimensional versions of the
functions in graph.asy.
To draw an x axis in three dimensions, use the routine
void xaxis3(picture pic=currentpicture, Label L="", axis axis=YZZero,
real xmin=-infinity, real xmax=infinity, pen p=currentpen,
ticks3 ticks=NoTicks3, arrowbar3 arrow=None,
margin3 margin=NoMargin3, bool above=false,
projection P=currentprojection);
Analogous routines yaxis and zaxis can be used to draw
y and z axes in three dimensions.
There is also a routine for drawing all three axis:
void axes3(picture pic=currentpicture,
Label xlabel="", Label ylabel="", Label zlabel="",
bool extend=false,
triple min=(-infinity,-infinity,-infinity),
triple max=(infinity,infinity,infinity),
pen p=currentpen, pen py=p, pen pz=p,
arrowbar3 arrow=None, margin3 margin=NoMargin3,
projection P=currentprojection);
The predefined three-dimensional axis types are
axis YZEquals(real y, real z, triple align=O, bool extend=false); axis XZEquals(real x, real z, triple align=O, bool extend=false); axis XYEquals(real x, real y, triple align=O, bool extend=false); axis YZZero(triple align=O, bool extend=false); axis XZZero(triple align=O, bool extend=false); axis XYZero(triple align=O, bool extend=false); axis Bounds(int type=Both, int type2=Both, triple align=O, bool extend=false);
The optional align parameter to these routines can be used to
specify the default axis and tick label alignments. The Bounds
axis accepts two type parameters, each of which must be one of
Min, Max, or Both. These parameters specify which
of the four possible three-dimensional bounding box edges should be drawn.
The three-dimensional tick options are NoTicks3, InTicks,
OutTicks, and InOutTicks. These specify the tick
directions for the Bounds axis type; other axis types inherit
the direction that would be used for the Bounds(Min,Min) axis.
Here is an example of a helix and bounding box axes with ticks and axis labels, using orthographic projection:
import graph3;
size(0,200);
size3(200,IgnoreAspect);
currentprojection=orthographic(4,6,3);
real x(real t) {return cos(2pi*t);}
real y(real t) {return sin(2pi*t);}
real z(real t) {return t;}
path3 p=graph(x,y,z,0,2.7,operator ..);
draw(p,Arrow3);
scale(true);
xaxis3(XZ()*"$x$",Bounds,red,InTicks(Label,2,2));
yaxis3(YZ()*"$y$",Bounds,red,InTicks(beginlabel=false,Label,2,2));
zaxis3(XZ()*"$z$",Bounds,red,InTicks);
The next example illustrates three-dimensional x, y, and z axes, without autoscaling of the axis limits:
import graph3;
size(0,200);
size3(200,IgnoreAspect);
currentprojection=perspective(dir(75,20));
scale(Linear,Linear,Log);
xaxis3("$x$",0,1,red,OutTicks(2,2));
yaxis3("$y$",0,1,red,OutTicks(2,2));
zaxis3("$z$",1,30,red,OutTicks(beginlabel=false));
One can also place ticks along a general three-dimensional axis:
import graph3;
size(0,100);
path3 g=yscale3(2)*unitcircle3;
currentprojection=perspective(10,10,10);
axis(Label("C",position=0,align=15X),g,InTicks(endlabel=false,8,end=false),
ticklocate(0,360,new real(real v) {
path3 h=O--max(abs(max(g)),abs(min(g)))*dir(90,v);
return intersect(g,h)[0];},
new triple(real t) {return cross(dir(g,t),Z);}));
Surface plots of matrices and functions over the region
box(a,b) in the XY plane are also implemented.
A surface can be constructed from a matrix f of heights, at evenly
spaced points over box(a,b) or at the points (x[i],y[j]),
or from a matrix of points:
surface surface(real[][] f, pair a, pair b, bool[][] cond={});
surface surface(real[][] f, pair a, pair b, splinetype xsplinetype,
splinetype ysplinetype=xsplinetype, bool[][] cond={});
surface surface(real[][] f, real[] x, real[] y,
splinetype xsplinetype=Spline,
splinetype ysplinetype=xsplinetype,
bool[][] cond={});
surface surface(triple[][] f, bool[][] cond={});
The graph of a real function f(x,y) over box(a,b) is
constructed with
surface surface(real f(pair z), pair a, pair b, int nx=nmesh, int ny=nx,
bool cond(pair z)=null);
surface surface(real f(pair z), pair a, pair b, int nx=nmesh, int ny=nx,
splinetype xsplinetype, splinetype ysplinetype=xsplinetype,
bool cond(pair z)=null);
On a logarithmic axis (see automatic scaling), the points at which
f is sampled are evenly spaced along the axis, as are the entries
of a matrix over box(a,b).
A parametric surface for a function f(u,v) over the parameter
space box(a,b), as illustrated in the example
parametricsurface.asy,
is constructed with
surface surface(triple f(pair z), pair a, pair b, int nu=nmesh, int nv=nu,
bool cond(pair z)=null);
surface surface(triple f(pair z), pair a, pair b, int nu=nmesh, int nv=nu,
splinetype[] usplinetype, splinetype[] vsplinetype=Spline,
bool cond(pair z)=null);
The parameters, in contrast, are sampled at evenly spaced values over
box(a,b) even in a picture with logarithmic axes. To sample them
at other values, specify the values in the arrays u and v:
surface surface(triple f(pair z), real[] u, real[] v,
bool cond(pair z)=null);
surface surface(triple f(pair z), real[] u, real[] v,
splinetype[] usplinetype, splinetype[] vsplinetype=Spline,
bool cond(pair z)=null);
A surface constructor with no splinetype parameter interpolates linearly;
the others interpolate with the splinetypes given, such as Spline for a smooth
surface.
The boolean array or function cond can be used to control which
surface mesh cells are actually drawn (by default all mesh cells are
drawn).
A surface constructed with these routines may be colored at draw time as
a function of position, parametric coordinates, or mesh cell
(see Surface coloring); the parametric coordinates of such a surface
are described in Surface parametrization.
One can also construct the surface generated by rotating a path g
between angle1 to angle2 (in degrees) sampled n
times about the line c--c+axis:
surface surface(triple c, path3 g, triple axis, int n=nslice,
real angle1=0, real angle2=360, pen color(int i, real j)=null);
The optional argument color(int i, real j) can be used to override the
surface color at the point obtained by rotating vertex i by
angle j (in degrees).
A surface constructed with any of the above routines is
structured: its patches form a rectangular array of nu by
nv mesh cells, and each point of the surface has parametric
coordinates (u,v). A surface assembled in any other way, from
paths or from individual patches, is unstructured and has no
parametric coordinates. Unstructured surfaces include unitsphere and the
other predefined surfaces of module three, as well as the implicit
surfaces produced by the modules contour3 and
smoothcontour3.
The point and a normal vector (not normalized) of a structured surface
s at the parametric coordinates (u,v) are returned by
triple s.paramPoint(real u, real v); triple s.paramNormal(real u, real v);
These functions abort with an error if s is unstructured; calling
one of them is therefore a simple test of whether a given surface is
structured. A structured surface remains structured, with the same
parametric coordinates, when a transform3 is applied to it.
The domain of the parametric coordinates depends on how the surface was constructed:
triple f(pair z) over box(a,b), whether or not a splinetype
is specified, (u,v) is the argument z of f: u
ranges from a.x to b.x and v ranges from a.y
to b.y;
real f(pair z) or of a real
matrix f over box(a,b), (u,v) are the x and
y coordinates of the point of the surface, so that they also range
over box(a,b). On a logarithmic axis (see automatic scaling)
the coordinate of a point is the logarithm of the value plotted there,
and this is the parametric coordinate as well: for x plotted from 1
to 100, u ranges from 0 to 2;
u and v, the
parametric coordinates are mesh coordinates: u ranges from
0 to nu and v ranges from 0 to nv,
where nu and nv are the number of mesh cells in each
direction (one less than the number of samples). The sample with
indices i and j has parametric coordinates (i,j);
radians(angle1) to radians(angle2), and v is the
time along the path g, ranging from 0 to length(g).
A structured surface can be given a different domain with
void s.domain(pair a, pair b);
after which the parametric coordinates range over box(a,b), with
the mesh taken to be uniform: the sample with indices i and
j then has the parametric coordinates returned by
pair s.paramCoords(real i, real j);
For example, if the entries of a matrix of triples are the values of a
function on an evenly spaced grid over the unit square, the surface
constructed from the matrix can be given the coordinates of that function
with s.domain((0,0),(1,1)).
Parametric coordinates must normally lie within this domain. The
exception is a surface that closes on itself: if s.ucyclic()
is true, then u may be any real number and is reduced
modulo the length of its domain, and likewise for v if
s.vcyclic() is true.
Whether a surface is cyclic in a given direction depends on how it was
constructed:
angle2-angle1 is a multiple of 360 degrees (as it is by default).
It is cyclic in v if the path g is cyclic;
Spline chooses a periodic splinetype
by itself, but only if the samples also approach the two edges with
nearly the same slope, so that the surface closes without a kink;
periodic.
It is an error to evaluate the surface outside its domain in a
direction in which it is not cyclic, or at a point belonging only to
mesh cells that were omitted by cond.
Surface lighting is illustrated in the example files
parametricsurface.asy and sinc.asy.
Lighting can be disabled by setting light=nolight, as in this example
of a Gaussian surface:
import graph3;
size(200,0);
currentprojection=perspective(10,8,4);
real f(pair z) {return 0.5+exp(-abs(z)^2);}
draw((-1,-1,0)--(1,-1,0)--(1,1,0)--(-1,1,0)--cycle);
draw(arc(0.12Z,0.2,90,60,90,25),ArcArrow3);
surface s=surface(f,(-1,-1),(1,1),nx=5,Spline);
xaxis3(Label("$x$"),red,Arrow3);
yaxis3(Label("$y$"),red,Arrow3);
zaxis3(XYZero(extend=true),red,Arrow3);
draw(s,lightgray,meshpen=black+thick(),nolight,render(merge=true));
label("$O$",O,-Z+Y,red);
A mesh can be drawn without surface filling by specifying nullpen
for the surfacepen.
A vector field of nu\timesnv arrows on a
parametric surface f over box(a,b) can be drawn with the routine
picture vectorfield(path3 vector(pair v), triple f(pair z), pair a, pair b,
int nu=nmesh, int nv=nu, bool truesize=false,
real maxlength=truesize ? 0 : maxlength(f,a,b,nu,nv),
bool cond(pair z)=null, pen p=currentpen,
arrowbar3 arrow=Arrow3, margin3 margin=PenMargin3)
as illustrated in the examples vectorfield3.asy and
vectorfieldsphere.asy.