A “Hello World” program using Asymptote - A Vector Graphics Language
Author: T.T.Le & Chat GPT
Asymptote is a potent coding language designed for generating vector images, which means that the resulting images maintain their high quality even when zoomed in. It employs a syntax reminiscent of C++, and it seamlessly integrates with LaTeX documents as a package.
In this guide, we will walk you through the process of creating a simple “Hello World” plot using Asymptote.
Checking Asymptote Installation
Before we get started, it’s important to ensure that Asymptote is installed on your computer. If you have LaTeX installed, there’s a good chance that Asymptote is already available. To check, open your terminal or command prompt and run the following command:
asy --version
If Asymptote is installed, you will see version information displayed in the terminal. If it’s not installed, you can download it from the official Asymptote website.
Creating a “Hello World” Plot
Now that we have Asymptote installed, let’s create a simple “Hello World” plot using the following code:
import graph;
size(3cm); // Set the size of the output image
real R = 2; // Radius of the circle
pair O = (0, 0); // Center of the circle
path circle = Circle(O, R); // Define the circle
filldraw(circle, purple); // Fill the circle with purple
label("Hello World!", O + (0, 0), yellow); // Optional: Add a label to the circle
// Save the image as a PDF file
settings.outformat = "pdf";
string filename = "hello.pdf";
shipout(filename);
In this code:
- We import the
graph
module, which provides us with tools for drawing graphics. - We set the size of the output image to 3cm by using the
size
function. - We define a circle with a radius
R
and a center pointO
. - We fill the circle with a purple color using the
filldraw
function. - We add an optional label, “Hello World!”, to the center of the circle in yellow.
- Finally, we save the image as a PDF file named “hello.pdf.”
Running the Asymptote Code
To run the Asymptote code and generate the “Hello World” plot, follow these steps:
-
Save the Asymptote code in a file with a
.asy
extension (e.g.,hello.asy
). -
Open your terminal or command prompt and navigate to the directory where you saved the
.asy
file. -
Run the following command to execute the Asymptote code:
asy hello.asy
-
Once the code is executed, you will find a PDF file named “hello.pdf” in the same directory.
Note that if you want to export file into a .png
format with high quality, you can use the command: asy -f png -render 100 hello.asy
Conclusion
Congratulations! You’ve successfully created your first Asymptote plot that displays a “Hello World” message inside a purple circle. Asymptote’s versatility and integration with LaTeX make it a valuable tool for creating custom graphics and illustrations in your documents. Feel free to explore more complex designs and customize your plots to suit your needs. Happy plotting!