Difference between revisions of "Asymptote: Logical Operators and Loops"
Raymondzhu (talk | contribs) (removed dead link) |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
Asymptote uses loops and logical operators that are almost identical to those in C++. Loops are absolutely essential if you want to make diagrams that look like this: | Asymptote uses loops and logical operators that are almost identical to those in C++. Loops are absolutely essential if you want to make diagrams that look like this: | ||
− | + | <asy> | |
− | + | import graph; | |
+ | real r=5; | ||
+ | size(r*cm); | ||
+ | picture smiley; | ||
+ | filldraw(smiley,Circle((0,0),1),yellow,black); | ||
+ | fill(smiley,Circle((-.3,.4),.1),black); | ||
+ | fill(smiley,Circle((.3,.4),.1),black); | ||
+ | draw(smiley,Arc((0,0),.5,-140,-40)); | ||
+ | for (int i=0; i<5; ++i) | ||
+ | { | ||
+ | for (int j=0; j<5; ++j) | ||
+ | { | ||
+ | if (floor((i-j)/2)==((i-j)/2)) | ||
+ | { | ||
+ | add(scale(r/10*cm)*smiley,(i,j)); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </asy> | ||
This particular example was produced with the following code: | This particular example was produced with the following code: | ||
− | + | import graph; | |
real r=5; | real r=5; | ||
size(r*cm); | size(r*cm); | ||
Line 26: | Line 44: | ||
Above, we created a picture called <tt>smiley</tt> and added it to <tt>currentpicture</tt> many times using a <tt>for</tt> loop, as the indices <math>i</math> and <math>j</math> each ranged from <math>0</math> to <math>4</math>. Basically, the arguments in the parentheses for the first <tt>for</tt> loop first declare <math>i</math> to be an integer and assign to i the value <math>0</math>. Then, if <math>i<5</math>, it executes what is inside the <tt>{}</tt> brackets and when it is finished, it adds <math>1</math> to <math>i</math> (<tt>++i</tt>). This process repeats until the boolean statement <math>i<5</math> has the value false, i.e. 5 times (hence the 5 columns of smileys). The <tt>if</tt> statement is self-explanatory; if <math>\lfloor(i-j)/2\rfloor=(i-j)/2</math> (which checks if <math>i</math> and <math>j</math> have the same parity or not), then the smiley is added, and if not it skips the brackets that follow. | Above, we created a picture called <tt>smiley</tt> and added it to <tt>currentpicture</tt> many times using a <tt>for</tt> loop, as the indices <math>i</math> and <math>j</math> each ranged from <math>0</math> to <math>4</math>. Basically, the arguments in the parentheses for the first <tt>for</tt> loop first declare <math>i</math> to be an integer and assign to i the value <math>0</math>. Then, if <math>i<5</math>, it executes what is inside the <tt>{}</tt> brackets and when it is finished, it adds <math>1</math> to <math>i</math> (<tt>++i</tt>). This process repeats until the boolean statement <math>i<5</math> has the value false, i.e. 5 times (hence the 5 columns of smileys). The <tt>if</tt> statement is self-explanatory; if <math>\lfloor(i-j)/2\rfloor=(i-j)/2</math> (which checks if <math>i</math> and <math>j</math> have the same parity or not), then the smiley is added, and if not it skips the brackets that follow. | ||
− | |||
− |
Latest revision as of 13:49, 23 July 2024
Asymptote uses loops and logical operators that are almost identical to those in C++. Loops are absolutely essential if you want to make diagrams that look like this:
This particular example was produced with the following code:
import graph; real r=5; size(r*cm); picture smiley; filldraw(smiley,Circle((0,0),1),yellow,black); fill(smiley,Circle((-.3,.4),.1),black); fill(smiley,Circle((.3,.4),.1),black); draw(smiley,Arc((0,0),.5,-140,-40)); for (int i=0; i<5; ++i) { for (int j=0; j<5; ++j) { if (floor((i-j)/2)==((i-j)/2)) { add(scale(r/10*cm)*smiley,(i,j)); } } }
Above, we created a picture called smiley and added it to currentpicture many times using a for loop, as the indices and each ranged from to . Basically, the arguments in the parentheses for the first for loop first declare to be an integer and assign to i the value . Then, if , it executes what is inside the {} brackets and when it is finished, it adds to (++i). This process repeats until the boolean statement has the value false, i.e. 5 times (hence the 5 columns of smileys). The if statement is self-explanatory; if (which checks if and have the same parity or not), then the smiley is added, and if not it skips the brackets that follow.