Drawing a Mixtillinear Circle With Asymptote
a cubic spline resolved into a fixed path. The implicit initializer for paths is nullpath.
For case, the routine circle(pair c, existent r), which returns a Bezier curve approximating a circle of radius r centered on c, is based on unitcircle (meet unitcircle):
path circle(pair c, existent r) { render shift(c)*calibration(r)*unitcircle; } If high accurateness is needed, a true circle may be produced with the routine Circle defined in the module graph:
import graph; path Circle(pair c, real r, int n=nCircle);
A circular arc consistent with circle centered on c with radius r from angle1 to angle2 degrees, cartoon counterclockwise if angle2 >= angle1, can exist synthetic with
path arc(pair c, real r, real angle1, real angle2);
1 may besides specify the direction explicitly:
path arc(pair c, real r, real angle1, existent angle2, bool direction);
Here the direction tin be specified equally CCW (counter-clockwise) or CW (clockwise). For convenience, an arc centered at c from pair z1 to z2 (assuming |z2-c|=|z1-c|) in the may too be constructed with
path arc(pair c, explicit pair z1, explicit pair z2, bool management=CCW)
If high accuracy is needed, true arcs may be produced with routines in the module graph that produce Bezier curves with n control points:
import graph; path Arc(pair c, real r, real angle1, real angle2, bool direction, int north=nCircle); path Arc(pair c, real r, existent angle1, real angle2, int n=nCircle); path Arc(pair c, explicit pair z1, explicit pair z2, bool direction=CCW, int n=nCircle);
An ellipse can exist drawn with the routine
path ellipse(pair c, real a, real b) { return shift(c)*scale(a,b)*unitcircle; } A brace can exist synthetic between pairs a and b with
path brace(pair a, pair b, real amplitude=bracedefaultratio*length(b-a));
This instance illustrates the use of all five guide connectors discussed in Tutorial and Bezier curves:
size(300,0); pair[] z=new pair[10]; z[0]=(0,100); z[ane]=(fifty,0); z[2]=(180,0); for(int n=3; n <= nine; ++n) z[due north]=z[n-3]+(200,0); path p=z[0]..z[1]---z[2]::{upwards}z[iii] &z[3]..z[4]--z[5]::{upward}z[6] &z[6]::z[7]---z[eight]..{up}z[9]; draw(p,grey+linewidth(4mm)); dot(z);
Here are some useful functions for paths:
-
int length(path p); -
This is the number of (linear or cubic) segments in path
p. Ifpis cyclic, this is the same equally the number of nodes inp. -
int size(path p); -
This is the number of nodes in the path
p. Ifpis circadian, this is the same aslength(p). -
bool cyclic(path p); -
returns
trueiff pathpis circadian. -
bool straight(path p, int i); -
returns
trueiff the segment of pathpbetween nodeiand nodei+1is straight. -
bool piecewisestraight(path p) -
returns
trueiff the pathpis piecewise straight. -
pair bespeak(path p, int t); -
If
pis circadian, return the coordinates of nodetmodlength(p). Otherwise, return the coordinates of nodet, unlesst< 0 (in which casebetoken(0)is returned) ort>length(p)(in which examplepoint(length(p))is returned). -
pair point(path p, real t); -
This returns the coordinates of the bespeak betwixt node
floor(t)andfloor(t)+1respective to the cubic spline parametert-flooring(t)(see Bezier curves). Iftlies exterior the range [0,length(p)], information technology is first reduced modulolength(p)in the example wherepis cyclic or else converted to the respective endpoint ofp. -
pair dir(path p, int t, int sign=0, bool normalize=true); -
If
sign < 0, return the direction (as a pair) of the incoming tangent to pathpat nodet; ifsign > 0, return the management of the approachable tangent. Ifsign=0, the mean of these ii directions is returned. -
pair dir(path p, real t, bool normalize=true); -
returns the direction of the tangent to path
pat the point between nodefloor(t)andfloor(t)+1respective to the cubic spline parametert-floor(t)(come across Bezier curves). -
pair dir(path p) -
returns dir(p,length(p)).
-
pair dir(path p, path q) -
returns unit(dir(p)+dir(q)).
-
pair accel(path p, int t, int sign=0); -
If
sign < 0, render the acceleration of the incoming pathpat nodet; ifsign > 0, return the dispatch of the outgoing path. Ifsign=0, the hateful of these two accelerations is returned. -
pair accel(path p, real t); -
returns the acceleration of the path
pat the pointt. -
real radius(path p, real t); -
returns the radius of curvature of the path
pat the pointt. -
pair precontrol(path p, int t); -
returns the precontrol point of
pat nodet. -
pair precontrol(path p, existent t); -
returns the effective precontrol point of
pat parametert. -
pair postcontrol(path p, int t); -
returns the postcontrol point of
pat nodet. -
pair postcontrol(path p, real t); -
returns the effective postcontrol signal of
pat parametert. -
real arclength(path p); -
returns the length (in user coordinates) of the piecewise linear or cubic curve that path
prepresents. -
real arctime(path p, existent Fifty); -
returns the path "time", a real number betwixt 0 and the length of the path in the sense of
point(path p, real t), at which the cumulative arclength (measured from the outset of the path) equalsL. -
pair arcpoint(path p, real L); -
returns
point(p,arctime(p,L)). -
real dirtime(path p, pair z); -
returns the first "time", a real number betwixt 0 and the length of the path in the sense of
point(path, real), at which the tangent to the path has the direction of pairz, or -1 if this never happens. -
existent reltime(path p, real l); -
returns the time on path
pat the relative fractionlof its arclength. -
pair relpoint(path p, real l); -
returns the signal on path
pat the relative fractionlof its arclength. -
pair midpoint(path p); -
returns the point on path
pat half of its arclength. -
path reverse(path p); -
returns a path running backwards along
p. -
path subpath(path p, int a, int b); -
returns the subpath of
prunning from nodeato nodeb. Ifa>b, the direction of the subpath is reversed. -
path subpath(path p, existent a, real b); -
returns the subpath of
prunning from path timeato path fourth dimensionb, in the sense ofsignal(path, real). Ifa>b, the direction of the subpath is reversed. -
real[] intersect(path p, path q, real fuzz=-i); -
If
pandqhave at least one intersection point, return a real assortment of length 2 containing the times representing the respective path times alongpandq, in the sense ofpoint(path, real), for 1 such intersection signal (equally chosen by the algorithm described on page 137 ofThe MetaFontbook). The computations are performed to the absolute error specified byfuzz, or iffuzz < 0, to car precision. If the paths practise non intersect, return a existent array of length 0. -
real[][] intersections(path p, path q, real fuzz=-ane); -
Return all (unless there are infinitely many) intersection times of paths
pandqas a sorted array of real arrays of length 2 (see sort). The computations are performed to the absolute error specified byfuzz, or iffuzz < 0, to machine precision. -
real[] intersections(path p, explicit pair a, explicit pair b, real fuzz=-1); -
Return all (unless there are infinitely many) intersection times of path
pwith the (infinite) line through pointsaandbevery bit a sorted array. The intersections returned are guaranteed to be correct to inside the absolute mistake specified byfuzz, or iffuzz < 0, to machine precision. -
real[] times(path p, real x) -
returns all intersection times of path
pwith the vertical line through(x,0). -
real[] times(path p, explicit pair z) -
returns all intersection times of path
pwith the horizontal line through(0,z.y). -
real[] mintimes(path p) -
returns an assortment of length 2 containing times at which path
preaches its minimal horizontal and vertical extents, respectively. -
real[] maxtimes(path p) -
returns an assortment of length 2 containing times at which path
preaches its maximal horizontal and vertical extents, respectively. -
pair intersectionpoint(path p, path q, real fuzz=-1); -
returns the intersection point
betoken(p,intersect(p,q,fuzz)[0]). -
pair[] intersectionpoints(path p, path q, real fuzz=-1); -
returns an array containing all intersection points of the paths
pandq. -
pair extension(pair P, pair Q, pair p, pair q); -
returns the intersection point of the extensions of the line segments
P--Qandp--q, or if the lines are parallel,(infinity,infinity). -
piece cut(path p, path knife, int north); -
returns the portions of path
pbefore and after thenth intersection ofpwith pathpocketknifeas a structurepiece(if no intersection exist is constitute, the entire path is considered to exist 'before' the intersection):struct slice { path earlier,later on; }The argument
nis treated as modulo the number of intersections. -
slice firstcut(path p, path pocketknife); -
equivalent to
cutting(p,knife,0);Note thatfirstcut.afterwardsplays the role of theMetaPost cutbeforecommand. -
piece lastcut(path p, path knife); -
equivalent to
cut(p,knife,-1);Note thatlastcut.beforeplays the office of theMetaPost cutaftercommand. -
path buildcycle(... path[] p); -
This returns the path surrounding a region divisional past a list of ii or more than consecutively intersecting paths, following the behaviour of the
MetaPost buildcyclecommand. -
pair min(path p); -
returns the pair (left,bottom) for the path bounding box of path
p. -
pair max(path p); -
returns the pair (right,meridian) for the path bounding box of path
p. -
int windingnumber(path p, pair z); -
returns the winding number of the cyclic path
prelative to the pointz. The winding number is positive if the path encircleszin the counterclockwise direction. Ifzlies onpthe abidingundefined(divers to be the largest odd integer) is returned. -
bool interior(int windingnumber, pen fillrule) -
returns true if
windingnumbercorresponds to an interior bespeak according tofillrule. -
bool inside(path p, pair z, pen fillrule=currentpen); -
returns
trueiff the betokenzlies inside or on the border of the region bounded by the circadian pathpaccording to the fill rulefillrule(see fillrule). -
int inside(path p, path q, pen fillrule=currentpen); -
returns
1if the cyclic pathpstrictly containsqaccording to the make full rulefillrule(encounter fillrule),-1if the circadian pathqstrictly containsp, and0otherwise. -
pair inside(path p, pen fillrule=currentpen); -
returns an capricious point strictly inside a cyclic path
pco-ordinate to the fill dominionfillrule(see fillrule). -
path[] strokepath(path g, pen p=currentpen); -
returns the path assortment that
PostScriptwould fill in drawing pathgrandwith penp.
an unresolved cubic spline (list of cubic-spline nodes and command points). The implicit initializer for a guide is nullpath; this is useful for building up a guide inside a loop.
A guide is similar to a path except that the computation of the cubic spline is deferred until drawing time (when it is resolved into a path); this allows two guides with gratis endpoint weather to be joined together smoothly. The solid curve in the post-obit example is built up incrementally as a guide, but merely resolved at drawing time; the dashed bend is incrementally resolved at each iteration, earlier the entire fix of nodes (shown in red) is known:
size(200); real mexican(real 10) {return (1-8x^ii)*exp(-(4x^two));} int n=30; real a=1.5; real width=2a/n; guide hat; path solved; for(int i=0; i < north; ++i) { real t=-a+i*width; pair z=(t,mexican(t)); hat=hat..z; solved=solved..z; } describe(lid); dot(hat,red); depict(solved,dashed);
Nosotros indicate out an efficiency distinction in the use of guides and paths:
guide g; for(int i=0; i < 10; ++i) grand=g--(i,i); path p=grand;
runs in linear time, whereas
path p; for(int i=0; i < 10; ++i) p=p--(i,i);
runs in quadratic time, as the entire path up to that point is copied at each step of the iteration.
The following routines tin can be used to examine the individual elements of a guide without actually resolving the guide to a fixed path (except for internal cycles, which are resolved):
-
int size(guide chiliad); -
Analogous to
size(path p). -
int length(guide g); -
Coordinating to
length(path p). -
bool circadian(path p); -
Analogous to
circadian(path p). -
pair point(guide m, int t); -
Analogous to
point(path p, int t). -
guide contrary(guide g); -
Coordinating to
reverse(path p). Ifyardis cyclic and as well contains a secondary cycle, it is first solved to a path, then reversed. Ifgis non cyclic merely contains an internal cycle, only the internal cycle is solved before reversal. If at that place are no internal cycles, the guide is reversed but not solved to a path. -
pair[] dirSpecifier(guide g, int i); -
This returns a pair array of length two containing the outgoing (in element 0) and incoming (in element 1) direction specifiers (or
(0,0)if none specified) for the segment of guidegbetwixt nodesiandi+1. -
pair[] controlSpecifier(guide g, int i); -
If the segment of guide
1000between nodesiandi+1has explicit outgoing and incoming control points, they are returned as elements 0 and 1, respectively, of a two-element array. Otherwise, an empty assortment is returned. -
tensionSpecifier tensionSpecifier(guide g, int i); -
This returns the tension specifier for the segment of guide
yardbetwixt nodesiandi+1. The private components of thetensionSpecifiertype tin can be accessed as the virtual membersin,out, andatLeast. -
real[] curlSpecifier(guide chiliad); -
This returns an assortment containing the initial curl specifier (in chemical element 0) and final curl specifier (in element 1) for guide
k.
As a technical item nosotros notation that a management specifier given to nullpath modifies the node on the other side: the guides
a..{upward}nullpath..b; c..nullpath{up}..d; e..{up}nullpath{down}..f; are respectively equivalent to
a..nullpath..{upwardly}b; c{upward}..nullpath..d; e{down}..nullpath..{up}f; gentileconstainey.blogspot.com
Source: https://asymptote.sourceforge.io/doc/Paths-and-guides.html