Difference between revisions of "Asymptote: 3D graphics"

(Three)
(Three)
 
(20 intermediate revisions by 12 users not shown)
Line 3: Line 3:
  
 
==Three==
 
==Three==
Three is a module in Asymptote that allows the user to create three dimensional graphics. Usually all you must do is import three,
+
Three is a module in Asymptote that allows the user to create three dimensional graphics. Usually all you must do is add <code>import three;</code> to your code, then change from using doubles eg. (x,y) to using triples eg. (x,y,z) as coordinates. Some functions do not work when three is active. For example: To fill a surface, one must define a surface and draw that, instead of using <tt>[[asymptote: Filling|filldraw]]</tt>. This is also described at http://www.artofproblemsolving.com/Forum/viewtopic.php?f=519&t=399845.
<code>
 
import three;
 
</code>
 
 
 
then change from using doubles eg. (x,y) to using triples eg. (x,y,z) as coordinates. Some functions do not work when three is active. For example: In order to fill a surface one must define a surface and draw that. instead of using <tt>[[asymptote: Filling|filldraw]]</tt>. This is also described <url>http://www.artofproblemsolving.com/Forum/viewtopic.php?f=519&t=399845 here</url>.
 
  
 
===Data types===
 
===Data types===
 
three defines the data types:
 
three defines the data types:
path3, (3D version of path)
+
* path3, (3D version of path)
guide3, (3D version of guide)
+
* guide3, (3D version of guide)
and surface (a surface bounded by a path(3))
+
* and surface (a surface bounded by a path(3))
 
and other, less important ones.
 
and other, less important ones.
 +
 
===Definitions===
 
===Definitions===
 
three defines the surfaces:
 
three defines the surfaces:
unitcube, unitsphere, unitdisk, unitplane, unitcylinder, unitcone, unitsolidcone, and unithemisphere.
+
* unitcube
These can be drawn like you would normally draw an object in 2D
+
* unitsphere
 +
* unitdisk  
 +
* unitplane  
 +
* unitcylinder
 +
* unitcone
 +
* unitsolidcone
 +
* and unithemisphere.
 +
These can be drawn like you would normally draw an object in 2D.
 
<pre><nowiki>
 
<pre><nowiki>
draw(unitcube,green);
+
draw(unitcylinder,heavymagenta);
 
</nowiki></pre>
 
</nowiki></pre>
 +
<asy>
 +
import three;
 +
draw(unitcylinder,heavymagenta);</asy>
 +
Transforms also work:
 +
<pre><nowiki>
 +
draw(shift(2,3,4)*scale(5,20,7)*unitcone,paleblue);
 +
</nowiki></pre>
 +
<asy>
 +
import three;
 +
draw(shift(2,3,4)*scale(5,20,7)*unitcone,paleblue);</asy>
 +
You can even add an outline:
 +
<pre><nowiki>
 +
draw(unitcube,orange,blue);
 +
</nowiki></pre>
 +
<asy>
 +
import three;
 +
draw(unitcube,orange,blue);</asy>
 +
(Just beware - if you give something with a circle as a base an outline then it gets really wonky, like a unitsphere with an outline:
 +
<pre><nowiki>
 +
draw(unitsphere,red,black);
 +
</nowiki></pre>
 +
<asy>
 +
import three;
 +
draw(unitsphere,red,black);</asy>
 +
So try not to do that unless you want your stuff to look wonky.)
  
 
==Projection==
 
==Projection==
Line 29: Line 57:
 
currentprojection=orthographic(x,y,z);
 
currentprojection=orthographic(x,y,z);
 
</code>
 
</code>
To change current the view.
+
to change the current view.
 
<code>
 
<code>
 
currentprojection=perspective(x,y,z);
 
currentprojection=perspective(x,y,z);
 
</code>
 
</code>
Does the same thing, but is distorts the picture to imitate actual perspective.
+
does the same thing, but it distorts the picture to imitate actual perspective.
  
 
'''Example:'''
 
'''Example:'''
Line 40: Line 68:
 
<pre><nowiki>
 
<pre><nowiki>
 
import three;
 
import three;
/* perspective line /*
+
/* perspective line */
 
draw(unitcube,palegrey);
 
draw(unitcube,palegrey);
 
</nowiki></pre>
 
</nowiki></pre>
Line 65: Line 93:
  
 
'''Note:''' When current projection is not given, <tt>three</tt> tries to find the "best" view.
 
'''Note:''' When current projection is not given, <tt>three</tt> tries to find the "best" view.
 +
 
==Interactive Projection==
 
==Interactive Projection==
When using Asymptote on your computer (not on AoPS), you can add some code that lets you rotate/pan/zoom with the mouse.
+
When using Asymptote on your computer (but not on AoPS), you can add some code that lets you rotate/pan/zoom with the mouse.
 
<pre><nowiki>
 
<pre><nowiki>
 
import settings;
 
import settings;
Line 76: Line 105:
 
</nowiki></pre>
 
</nowiki></pre>
 
When compiling to PDF, it will allow you to rotate/pan/zoom with the mouse.
 
When compiling to PDF, it will allow you to rotate/pan/zoom with the mouse.
 +
 
==Arrows and bars==
 
==Arrows and bars==
 
Arrows and bars in 3D are the same as in 2D except you add a 3 to the end of the name.
 
Arrows and bars in 3D are the same as in 2D except you add a 3 to the end of the name.
Line 107: Line 137:
 
label("$y=1$",(1,1,0.5),E);
 
label("$y=1$",(1,1,0.5),E);
 
label("$z=1$",(1,0.5,0),SE);
 
label("$z=1$",(1,0.5,0),SE);
label("$c$",(0.5,0.5,0.5),N);[/asy]
+
label("$c$",(0.5,0.5,0.5),N);
 
</nowiki></pre>
 
</nowiki></pre>
 
Which renders to
 
Which renders to
Line 128: Line 158:
 
label("$z=1$",(1,0.5,0),SE);
 
label("$z=1$",(1,0.5,0),SE);
 
label("$c$",(0.5,0.5,0.5),N);</asy>
 
label("$c$",(0.5,0.5,0.5),N);</asy>
See also:
 
  
* http://www.artofproblemsolving.com/Forum/viewtopic.php?p=1074625&#p1074625
+
For other examples, see [[Platonic solids]] and [[2000 AMC 12 Problems/Problem 25]].
  
 
==Other 3D Modules==
 
==Other 3D Modules==
 
Other modules in Asymptote that are for 3D are:
 
Other modules in Asymptote that are for 3D are:
graph3, grid3, contour3, and solids.
+
* graph3
 +
* grid3
 +
* contour3
 +
* and solids.

Latest revision as of 08:53, 4 August 2024

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


Three

Three is a module in Asymptote that allows the user to create three dimensional graphics. Usually all you must do is add import three; to your code, then change from using doubles eg. (x,y) to using triples eg. (x,y,z) as coordinates. Some functions do not work when three is active. For example: To fill a surface, one must define a surface and draw that, instead of using filldraw. This is also described at http://www.artofproblemsolving.com/Forum/viewtopic.php?f=519&t=399845.

Data types

three defines the data types:

  • path3, (3D version of path)
  • guide3, (3D version of guide)
  • and surface (a surface bounded by a path(3))

and other, less important ones.

Definitions

three defines the surfaces:

  • unitcube
  • unitsphere
  • unitdisk
  • unitplane
  • unitcylinder
  • unitcone
  • unitsolidcone
  • and unithemisphere.

These can be drawn like you would normally draw an object in 2D.

draw(unitcylinder,heavymagenta);

[asy] import three; draw(unitcylinder,heavymagenta);[/asy] Transforms also work:

draw(shift(2,3,4)*scale(5,20,7)*unitcone,paleblue);

[asy] import three; draw(shift(2,3,4)*scale(5,20,7)*unitcone,paleblue);[/asy] You can even add an outline:

draw(unitcube,orange,blue);

[asy] import three; draw(unitcube,orange,blue);[/asy] (Just beware - if you give something with a circle as a base an outline then it gets really wonky, like a unitsphere with an outline:

draw(unitsphere,red,black);

[asy] import three; draw(unitsphere,red,black);[/asy] So try not to do that unless you want your stuff to look wonky.)

Projection

You can use currentprojection=orthographic(x,y,z); to change the current view. currentprojection=perspective(x,y,z); does the same thing, but it distorts the picture to imitate actual perspective.

Example:

base code:

import three;
/* perspective line */
draw(unitcube,palegrey);

Using currentprojection=orthographic(1,1/2,1/2); We get a unit cube as: [asy] import three; currentprojection=orthographic(1,1/2,1/2);  draw(unitcube,palegrey); [/asy] Using currentprojection=perspective(1,1/2,1/2); We get a unit cube as: [asy] import three; currentprojection=perspective(1,1/2,1/2);  draw(unitcube,palegrey); [/asy]

Note: When current projection is not given, three tries to find the "best" view.

Interactive Projection

When using Asymptote on your computer (but not on AoPS), you can add some code that lets you rotate/pan/zoom with the mouse.

import settings;
leftbutton=new string[] {"rotate","zoom","shift","pan"};
middlebutton=new string[] {"menu"};
rightbutton=new string[] {"zoom/menu","rotateX","rotateY","rotateZ"};
wheelup=new string[] {"zoomin"};
wheeldown=new string[] {"zoomout"};

When compiling to PDF, it will allow you to rotate/pan/zoom with the mouse.

Arrows and bars

Arrows and bars in 3D are the same as in 2D except you add a 3 to the end of the name. Example.

import three;
draw((0,0,0)--(1,1,1),green,Arrows3);
draw((0,1,0)--(1,0,1),blue,Bars3);

[asy] import three; draw((0,0,0)--(1,1,1),green,Arrows3); draw((0,1,0)--(1,0,1),blue,Bars3); [/asy]

Examples

import three;
unitsize(1cm);
size(200);
currentprojection=perspective(1/3,-1,1/2);
draw((0,0,0)--(1,0,0)--(1,1,0)--(0,1,0)--cycle,red);
draw((0,0,0)--(0,0,1),red);
draw((0,1,0)--(0,1,1),red);
draw((1,1,0)--(1,1,1),red);
draw((1,0,0)--(1,0,1),red);
draw((0,0,1)--(1,0,1)--(1,1,1)--(0,1,1)--cycle,red);
draw((0,0,0)--(1,0,0)--(1,1,0)--cycle,red);
draw((0,0,0)--(1,1,0)--(1,1,1)--cycle,blue);
label("$o$",(0,0,0),NW);
label("$x=1$",(0.5,0,0),S);
label("$y=1$",(1,1,0.5),E);
label("$z=1$",(1,0.5,0),SE);
label("$c$",(0.5,0.5,0.5),N);

Which renders to [asy] import three; unitsize(1cm); size(200); currentprojection=orthographic(1/3,-1,1/2); draw((0,0,0)--(1,0,0)--(1,1,0)--(0,1,0)--cycle,red); draw((0,0,0)--(0,0,1),red); draw((0,1,0)--(0,1,1),red); draw((1,1,0)--(1,1,1),red); draw((1,0,0)--(1,0,1),red); draw((0,0,1)--(1,0,1)--(1,1,1)--(0,1,1)--cycle,red); draw((0,0,0)--(1,0,0)--(1,1,0)--cycle,red); draw((0,0,0)--(1,1,0)--(1,1,1)--cycle,blue); label("$o$",(0,0,0),NW); label("$x=1$",(0.5,0,0),S); label("$y=1$",(1,1,0.5),E); label("$z=1$",(1,0.5,0),SE); label("$c$",(0.5,0.5,0.5),N);[/asy]

For other examples, see Platonic solids and 2000 AMC 12 Problems/Problem 25.

Other 3D Modules

Other modules in Asymptote that are for 3D are:

  • graph3
  • grid3
  • contour3
  • and solids.