Lesson 3
Creating and Using Variables
In Asymptote, it's quite handy to name variables, whether it's for a value, for a point, or for a path. This is useful because you may need to use a point many times - with variables, instead of needing to type the coordinates every single time, you can just create a variable once and use the variable instead.
Next, we need to learn how to create a variable. The syntax is simple:
variableType variableName = variableValue;
I'm going to break this up into three parts: the variable type, name, and value.
If you've learned Java or something similar to it, you'll know what the variable type is. When creating a variable, Asymptote needs to know what kind of variable you're creating - for example, the four main types I'm going to be going over in this tutorial are
ints,
reals,
pairs, and
paths. We'll get to that soon.
The variable name is the name of the variable you're creating. This is effectively it's identifier - just as we identify each other by our names, Asymptote identifies variables by their names. NAMES CAN BE ANYTHING, BUT CANNOT CONTAIN SPACES.
The variable value is what is being stored inside that value, which you retrieve by referring to its name. We'll get to that soon.
Creating Variables
Ints. Other languages such as Java also have this data type. It's name is quite self-explanatory, the int stands for integer - in this kind of data type, you can store any integer you want, such as 5, -2, or 18430. To create an int, all you need to do is type this:
int variableName = integerValue;
The variableName is what you want to call this variable - try to call it something that makes sense. For example, if it's the x-coordinate of a point, you may want to call it x_coor.
Reals. If you want to create a variable that stores a real number but isn't an integer, use the real type. Just like ints, the syntax is as follows:
real variableName = numberValue;
If you know Python, reals are somewhat similar to floats in a sense. It can contain numbers such as 5, 5.5, or 3.141592 (note that you can store an int inside of a real).
Pairs. A pair is a data type that stores a coordinate pair. This is the data type that I use the most frequently in Asymptote, as Asymptote uses a coordinate system to tell where you want to draw. The syntax is as follows:
pair variableName = (xCoord, yCoord);
Note that you must use parenthesis around your variable value, or Asymptote will error.
Paths. A path is a drawing, such as a line or circle. This variable type is more open-ended, but the basic syntax is like this:
path variableName = variableValue;
For example, to create a variable containing a circle, you would do
path circle = circle((center_x, center_y), radius);
And to create a line,
path line = (x_1, y_1) -- (x_2, y_2);
Additionally, if you want to create multiple variables of the same type in a row, you can do this:
variableType name1 = value1, name2 = value2, name3 = value3;
where name1, name2, and name3 are the names of the variables and value1, value2, and value3 are the values of the variables respectively (you can create more or less than just 3 variables in one line).
Using Created Variables
To use a created variable, all you need to do is put the variable name wherever you want its value to be used. For example, you could do this:
pair center = (0, 0);
int radius = 5;
path circle = circle(center, radius);
draw(circle);
dot(center);
which would give you
this
Notice how I use a variable inside the creation of another variable.
Practice Problems
Problem 1: Create a variable of type int with name radius and value 5, and draw a dot using that variable at (radius, radius).
SolutionCode:
int radius = 5;
dot((radius, radius));
Result:
![[asy]
int radius = 5;
dot((radius, radius));
[/asy]](//latex.artofproblemsolving.com/b/0/f/b0f85348cddf6da3d29d72f627d52a1999123cc7.png)
Problem 2: Create 2 pairs in one line: one with coordinates (1, 2) and the other with coordinates (3, 1). Draw a line between those two points with the variables.
SolutionCode:
pair point1 = (1, 2), point2 = (3, 1);
draw(point1--point2);
Result:
![[asy]
pair point1 = (1, 2), point2 = (3, 1);
draw(point1--point2);[/asy]](//latex.artofproblemsolving.com/a/f/6/af6d4f621f488e44b65486f5b09bbf8bc933d422.png)
Problem 3: Create a variable of type path that contains a circle with center (4, 1) and radius 2. Draw that circle.
SolutionCode:
path circle = circle((4, 1), 2);
draw(circle);
Result:
![[asy]
path circle = circle((4, 1), 2);
draw(circle);[/asy]](//latex.artofproblemsolving.com/a/7/2/a723cfbf06ca1d6a97785caca8c7aaff0d4b1438.png)
This post has been edited 2 times. Last edited by CoolCarsOnTheRun, Nov 5, 2020, 4:51 AM