0

我正在开发两个三角形的渐近线图形,其中边长是随机生成的。我打算将其用作 r-exams 中的问题模板,它可以生成同一问题的多个副本。

我已经解决了大部分问题。剩下的主要是将图像重新缩放到适当的大小,同时使文本保持一致的大小。当前的解决方案确实调整了图片的大小,但也调整了文本的大小。

请注意,请忽略直角标记未正确调整大小。我决定对其进行硬编码,直到对其他所有内容进行排序。

settings.outformat = "svg";
unitsize(2cm);

import geometry;
int max_length = 10;

srand(seconds());
 
//function for random numbers 1:max_length

int rand1toN(int n){
    return rand()%n + 1;
}

//randomly generated side lengths. 

int a, b, c;
bool is_triangle = false;

a = rand1toN(max_length);
b = rand1toN(max_length);

//to check that the three sides actually make a triangle
while(is_triangle==false){
    c = rand1toN(max_length);
    if(a+b>c &&
       b+c>a &&
       a+c>b){
           is_triangle = true;
       } 
}

//to sort sides so that the bottom edge is always the longest.
int[] side_lengths = {a, b, c};
side_lengths=sort(side_lengths);
a = side_lengths[0];
b = side_lengths[1];
c = side_lengths[2];

// for scale 

write(side_lengths);
//build the triangles 
triangle t = triangleabc(a,b,c,0,(0,0));
show(La = string(a), Lb = string(b), Lc = "", t);

//for perpendicular line
point D = projection(t.AB)*t.C;
dot("$D$", D, S);
segment CD = segment(t.C,D);
draw(CD);
pair diff = t.C - D;
label(format('%.3f',length(diff)), D + diff/2, 2W);

//for right angle
markrightangle(t.C, D, t.B, size = .3cm);

//for remaining side lengths
segment AD = segment(t.A, D);
real lengthAD = length(D-t.A);
label(format('%.3f', lengthAD), midpoint(AD), 2S);

segment DB = segment(t.B, D);
real lengthDB = length(D-t.B);
label(format('%.3f', lengthDB), midpoint(DB), 2S);

shipout(scale(10/c)*currentpicture.fit());
4

0 回答 0