Difference between revisions of "Asymptote: Useful functions"

(Marking Angles)
(Olympiad)
(6 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
__TOC__
 
__TOC__
  
== Marking Angles ==
+
== Labeling angles ==
 
The bundled <code>markers</code> package provides a useful function for marking angles. The exact same function exists in the <code>geometry</code> package.
 
The bundled <code>markers</code> package provides a useful function for marking angles. The exact same function exists in the <code>geometry</code> package.
 
<blockquote>
 
<blockquote>
    void '''markangle'''(picture ''pic''=currentpicture,<br/>
+
void '''markangle'''(picture ''pic''=currentpicture,<br/>
    Label ''L''="", int ''n''=1, real ''radius''=0, real ''space''=0,<br/>
+
Label ''L''="", int ''n''=1, real ''radius''=0, real ''space''=0,<br/>
    line ''l1'', line ''l2'',<br/>
+
line ''l1'', line ''l2'',<br/>
    arrowbar ''arrow''=None, pen ''p''=currentpen,<br/>
+
arrowbar ''arrow''=None, pen ''p''=currentpen,<br/>
    margin ''margin''=NoMargin, marker ''marker''=nomarker)
+
margin ''margin''=NoMargin, marker ''marker''=nomarker)
 
</blockquote>
 
</blockquote>
As with other angle functions, you can replace <code>line l1, line l2</code> with <code>pair A, pair O, pair B</code>, which would mark the angle <math>\angle AOB</math>. The function will draw an arc from line AO or l1 to line BO or l2, counterclockwise, with radius <math>\abs(\textrm{radius})</math> and label it with the provided <code>Label L</code>.
+
As with other angle functions, you can replace <code>line l1, line l2</code> with <code>pair A, pair O, pair B</code>, which would mark the angle <math>\angle AOB</math>. The function will draw an arc from line AO or l1 to line BO or l2, counterclockwise, with radius <math>|\textrm{radius}|</math> and label it with the provided <code>Label L</code>.
  
 
If the function draws the angle in the wrong direction, try swapping the first pair with the third pair, or, if you put in lines, swapping l1 and l2.
 
If the function draws the angle in the wrong direction, try swapping the first pair with the third pair, or, if you put in lines, swapping l1 and l2.
  
 
Here's an example to do that with an orange pen:
 
Here's an example to do that with an orange pen:
<pre><nowiki>
+
<syntaxhighlight lang="asy">
 
pair A = (1,1);
 
pair A = (1,1);
 
pair O = (0,0);
 
pair O = (0,0);
Line 26: Line 26:
 
import geometry;
 
import geometry;
 
markangle("$45^o$", B, O, A, p=orange);
 
markangle("$45^o$", B, O, A, p=orange);
</nowiki></pre>
+
</syntaxhighlight>
 
<asy>
 
<asy>
 
pair A = (1,1);
 
pair A = (1,1);
Line 35: Line 35:
  
 
import geometry;
 
import geometry;
markangle("$45^o$", B, O, A, p=orange);
+
markangle("$45^\circ$", B, O, A, p=orange);
 +
</asy>
 +
 
 +
=== Olympiad ===
 +
{{hatnote|Main article: [[Asymptote: Marking Angles]]}}
 +
The <code>olympiad</code> package provides a function that generates a path that can then be drawn using the draw() function.
 +
<blockquote>
 +
path '''anglemark'''(pair ''A'', pair ''B'', pair ''C'',<br/>
 +
real ''t''=8, ... real[] ''s''=8)
 +
</blockquote>
 +
''t'' is the radius of the first line, and s[] stores the radius of subsequent lines.
 +
To label it, simply use the label() function per usual.
 +
<syntaxhighlight lang="asy">
 +
pair A = (1,1);
 +
pair O = (0,0);
 +
pair B = (2,0);
 +
draw(O--A);
 +
draw(O--B);
 +
 
 +
import olympiad;
 +
draw(anglemark(B, O, A), p=orange);
 +
label("$45^\circ$", anglemark(B, O, A), NE);
 +
</syntaxhighlight>
 +
<asy>
 +
pair A = (1,1);
 +
pair O = (0,0);
 +
pair B = (2,0);
 +
draw(O--A);
 +
draw(O--B);
 +
import olympiad;
 +
draw(anglemark(B, O, A), p=orange);
 +
label("$45^\circ$", anglemark(B, O, A), NE);
 
</asy>
 
</asy>
 +
As you can see, this is an inferior method as the path also covers part of the angle and the origin of labeling is the point instead of the angle mark. However, anglemark() makes a bit more abstract sense and is more flexible.
  
 
== Intersection points ==
 
== Intersection points ==
Line 42: Line 74:
  
 
Examples:  
 
Examples:  
<pre><nowiki>size(8cm,0);
+
<syntaxhighlight lang="asy">
 +
size(8cm,0);
 
import math;
 
import math;
 
import graph;
 
import graph;
Line 58: Line 91:
 
dot(x[0],3bp+blue);
 
dot(x[0],3bp+blue);
 
dot(x[1],3bp+blue);
 
dot(x[1],3bp+blue);
draw(x[0] -- x[1],1bp+red);</nowiki></pre>
+
draw(x[0] -- x[1],1bp+red);
 +
</syntaxhighlight>
  
 
<asy>size(200,200);
 
<asy>size(200,200);
Line 78: Line 112:
 
draw(x[0] -- x[1],1bp+red);</asy>
 
draw(x[0] -- x[1],1bp+red);</asy>
  
<pre><nowiki>
+
<syntaxhighlight lang="asy">
 
path g=(-3,-sqrt(3))--(3,-sqrt(3))--(0,2*sqrt(3))--cycle;
 
path g=(-3,-sqrt(3))--(3,-sqrt(3))--(0,2*sqrt(3))--cycle;
 
draw(g,black);
 
draw(g,black);
Line 95: Line 129:
 
fill(x[0]--x[1]--x[2]--x[3]--x[4]--x[5]--cycle,black);
 
fill(x[0]--x[1]--x[2]--x[3]--x[4]--x[5]--cycle,black);
 
dot((0,0),white);
 
dot((0,0),white);
</asy>
+
</syntaxhighlight>
 
Note that this function will cause an error if the paths involved do not intersect.
 
Note that this function will cause an error if the paths involved do not intersect.
  

Revision as of 20:18, 26 March 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

Labeling angles

The bundled markers package provides a useful function for marking angles. The exact same function exists in the geometry package.

void markangle(picture pic=currentpicture,
Label L="", int n=1, real radius=0, real space=0,
line l1, line l2,
arrowbar arrow=None, pen p=currentpen,
margin margin=NoMargin, marker marker=nomarker)

As with other angle functions, you can replace line l1, line l2 with pair A, pair O, pair B, which would mark the angle $\angle AOB$. The function will draw an arc from line AO or l1 to line BO or l2, counterclockwise, with radius $|\textrm{radius}|$ and label it with the provided Label L.

If the function draws the angle in the wrong direction, try swapping the first pair with the third pair, or, if you put in lines, swapping l1 and l2.

Here's an example to do that with an orange pen:

pair A = (1,1);
pair O = (0,0);
pair B = (2,0);
draw(O--A);
draw(O--B);

import geometry;
markangle("$45^o$", B, O, A, p=orange);

[asy] pair A = (1,1); pair O = (0,0); pair B = (2,0); draw(O--A); draw(O--B);  import geometry; markangle("$45^\circ$", B, O, A, p=orange); [/asy]

Olympiad

The olympiad package provides a function that generates a path that can then be drawn using the draw() function.

path anglemark(pair A, pair B, pair C,
real t=8, ... real[] s=8)

t is the radius of the first line, and s[] stores the radius of subsequent lines. To label it, simply use the label() function per usual.

pair A = (1,1);
pair O = (0,0);
pair B = (2,0);
draw(O--A);
draw(O--B);

import olympiad;
draw(anglemark(B, O, A), p=orange);
label("$45^\circ$", anglemark(B, O, A), NE);

[asy] pair A = (1,1); pair O = (0,0); pair B = (2,0); draw(O--A); draw(O--B); import olympiad; draw(anglemark(B, O, A), p=orange); label("$45^\circ$", anglemark(B, O, A), NE); [/asy] As you can see, this is an inferior method as the path also covers part of the angle and the origin of labeling is the point instead of the angle mark. However, anglemark() makes a bit more abstract sense and is more flexible.

Intersection points

pair[] intersectionpoints(path, path);

Examples:

size(8cm,0);
import math;
import graph;
real r,s;
pair a,b, common;
path circ1, circ2;
r=1; s=1;
a=(0,0);
b=(1,0);
circ1=circle(a,r);
circ2=circle(b,s);
draw(circ1,linewidth(1bp));
draw(circ2,1bp+green);
pair [] x=intersectionpoints(circ1, circ2);
dot(x[0],3bp+blue);
dot(x[1],3bp+blue);
draw(x[0] -- x[1],1bp+red);

[asy]size(200,200); import math; import graph; real r,s; pair a,b, common; path circ1, circ2; r=1; s=1; a=(0,0); b=(1,0); circ1=circle(a,r); circ2=circle(b,s); draw(circ1,linewidth(1bp)); draw(circ2,1bp+green); pair [] x=intersectionpoints(circ1, circ2); dot(x[0],3bp+blue); dot(x[1],3bp+blue); draw(x[0] -- x[1],1bp+red);[/asy]

path g=(-3,-sqrt(3))--(3,-sqrt(3))--(0,2*sqrt(3))--cycle;
draw(g,black);
path p=-2/3*(-3,-sqrt(3))--(-2/3)*(3,-sqrt(3))--(-2/3)*(0,2*sqrt(3))--cycle;
draw(p,black);
pair [] x=intersectionpoints(g, p);
fill(x[0]--x[1]--x[2]--x[3]--x[4]--x[5]--cycle,black);
dot((0,0),white);
</nowiki></pre>
<asy>
path g=(-3,-sqrt(3))--(3,-sqrt(3))--(0,2*sqrt(3))--cycle;
draw(g,black);
path p=-2/3*(-3,-sqrt(3))--(-2/3)*(3,-sqrt(3))--(-2/3)*(0,2*sqrt(3))--cycle;
draw(p,black);
pair [] x=intersectionpoints(g, p);
fill(x[0]--x[1]--x[2]--x[3]--x[4]--x[5]--cycle,black);
dot((0,0),white);

Note that this function will cause an error if the paths involved do not intersect.


Next: CSE5