Asymptote code doesn't generate triangle in a pictures

57 Views Asked by At
from google.colab import drive
drive.mount('/content/drive')

import subprocess

# Install Asymptote
subprocess.run(['apt-get', '-y', 'install', 'asymptote'])
import os

# Generating asymptote code for a right triangle with leg lengths 1 to x and 1 to y (Assume x<=y)
x = 1
y = 2

for i in range (1, x+1):
  for j in range (i, y+1):
    side_lengths = [i, j]

    asy_code = '''
    import graph;
    size(3cm);
    real a = {};
    real b = {};

    pair A = (0, 0);
    pair B = (a, 0);
    pair C = (0, b);

    draw(A--B--C--cycle);
    label("$A$", A, SW);
    label("$B$", B, SE);
    label("$C$", C, N);
    label(string(a), (A + B) / 2, S);
    label(string(b), (A + C) / 2, W);
    '''

    asy_code = asy_code.format(*side_lengths)



    output_file_path = f'/content/drive/My Drive/rightTriangle{i}-{j}.asy'
    print(output_file_path)
    with open(output_file_path, 'w') as f:
      f.write(asy_code)

    # Define the output file path for the generated image
    output_image_path = f'/content/drive/My Drive/rightTriangle{i}-{j}.png'

    # Run Asymptote to generate the image
    subprocess.run(['asy', '-f', 'png', '-o', output_image_path, output_file_path])

Expecting to generate png files with triangles. The labels are drawn correctly, but lines are not drawn in the files.

I tested the generated asymptote code using a web interface, and it works perfectly fine.

The issue probably has to do with the settings I passed into the Asymptote command.

0

There are 0 best solutions below