|
|
|
(Compiled with Asymptote version 1.87svn-r4652)
|
|
size(15cm,0);
srand(rand());
path p1 = randompath(9);
path p2 = randompath(8);
real Minx=min(min(p1).x,min(p2).x);
real Maxx=max(max(p1).x,max(p2).x);
real Miny=min(min(p1).y,min(p2).y);
pair[] inter=intersectionpoints(p1,p2);
int nb=inter.length;
for (int i=0 ; i<nb; ++i)
{
dot(inter[i]);
label("$" + (string) i +"$", inter[i],N);
}
draw(p1,.8red);
draw(p2,.8green);
label("I found " + (string) nb + " points of intersection.",((Maxx+Minx)/2,Miny),2S);
Mots-clefs : Basis, Intersection, path, Random
|
|
|
(Compiled with Asymptote version 1.87svn-r4652)
|
|
size(8cm,0);
pair[] self_intersection(path p, int n=100)
{
pair[] rpair=new pair[];
path tpath;
real [] tpoint;
real l=length(p);
int i=1;
for (real t1=0; t1<l ; t1+=l/n)
{
for (real t2=t1+2*l/n; t2<l; t2+=l/n)
{
tpoint=intersect(subpath(p,t1,t1+l/n),
subpath(p,t2,t2+l/n));
if (tpoint.length == 2)
{
rpair[i]=point(subpath(p,t1,t1+l/n),tpoint[0]);
++i;
}
}
}
return rpair;
}
void dott(pair[] pt, pen p)
{
for (int i=1 ; i<pt.length; ++i)
{
dot(pt[i], p);
}
}
srand(rand());
path p = randompath(15);
pair[] inter=self_intersection(p);
dott(inter, .8red);
draw(p);
Mots-clefs : Intersection, path, Random
|
|
|
(Compiled with Asymptote version 1.87svn-r4652)
|
|
//From documentation of Asymptote
size(10cm);
// Draw Sierpinski triangle with top vertex A, side s, and depth q.
void Sierpinski(pair A, real s, int q,
bool top=true, bool randcolor=false) {
pair B=A-(1,sqrt(2))*s/2;
pair C=B+s;
if(top) draw(A--B--C--cycle);
if (randcolor) {
filldraw((A+B)/2--(B+C)/2--(A+C)/2--cycle,
(.33*rand()/randMax*red+.33*rand()/randMax*green+.33*rand()/randMax*blue));
} else draw((A+B)/2--(B+C)/2--(A+C)/2--cycle);
if(q > 0) {
Sierpinski(A,s/2,q-1,false,randcolor);
Sierpinski((A+B)/2,s/2,q-1,false,randcolor);
Sierpinski((A+C)/2,s/2,q-1,false,randcolor);
}
}
Sierpinski((0,1), 1, 5, randcolor=true);
Mots-clefs : Fractals, Function (creating), Function (recursion), Random
|
|
|
(Compiled with Asymptote version 1.84svn-r4619)
|
|
import three;
settings.render=0;
// The available directions of steps
triple[] dirs={X,-X,Y,-Y,Z,-Z};
dirs.cyclic=true;
// Return the nodes of the path
triple[] randWalk(real Srnd(), int n)
{
triple[] randPath;
triple camera=1e10*currentprojection.camera;
triple pos=O, tpos;
int R;
for (int i=0; i < n; ++i) {
R=round(Srnd());
tpos=pos+dirs[R];
randPath.push(tpos);
pos=tpos;
}
return randPath;
}
triple[] randWalk(int Srnd(), int n)
{
real R(){ return Srnd();}
return randWalk(R,n);
}
void drawWalk(triple[] nodes, pen p=white)
{
triple camera=currentprojection.camera;
if(currentprojection.infinity)
camera *= max(abs(minbound(nodes)),abs(maxbound(nodes)));
real[][] depth;
for(int i=0; i < nodes.length-1; ++i) {
real d=abs(camera-0.5*(nodes[i]+nodes[i+1]));
depth.push(new real[] {d,i});
}
depth=sort(depth);
triple M=nodes[round(depth[0][1])];
triple m=nodes[round(depth[depth.length-1][1]+1)];
// Draw from farthest to nearest
while(depth.length > 0) {
real[] a=depth.pop();
int i=round(a[1]);
draw(nodes[i]--nodes[i+1],abs(nodes[i]-m)/abs(M-m)*p);
}
}
size(18cm);
currentprojection=orthographic((1,1,1));
drawWalk(randWalk(rand,50000),cyan);
shipout(bbox(3mm,Fill));
Mots-clefs : array, Function (creating), Loop/for/while, Random
|
|
|
(Compiled with Asymptote version 1.84svn-r4619)
|
|
import three;
settings.render=0;
// The available directions of steps
triple[] dirs={X,-X,Y,-Y,Z,-Z};
dirs.cyclic=true;
struct walk
{
triple[] nodes;
pen[] p;
}
// Comput the nodes of the path
walk randWalk(real Srnd(), int n, pen[] p={currentpen})
{
p.cyclic=true;
walk ow;
triple pos=O, tpos;
for (int i=0; i < n; ++i) {
int R=round(Srnd());
tpos=pos+dirs[R];
ow.nodes.push(tpos);
ow.p.push(p[R]);
pos=tpos;
}
return ow;
}
walk randWalk(int Srnd(), int n, pen[] p={currentpen})
{
real R(){ return Srnd();}
return randWalk(R,n,p);
}
void drawWalk(walk walk)
{
triple camera=currentprojection.camera;
if(currentprojection.infinity)
camera *= max(abs(minbound(walk.nodes)),abs(maxbound(walk.nodes)));
real[][] depth;
for(int i=0; i < walk.nodes.length-1; ++i) {
real d=abs(camera-0.5*(walk.nodes[i]+walk.nodes[i+1]));
depth.push(new real[] {d,i});
}
depth=sort(depth);
triple M=walk.nodes[round(depth[0][1])];
triple m=walk.nodes[round(depth[depth.length-1][1]+1)];
// Draw from farthest to nearest
while(depth.length > 0) {
real[] a=depth.pop();
int i=round(a[1]);
// dot(walk.nodes[i],walk.p[i]);
draw(walk.nodes[i]--walk.nodes[i+1],abs(walk.nodes[i]-m)/abs(M-m)*(walk.p[i]+walk.p[i+1]));
}
}
size(18cm);
currentprojection=orthographic((0.5,0.5,1));
drawWalk(randWalk(rand,50000,new pen[]{red, blue, green, yellow, purple}));
shipout(bbox(3mm,Fill));
Mots-clefs : Random, struct
|
|
|
(Compiled with Asymptote version 1.87svn-r4652)
|
|
/* This code comes from The Official Asymptote Gallery */
size(7cm,0);
pair z1=(1,-0.25);
pair v1=dir(45);
pair z2=-z1;
pair v2=0.75*dir(260);
pair z3=(z1.x,-3);
// A centered random number
real crand() {return unitrand()-0.5;}
guide g;
pair lastz;
for(int i=0; i < 60; ++i) {
pair z=0.75*lastz+(crand(),crand());
g=g..2.5*z;
lastz=z;
}
g=shift(0,-.5)*g..cycle;
draw(g,gray(0.7));
draw("$r$",z1--z2,RightSide,red,Arrows,DotMargins);
draw(z1--z1+v1,Arrow);
draw(z2--z2+v2,Arrow);
draw(z3--z3+v1-v2,green,Arrow);
dot("1",z1,S,blue);
dot("2",z2,NW,blue);
Mots-clefs : path, Random
|
|
|
(Compiled with Asymptote version 1.87svn-r4652)
|
|
/* This code comes from The Official Asymptote Gallery */
import contour;
size(200);
int n=100;
pair[] points=new pair[n];
real[] values=new real[n];
real f(real a, real b) {return a^2+b^2;}
real r() {return 1.1*(rand()/randMax*2-1);}
for(int i=0; i < n; ++i) {
points[i]=(r(),r());
values[i]=f(points[i].x,points[i].y);
}
draw(contour(points,values,new real[]{0.25,0.5,1},operator ..),blue);
Mots-clefs : contour, Random
|
|
|
(Compiled with Asymptote version 1.87svn-r4652)
|
|
Mots-clefs : path3, Random