!set gl_type=dynamic
!set gl_author=Euler, Acadmie de Versailles
!set gl_title=Aire d'un triangle (exemple)
!set gl_renew=1

!set gl_polyg=BEC,BIC,BIO,BIP,BLE,BOF,BOL,BON,BOX,BUS,BUT,CIL,CLE,COL,COR,COU,CRI,CRU,DEO,DES,DIX,DON,DOS,DRU,DUC,DUO,DUR,EST,EUX,EXO,FER,FEU,FIL,FIN,FOU,FUT,GEL,GIF,GYM,ILE,ILS,INF,ION,JEU,JET,JOB,JUS,LED,LES,LIN,LIT,LOI,LOT,LUI,LYS,MER,MES,MIE,MIL,MOI,MON,MOT,MOU,MUR,NEF,NEZ,NID,NOM,NOS,NUL,ODE,OIE,OUF,OUI,PEU,PIC,PIE,PIF,PIN,PLI,POC,POF,POT,PUB,PUR,RIO,RIZ,ROC,ROI,RUE,SEC,SEL,SET,SIR,SIX,SKI,SOL,SON,SOU,SPI,SUD,SUP,SUR,TEL,TES,TIC,TIR,TOC,TOI,TOM,TON,TOP,TRI,UNE,UNI,VIE,VIF,VIS,VOL,VOS,VUE,WEB,YEN,ZEN,ZOE,ZUT
!set gl_nom=!randitem $gl_polyg
!set gl_R=!char 1 of $gl_nom
!set gl_S=!char 2 of $gl_nom
!set gl_T=!char 3 of $gl_nom
!readproc data/glossary/mathematics/geometry/macro/randalphanum_gen
!set gl_alea=$gl_rd

!set gl_b1=brd_$gl_alea
!set gl_id1=id_$gl_alea
<style>
/*<![CDATA[*/
#appletA{order:2;}
#appletB{order:1;}
@media screen and (max-width: 40em) {
#appletA_$gl_alea{order:2;}
#appletB_$gl_alea{order:1;}
}
div.appletB{padding-left:1.5em;}
.rem_$gl_alea{font-size:0.8em;font-style:italic;text-align:center}
/*]]>*/
</style>
!set gl_dim1=!randint 2,9
!set gl_dim2=!randint 1,12
!set gl_dim3=!randint 3,12
!set gl_choix=!randint 1,8
!set gl_ens_unitl=unit de longueur,mm,cm,dm,m,dam,hm,km
!set gl_ens_unita=unit d'aire,mm<sup>2</sup>,cm<sup>2</sup>,dm<sup>2</sup>,m<sup>2</sup>,dam<sup>2</sup>,hm<sup>2</sup>,km<sup>2</sup>
!set gl_unitl=$(gl_ens_unitl[$gl_choix])
!set gl_unita=$(gl_ens_unita[$gl_choix])

!set gl_script_$gl_alea =\
JXG.Options.point.snapToGrid = true;\
JXG.Options.point.snapSizeX = 1;\
JXG.Options.point.snapSizeY = 1;\
JXG.Options.elements.tabindex = -1;\
const $gl_b1 = JXG.JSXGraph.initBoard('$gl_id1', {\
  boundingbox:  [-1.75, 16, 15.75, -1.5],\
  axis:false,\
  grid:false,\
  showCopyright : false,\
  showNavigation:false,\
  showZoom : false,\
  keepaspectratio : true,\
  zoom: {enabled: false,wheel:false,needShift: false,factorX: 0,factorY: 0,min:0.001,max:0.001,pinchHorizontal: false,pinchVertical: false,pinchSensitivity: 0},\
  pan: {enabled: false,needTwoFingers: false,needShift:false}\
  });\
$gl_b1.renderer.displayCopyright('Figure ralise avec JSXGraph v'+JXG.version,11);\
dir4 = function () {\
  if (!this.visProp.fixed) {\
    $gl_b1.containerObj.style.cursor = 'move';\
  }\
};\
out = function () {\
  if (!this.visProp.fixed) {\
    $gl_b1.containerObj.style.cursor = 'default';\
  }\
};\
dir2 = function () {\
  if (!this.visProp.fixed) {\
    $gl_b1.containerObj.style.cursor = 'w-resize';\
  }\
};\
let s1_$gl_alea = $gl_dim1;\
let s2_$gl_alea = $gl_dim2;\
let s3_$gl_alea = $gl_dim3;\
let xR_$gl_alea=0;\
let yR_$gl_alea=0;\
let seg_$gl_alea=[];\
for (let i=-1;i<16;i++){\
  for (let j=-1;j<13;j++){\
  seg_$gl_alea[i]=$gl_b1.create('segment',[[-1,j],[15,j]],{strokeColor:'#e3e3e3',strokeWidth:0.7,highlight: false,fixed:true});\
 $gl_b1.create('segment',[[i,-1],[i,12]],{strokeColor:'#e3e3e3',strokeWidth:0.7,highlight: false,fixed:true});\
 }\
}\
let z_$gl_alea=[];\
const coordo_$gl_alea=[[-1,1],[15,1],[-1,12],[15,12]];\
for (let m=0;m<4;m++){\
  z_$gl_alea[m]=$gl_b1.create('point',coordo_$gl_alea[m],{fixed:true,size:0,name:'',showInfobox:false});\
}\
let zone_$gl_alea= $gl_b1.create('polygon',[z_$gl_alea[0],z_$gl_alea[1],z_$gl_alea[3],z_$gl_alea[2]],{hasInnerPoints:true,borders:{visible:false},fillcolor:'transparent',highlight:false});\
let p1_$gl_alea = $gl_b1.create('point',[xR_$gl_alea, yR_$gl_alea],{name:'$gl_R',size:0,color:'green',fixed:true,face:'plus',showInfobox:false,label:{offset:[0,-15]}});\
let l1_$gl_alea=$gl_b1.create('segment',[[12,0],[1,0]],{strokeColor:'transparent',highlight:false,fixed:true});\
let p2_$gl_alea=$gl_b1.create('glider', [s1_$gl_alea, yR_$gl_alea,l1_$gl_alea],{fixed:false,name:'$gl_S',size:2,color:'green',showInfobox:false,tabindex:0,label:{offset:[0,-15]},layer:15});\
let p3_$gl_alea=$gl_b1.create('point',[s2_$gl_alea,s3_$gl_alea],{name:'$gl_T',size:2,fixed:false,color:'green',showInfobox:false,tabindex:0,label:{offset:[0,10]},layer:15});\
let pol_$gl_alea=$gl_b1.create('polygon',[p1_$gl_alea,p2_$gl_alea,p3_$gl_alea],{fillcolor:'green',borders:{strokeColor:'green',strokeWidth:2,layer:10,highlight:false},vertices:{face:'plus',strokeColor:'green',size:0,showInfobox:false,label:{fixed:false,offset:[-4,8]}},highlight: false});\
let p5_$gl_alea=$gl_b1.create('midpoint', [p1_$gl_alea, p2_$gl_alea],{name:'',size:0,color:'green',fixed:true,face:'',showInfobox:false,withLabel:false});\
let t2_$gl_alea = $gl_b1.create('text',[0, -0.5,function() { return p2_$gl_alea.X().toFixed(0)}], {anchor: p5_$gl_alea,anchorX:'middle',anchorY:'top',color:'green',fixed:true});\
let montrer_$gl_alea;\
if((p2_$gl_alea.X()==p3_$gl_alea.X())||(p1_$gl_alea.X()==p3_$gl_alea.X())){ montrer_$gl_alea=false;} else {montrer_$gl_alea=true;}\
let H_$gl_alea =$gl_b1.create('perpendicularpoint',[p3_$gl_alea, l1_$gl_alea],{name:'H',visible:montrer_$gl_alea,size:1,face:"plus",color:'blue',showInfobox:false,highlight:false,label:{offset:[-2,-10]}});\
let haut_$gl_alea=$gl_b1.create('segment',[H_$gl_alea,p3_$gl_alea],{dash:"1",highlight: false});\
let ang_$gl_alea =$gl_b1.create('angle',[haut_$gl_alea,l1_$gl_alea,1,1],{name:' ',radius:0.4,color:'blue',highlight:false});\
let base_$gl_alea= $gl_b1.create('segment',[p2_$gl_alea,H_$gl_alea],{dash:"1",highlight: false});\
let p6_$gl_alea=$gl_b1.create('midpoint',[p3_$gl_alea, H_$gl_alea],{name:' ',size:0,fixed:true,face:'',visible:false,showInfobox:false,withLabel:false,highlight: false});\
let t3_$gl_alea =$gl_b1.create('text',[0.3,0,function() {return p3_$gl_alea.Y().toFixed(0)}], {anchor:p6_$gl_alea,anchorX:'middle',fixed:true,color:'blue'});\
let u1_$gl_alea=$gl_b1.create('point',[-1,14],{color:'green',withLabel:false,visible: false});\
let u2_$gl_alea=$gl_b1.create('point',[0,14],{color:'green',withLabel:false,visible: false});\
let polu_$gl_alea = $gl_b1.create('regularpolygon', [u1_$gl_alea, u2_$gl_alea, 4],{fixed:true,fillcolor:'green',borders:{strokeColor:'#e3e3e3',highlight: false,strokeWidth:1,fixed:true},vertices:{face:'',showInfobox:false,withLabel:false,visible:false,fixed:true},highlight: false});\
let e1_$gl_alea=$gl_b1.create('segment',[[-1,13],[0,13]],{fixed:true,strokeColor: 'green',strokeWidth: 2,highlight: false});\
let t1_$gl_alea = $gl_b1.create('text',[0.25,14.5,"1 $gl_unita"],{fixed:true});\
let t4_$gl_alea = $gl_b1.create('text',[0.25,13,"1 $gl_unitl"],{fixed:true});\
let quad_$gl_alea = $gl_b1.create('text',[6,14.5,"Le quadrillage ci-dessous est constitu de carrs."],{fixed:true});\
document.getElementById("unite_$gl_alea").addEventListener("change", function(){\
unitl_$gl_alea = document.getElementById("unite_$gl_alea").value;\
$gl_b1.removeObject(t1_$gl_alea);\
t1_$gl_alea = $gl_b1.create('text',[0.25,14.5,"1 "+unita_$gl_alea.get(unitl_$gl_alea)],{fixed:true});\
$gl_b1.removeObject(t4_$gl_alea);\
t4_$gl_alea = $gl_b1.create('text',[0.25,13,"1 "+unitl_$gl_alea],{fixed:true});\
});\
p2_$gl_alea.on('over',dir2);\
p2_$gl_alea.on('out',out);\
p3_$gl_alea.on('over',dir4);\
p3_$gl_alea.on('out',out);\
$gl_b1.fullUpdate();

<div class="grid-container fluid">
  <div>
    <label for="unite_$gl_alea">Choix de l'unit de longueur&nbsp;:</label>
    <select name="unites" id="unite_$gl_alea" onchange=" ChoixUnite_$gl_alea()">
    !for gl_k=1 to 8
      <option value="$(gl_ens_unitl[$gl_k])"
      !if $gl_k=$gl_choix
        selected
      !endif
      >$(gl_ens_unitl[$gl_k])</option>
    !next gl_k
    </select>
  </div>
  <div class="grid-x grid-padding-x">
    <div id="appletA_$gl_alea" class="cell2 small-12 medium-6 large-6">
      <div class="appletA">
        !readproc slib/geo2D/jsxgraph $gl_id1 $gl_b1,[250x250, center min=250px max=400px scroll],$(gl_script_$gl_alea)
        $slib_out
      </div>
      <div class="rem_$gl_alea">La figure est modifiable en dplaant les points \(\mathrm{$gl_S}\) et <span class="nowrap">\(\mathrm{$gl_T}\).</span>
      </div>
    </div>
    <div id="appletB_$gl_alea" class="cell2 small-12 medium-6 large-6">
      <div class="appletB">
        <div id="txt_$gl_alea"></div>
      </div>
    </div>
  </div>
</div>
<script>
let myText_$gl_alea;
const unita_$gl_alea = new Map();
  !for gl_kkk=1 to 8
    unita_$gl_alea.set("$(gl_ens_unitl[$gl_kkk])","$(gl_ens_unita[$gl_kkk])");
  !next gl_kkk

let unitl_$gl_alea="$gl_unitl";
let unitarea_$gl_alea="$gl_unita";
let unitl_accord_$gl_alea="$gl_unitlaccord";
let unita_accord_$gl_alea="$gl_unitaaccord";
const debMML_$gl_alea='<span class="wims_mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mstyle  mathsize="110%">';
const finMML_$gl_alea='</mstyle></math></span>';

function ChoixUnite_$gl_alea() {
  unitl_$gl_alea = document.getElementById("unite_$gl_alea").value;
  unitarea_$gl_alea =unita_$gl_alea.get(unitl_$gl_alea);
  Accord_$gl_alea(unitl_$gl_alea,unitarea_$gl_alea);
  textDisplay_$gl_alea();
  document.getElementById("txt_$gl_alea").innerHTML = myText_$gl_alea;
}
function Accord_$gl_alea(l,a) {
  unitl_$gl_alea =l;
  unitarea_$gl_alea =a;
  if(unitl_$gl_alea == 'unit de longueur' && p2_$gl_alea.X()*p3_$gl_alea.Y()/2>1){
    unitl_accord_$gl_alea ='units de longueur';
    unita_accord_$gl_alea ="units d\'aire";
  } else {
    unitl_accord_$gl_alea =unitl_$gl_alea;
    unita_accord_$gl_alea =unitarea_$gl_alea;
  };
}
function isIn_$gl_alea(pt,poly) {
  let coor = new JXG.Coords(JXG.COORDS_BY_USER, [pt.X(), pt.Y()], $gl_b1);
  return poly.hasPoint(coor.scrCoords[1], coor.scrCoords[2])
}
function textDisplay_$gl_alea() {
  let area_$gl_alea,ph1_$gl_alea,ph2_$gl_alea,ph4_$gl_alea,ph5_$gl_alea,ph6_$gl_alea,ph7_$gl_alea;
  area_$gl_alea=(p2_$gl_alea.X()*p3_$gl_alea.Y()/2).toString().replaceAll('.', ',');
  type1={
    nature:'<strong>rectangle</strong> en '+debMML_$gl_alea+'<mi mathvariant="normal">$gl_R</mi>'+finMML_$gl_alea,
    cote1:'$gl_R$gl_S',
    cote2:'$gl_T$gl_R'
  };
  type2={
    nature:'<strong>rectangle</strong> en '+debMML_$gl_alea+'<mi mathvariant="normal">$gl_S</mi>'+finMML_$gl_alea,
    cote1:'$gl_R$gl_S',
    cote2:'$gl_T$gl_S'
  };
  type3={
    nature:'de ct '+debMML_$gl_alea+'<mrow><mo fence="true" form="prefix">[</mo><mrow><mrow><mi mathvariant="normal">$gl_R</mi></mrow><mrow><mi mathvariant="normal">$gl_S</mi></mrow></mrow><mo fence="true" form="postfix">]</mo></mrow>'+finMML_$gl_alea+' et de hauteur '+ debMML_$gl_alea+'<mrow><mo fence="true" form="prefix">[</mo><mrow><mrow><mi mathvariant="normal">$gl_T</mi></mrow><mrow><mi mathvariant="normal">H</mi></mrow></mrow><mo fence="true" form="postfix">]</mo></mrow>'+finMML_$gl_alea+' relative  ce ct ',
    cote1:'$gl_R$gl_S',
    cote2:'$(gl_T)H'
  };
  let type;
  switch(true){
    case p1_$gl_alea.X()==p3_$gl_alea.X():
      type=type1;
      break;
    case p2_$gl_alea.X()==p3_$gl_alea.X():
      type=type2;
      break;
    default:
      type=type3;
      break;
  }
  ph1_$gl_alea='Soit '+debMML_$gl_alea+'<mi mathvariant="normal">$gl_R$gl_S$gl_T</mi>'+finMML_$gl_alea+' un <strong>triangle</strong> '+type.nature+' tel que, en '+unitl_$gl_alea+', ';
  ph2_$gl_alea= ((x,y) => debMML_$gl_alea+'<mi mathvariant="normal">'+x+'</mi><mo>=</mo><mn>'+y+'</mn>'+finMML_$gl_alea)(type.cote1,p2_$gl_alea.X());
  ph2_$gl_alea=ph2_$gl_alea+' et <span class="nowrap">'+ ((x,y) => debMML_$gl_alea+'<mi mathvariant="normal">'+x+'</mi><mo>=</mo><mn>'+y+'</mn>'+finMML_$gl_alea)(type.cote2,p3_$gl_alea.Y())+'.</span>';
  const ph3_$gl_alea='Soit '+debMML_$gl_alea+'<mi>A</mi>'+finMML_$gl_alea+' son aire en ';
  ph4_$gl_alea=((x,y) => debMML_$gl_alea+'<mi>A</mi><mo>=</mo><mstyle displaystyle="true"><mfrac><mrow><mi mathvariant="normal">'+x+'</mi><mo>&#xD7;</mo><mi mathvariant="normal">'+y+'</mi></mrow><mn>2</mn></mfrac></mstyle>'+finMML_$gl_alea)(type.cote1,type.cote2);
  ph5_$gl_alea= ((x,y) => debMML_$gl_alea+'<mi>A</mi><mo>=</mo><mstyle displaystyle="true"><mfrac><mrow><mi mathvariant="normal">'+x+'</mi><mo>&#xD7;</mo><mi mathvariant="normal">'+y+'</mi></mrow><mn>2</mn></mfrac></mstyle>'+finMML_$gl_alea)(p2_$gl_alea.X(),p3_$gl_alea.Y());
  ph6_$gl_alea=(x => debMML_$gl_alea+'<mi>A</mi><mo>=</mo><mn>'+x+'</mn>'+finMML_$gl_alea)(area_$gl_alea);
  ph7_$gl_alea='L\'aire du <strong>triangle</strong> '+debMML_$gl_alea+'<mi mathvariant="normal">$gl_R$gl_S$gl_T</mi>'+finMML_$gl_alea+' est gale  ';
  ph8_$gl_alea=(x => debMML_$gl_alea+'<mn>'+x+'</mn>'+finMML_$gl_alea)(area_$gl_alea);
  myText_$gl_alea='<p>'+ph1_$gl_alea+ph2_$gl_alea+'</p><p>'+ph3_$gl_alea+unitarea_$gl_alea+'.</p><p>'+ph4_$gl_alea+'</p><p>'+ph5_$gl_alea+'</p><p>'+ph6_$gl_alea+'</p><p>'+ph7_$gl_alea+ph8_$gl_alea+' '+unita_accord_$gl_alea+'.</p>';
}
document.addEventListener('DOMContentLoaded', function() {
  Accord_$gl_alea(unitl_$gl_alea,unitarea_$gl_alea);
  textDisplay_$gl_alea();
  document.getElementById("txt_$gl_alea").innerHTML = myText_$gl_alea;
})
$gl_b1.on('update', function(){
 if(H_$gl_alea.isOn(p2_$gl_alea) || H_$gl_alea.isOn(p1_$gl_alea) ){
  H_$gl_alea.setAttribute({visible: false});
  } else {
  H_$gl_alea.setAttribute({visible: true});
  }
})
p2_$gl_alea.coords.on('update', function(){
  Accord_$gl_alea(unitl_$gl_alea,unitarea_$gl_alea);
  textDisplay_$gl_alea();
  document.getElementById("txt_$gl_alea").innerHTML = myText_$gl_alea;
})
p3_$gl_alea.coords.on('update', function(){
  if(isIn_$gl_alea(p3_$gl_alea,zone_$gl_alea)==false){
   switch (true) {
    case p3_$gl_alea.Y()>12 && p3_$gl_alea.X()>15:
      p3_$gl_alea.moveTo([15,12]);
      break;
    case p3_$gl_alea.Y()>12 && p3_$gl_alea.X()>=-1 && p3_$gl_alea.X()<=15:
      p3_$gl_alea.moveTo([p3_$gl_alea.X(),12]);
      break;
    case p3_$gl_alea.Y()>12 && p3_$gl_alea.X()<-1:
      p3_$gl_alea.moveTo([-1,12]);
      break;
    case p3_$gl_alea.Y()<1 && p3_$gl_alea.X()>15:
      p3_$gl_alea.moveTo([15,1]);
      break;
    case p3_$gl_alea.Y()<1 && p3_$gl_alea.X()>=-1 && p3_$gl_alea.X()<=15:
      p3_$gl_alea.moveTo([p3_$gl_alea.X(),1]);
      break;
    case p3_$gl_alea.Y()<1 && p3_$gl_alea.X()<-1:
      p3_$gl_alea.moveTo([-1,1]);
      break;
    case p3_$gl_alea.X()>15 :
      p3_$gl_alea.moveTo([15,p3_$gl_alea.Y()]);
      break;
    case p3_$gl_alea.X()<-1:
      p3_$gl_alea.moveTo([-1,p3_$gl_alea.Y()]);
      break;
   }
  }
  if(p3_$gl_alea.X()<=0){
  $gl_b1.removeObject(ang_$gl_alea);
  ang_$gl_alea=$gl_b1.create('angle',[l1_$gl_alea,haut_$gl_alea,-1,1],{name:' ',radius:0.4,color:'blue',highlight:false});
  } else {
  $gl_b1.removeObject(ang_$gl_alea);
  ang_$gl_alea=$gl_b1.create('angle',[haut_$gl_alea,l1_$gl_alea,1,1], {name:' ',radius:0.4,color:'blue',highlight: false});
  }
  Accord_$gl_alea(unitl_$gl_alea,unitarea_$gl_alea);
  textDisplay_$gl_alea();
  document.getElementById("txt_$gl_alea").innerHTML = myText_$gl_alea;
})
</script>
