Difference between revisions of "Asymptote: Drawing"

m (Lines)
(Dots)
 
(19 intermediate revisions by 10 users not shown)
Line 10: Line 10:
 
dot((0,0));
 
dot((0,0));
 
</tt>
 
</tt>
 
<asy>
 
dot((0,0));
 
</asy>
 
  
 
You can fix certain attributes to this dot, such as color:
 
You can fix certain attributes to this dot, such as color:
  
 
<tt>
 
<tt>
dot((0,0),green);
+
dot((0,0),blue);
 
</tt>
 
</tt>
  
 
<asy>
 
<asy>
dot((0,0),green);
+
dot((0,0),blue);
 
</asy>
 
</asy>
  
==Lines==
+
To make the dot an open dot, you could draw a really small circle with a white fill and black outline:
  
Now let's draw a path, or a line segment.
+
<tt>
 +
filldraw(circle((0, 0), 0.02), white, black);
 +
</tt>
  
<tt>draw((0,0)--(5,5));</tt>
+
For example:
  
 
<asy>
 
<asy>
draw((0,0)--(5,5));
+
draw((0, 0) -- (0, 1), EndArrow(10));
 +
label("$(0, 1)$", (0, 1), NW);
 +
draw((0, 0) -- (1, 0), EndArrow(10));
 +
label("$(1, 0)$", (1, 0), SE);
 +
draw((0, 0) --(1, 1), blue);
 +
label("$x = y$", (0, 0) -- (1, 1), SE, blue);
 +
filldraw(circle((0, 0), 0.02), white, black);
 
</asy>
 
</asy>
  
Once again, we can set certain attributes, such as color and linewidth, both at the same time.
+
Or you could do this craziness:
 
 
<tt>draw((0,0)--(5,5),green+linewidth(3));</tt>
 
 
 
<asy>
 
draw((0,0)--(5,5),green+linewidth(1));
 
</asy>
 
 
 
Now if this diagram is too large, we can size it to be smaller:
 
  
 
<tt>
 
<tt>
size(100);
+
dot((0,0),filltype=FillDraw(fillpen=white, drawpen=black));
draw((0,0)--(5,5),green+linewidth(1));</tt>
+
</tt>
  
<asy>
+
For example:
size(100);
 
draw((0,0)--(5,5),green+linewidth(1));
 
</asy>
 
 
 
We can also create multiple paths with one line, if we want a triangle or a square, for example:
 
 
 
<tt>
 
draw((0,0)--(5,5)--(5,0)--cycle);</tt>
 
  
 
<asy>
 
<asy>
draw((0,0)--(5,5)--(5,0)--cycle);
+
draw((0,1)..(0,0)..(1,0), Arrows(10));
 +
dot((0,0),filltype=FillDraw(fillpen=white, drawpen=black));
 
</asy>
 
</asy>
 
Note that this uses the cycle command, meaning the path returns to its original point, in this case (0,0).
 
  
 
==Circles==
 
==Circles==
  
In this article, we create circular objects.
+
In this article,  
 
 
 
<tt>draw(circle((0,0),5));</tt>
 
<tt>draw(circle((0,0),5));</tt>
  
We see that the first '''draw()''' command creates the circle, which uses the '''circle()''' command. Within the circle command, we see the center point is located at the cartesian plane point (0,0), and it has a radius of 5.
+
We see that the first '''draw()''' command creates the circle, which uses the '''circle()''' command. How this works is that the circle() command produces a path in which the draw() command draws. Within the circle command, we see the center point is located at the cartesian plane point (0,0), and it has a radius of 5.
  
 
This code produces:
 
This code produces:
Line 101: Line 87:
 
<tt>draw(ellipse((0,0),5,3));</tt>
 
<tt>draw(ellipse((0,0),5,3));</tt>
  
In this case, the (0,0) is the center of the ellipse, the 5 is the length of the major axis and the 3 is the length of the minor axis. This results in:
+
In this case, the (0,0) is the center of the ellipse, the 5 is the length of the semi-major axis and the 3 is the length of the semi-minor axis. This results in:
  
 
<asy>
 
<asy>
Line 108: Line 94:
  
 
Once again, we can fix attributes and fill the inside.
 
Once again, we can fix attributes and fill the inside.
 +
 +
<tt>filldraw(ellipse((0,0),5,3),green,red+linewidth(1));</tt>
  
 
<asy>
 
<asy>
 
filldraw(ellipse((0,0),5,3),green,red+linewidth(1));
 
filldraw(ellipse((0,0),5,3),green,red+linewidth(1));
 +
</asy>
 +
 +
==Unit- Paths==
 +
 +
There are several useful pre defined paths for drawing things like unit squares, unit circles, etc. Just use the unit- paths!
 +
 +
You can use the
 +
 +
<tt>unitsquare</tt>
 +
<tt>unitcircle</tt>
 +
 +
paths for 2D. A list of Unit- paths for 3D can be found in the "Definitions": section of [[Asymptote: 3D graphics]]
 +
 +
Here is the <tt>unitsquare</tt> command:
 +
 +
<tt>draw(unitsquare);</tt> yields
 +
<asy>
 +
draw(unitsquare);
 +
</asy>
 +
 +
And the <tt>unitsphere</tt> command.(Note: you have to import the three module for this to work.)
 +
 +
<tt>import three;
 +
draw(unitsphere,pink);</tt>
 +
yields
 +
<asy>import three;
 +
draw(unitsphere,pink);</asy>
 +
 +
Since the unit- variables are paths, you can assign pen, fill them, and define other paths as them:
 +
 +
 +
<tt>path u=unitcircle;</tt>
 +
<tt>pen p=red+dashed;</tt>
 +
<tt>draw(u,p);</tt>
 +
 +
yields
 +
 +
<asy>
 +
path u=unitcircle;
 +
pen p=red+dashed;
 +
draw(u,p);
 
</asy>
 
</asy>

Latest revision as of 09:04, 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

Dots

Let us start off with the most basic of this basic command: drawing a dot.

To draw a dot, simply write the following code:

dot((0,0));

You can fix certain attributes to this dot, such as color:

dot((0,0),blue);

[asy] dot((0,0),blue); [/asy]

To make the dot an open dot, you could draw a really small circle with a white fill and black outline:

filldraw(circle((0, 0), 0.02), white, black);

For example:

[asy] draw((0, 0) -- (0, 1), EndArrow(10)); label("$(0, 1)$", (0, 1), NW); draw((0, 0) -- (1, 0), EndArrow(10)); label("$(1, 0)$", (1, 0), SE); draw((0, 0) --(1, 1), blue); label("$x = y$", (0, 0) -- (1, 1), SE, blue); filldraw(circle((0, 0), 0.02), white, black); [/asy]

Or you could do this craziness:

dot((0,0),filltype=FillDraw(fillpen=white, drawpen=black));

For example:

[asy] draw((0,1)..(0,0)..(1,0), Arrows(10)); dot((0,0),filltype=FillDraw(fillpen=white, drawpen=black)); [/asy]

Circles

In this article, draw(circle((0,0),5));

We see that the first draw() command creates the circle, which uses the circle() command. How this works is that the circle() command produces a path in which the draw() command draws. Within the circle command, we see the center point is located at the cartesian plane point (0,0), and it has a radius of 5.

This code produces:

[asy] draw(circle((0,0),5)); [/asy]

Once again, we can fix certain attributes to this code:

draw(circle((0,0),5),red+linewidth(1));

[asy] draw(circle((0,0),5),red+linewidth(1)); [/asy]

And we can fill the inside:

filldraw(circle((0,0),5),green,red+linewidth(1));

[asy] filldraw(circle((0,0),5),green,red+linewidth(1)); [/asy]

Ellipse

Another rounded figure we can create is the ellipse.

draw(ellipse((0,0),5,3));

In this case, the (0,0) is the center of the ellipse, the 5 is the length of the semi-major axis and the 3 is the length of the semi-minor axis. This results in:

[asy] draw(ellipse((0,0),5,3)); [/asy]

Once again, we can fix attributes and fill the inside.

filldraw(ellipse((0,0),5,3),green,red+linewidth(1));

[asy] filldraw(ellipse((0,0),5,3),green,red+linewidth(1)); [/asy]

Unit- Paths

There are several useful pre defined paths for drawing things like unit squares, unit circles, etc. Just use the unit- paths!

You can use the

unitsquare 
unitcircle 

paths for 2D. A list of Unit- paths for 3D can be found in the "Definitions": section of Asymptote: 3D graphics

Here is the unitsquare command:

draw(unitsquare); yields [asy] draw(unitsquare); [/asy]

And the unitsphere command.(Note: you have to import the three module for this to work.)

import three; draw(unitsphere,pink); yields [asy]import three; draw(unitsphere,pink);[/asy]

Since the unit- variables are paths, you can assign pen, fill them, and define other paths as them:


path u=unitcircle;
pen p=red+dashed;
draw(u,p);

yields

[asy] path u=unitcircle; pen p=red+dashed; draw(u,p); [/asy]