0
$\begingroup$

hello (edited and clarified) Could you help me improve this code to generate at least 10 "slightly larger" right triangles, but with small random data between 1 and 20 on two of the sides, can be the two legs or the hypotenuse and a leg and the missing side with an "x". In addition, add to the resulting figures a typical square that indicates that it is a right triangle.

 triangle[a_?NumericQ,b_?NumericQ,c_?NumericQ]:=
 Block[{x,y,pt,sqr},
 sqr=#.#&;
  pt[a1_,b1_,c1_]:=
  Reduce[sqr[{x,y}]==b1^2&&sqr[{x,y}-{a1,0}]==c1^2&&y>0,{x,y}];
 {(Polygon[{{0,0},{a,0},{x,y}}]),
  Text[Style[Framed[a,Background-> LightYellow],11],{a/2,0}],
  Text[Style[Framed[b,Background-> LightYellow],11],{x/2,y/2}],
  Text[Style[Framed[c,Background-> LightYellow],11], 
     {(a+x)/2,y/2}]}/.ToRules[pt[a,b,c]]]

  g[{s1_,s2_,s3_}]:=
  Graphics[{EdgeForm[Thick],FaceForm[None],triangle[s1,s2,s3]},
          ImagePadding->20,ImageSize->{200,200}]


  GraphicsGrid[{
  {g[{2, 1, Sqrt[5]}], g[{1, 2, Sqrt[5]}],
  g[{Sqrt[5], 1, 2}], g[{Sqrt[5], 2, 1}]},
  {g[{2, 2, Sqrt[8]}], g[{Sqrt[8], 2, 2}],
  g[{2, Sqrt[8], 2}],
  g[{Sqrt[2], Sqrt[2], 2}]}}]

  out put 

enter image description here desired execution

enter image description here

$\endgroup$

1 Answer 1

2
$\begingroup$
triangle[] := Module[{x, y, z, r, rep}, x = RandomInteger[{1, 20}];
  If[RandomInteger[{1, 2}] == 1, y = RandomInteger[{x, Min[6 x, 20]}],
    z = RandomInteger[{x + 1, Min[6 x, 20]}]];
  r = {RotationMatrix[{{-x, y}, {x, 0}}], 
     RotationMatrix[{{0, y}, {-x, 0}}], 
     IdentityMatrix[2]}[[RandomInteger[{1, 3}]]];
  {x, y, z} = ({x, y, z} /. 
     Solve[x^2 + y^2 == z^2, PositiveReals][[1]]);
  rep = {x, y, z};
  rep[[RandomInteger[{1, 3}]]] = "x";
  Graphics[{{Line[
      r . # & /@ {{Min[x, y]/10, 0}, {Min[x, y]/10, Min[x, y]/10}, {0,
          Min[x, y]/10}}]}, 
    Line[r . # & /@ {{0, 0}, {x, 0}, {0, y}, {0, 0}}], 
    Text[Style[Framed[rep[[1]], Background -> LightYellow], 10], 
     r . {x/2, 0}], 
    Text[Style[Framed[rep[[2]], Background -> LightYellow], 10], 
     r . {0, y/2}], 
    Text[Style[Framed[rep[[3]], Background -> LightYellow], 10], 
     r . ({(x y^2)/z^2 + x/2, (x^2 y)/z^2 + y/2}/2)]}, 
   ImageSize -> {200, 200}]]

GraphicsGrid[Partition[Table[triangle[], 9], 3], ImageSize -> 600]

enter image description here

$\endgroup$
3
  • $\begingroup$ azerbajdzan,exactly what i was looking for thank you. It can be done by presenting the triangles downwards, numbering them and leaving a column for their resolution. $\endgroup$
    – padre
    Commented Sep 30, 2022 at 12:13
  • 1
    $\begingroup$ (+1) @padre If the result is what your want, why not up-vote the answer and then ask another require. $\endgroup$
    – cvgmt
    Commented Sep 30, 2022 at 15:28
  • $\begingroup$ I'm sorry, I didn't think it was necessary, because I was going to vote for answer anyway, I'm sorry you thought otherwise $\endgroup$
    – padre
    Commented Oct 1, 2022 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.