AoPSWiki
Visit the AoPS Book Store.

Asymptote: Logical Operators and Loops

From AoPSWiki

Revision as of 20:14, 5 March 2007 by Jcbowman (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Asymptote (Vector Graphics Language)
Getting Started - Basics - Reference - Examples - How to - Macros and Packages - Advanced Asymptote - Help

Useful functions - CSE5 Package

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:

Image:Smileys.gif

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 i and j each ranged from 0 to 4. Basically, the arguments in the parentheses for the first for loop first declare i to be an integer and assign to i the value 0. Then, if i<5, it executes what is inside the {} brackets and when it is finished, it adds 1 to i (++i). This process repeats until the boolean statement i<5 has the value false, i.e. 5 times (hence the 5 columns of smileys). The if statement is self-explanatory; if \lfloor(i-j)/2\rfloor=(i-j)/2 (which checks if i and j have the same parity or not), then the smiley is added, and if not it skips the brackets that follow.

For more information on logical operators and loops, see [here].

Looking for a challenging geometry text? Preparing for MATHCOUNTS or the AMC exams? Check out Art of Problem Solving's Introduction to Geometry by Richard Rusczyk.
© Copyright 2008 AoPS Incorporated. All Rights Reserved. • FoundationPrivacyContact Us