{"id":9941,"date":"2025-09-10T09:42:03","date_gmt":"2025-09-10T07:42:03","guid":{"rendered":"https:\/\/spgoo.org\/?page_id=9941"},"modified":"2025-09-10T09:42:03","modified_gmt":"2025-09-10T07:42:03","slug":"methode-de-monte-carlo","status":"publish","type":"page","link":"https:\/\/spgoo.org\/?page_id=9941","title":{"rendered":"M\u00e9thode de Monte-Carlo"},"content":{"rendered":"  <!-- JSXGraph -->\r\n    <link rel=\"stylesheet\" href=\"https:\/\/cdn.jsdelivr.net\/npm\/jsxgraph\/distrib\/jsxgraph.css\">\r\n  <!-- <script src=\"https:\/\/cdn.jsdelivr.net\/npm\/jsxgraph\/distrib\/jsxgraphcore.js\"><\/script> -->\r\n    <script src=\"https:\/\/cdn.jsdelivr.net\/npm\/jsxgraph@1.11.1\/distrib\/jsxgraphcore.js\"><\/script>\r\n  <!-- MathJax -->\r\n  <script>\r\n    window.MathJax = {\r\n      tex: { inlineMath: [['\\\\(', '\\\\)'], ['$', '$']] },\r\n      svg: { fontCache: 'global' }\r\n    };\r\n  <\/script>\r\n  <script src=\"https:\/\/cdn.jsdelivr.net\/npm\/mathjax@3\/es5\/tex-svg.js\"><\/script>\r\n\r\n  <style>\r\n\r\n    #boxA, .jxgboxA {\r\n      width: 600px;\r\n      height: 600px;\r\n      border: none;\r\n    }\r\n    #boxB, .jxgboxB {\r\n      width: 400px;\r\n      height: 600px;\r\n      border: none;\r\n    }\r\n    .board-container {\r\n      display: flex;\r\n      justify-content: center;\r\n      align-items: flex-start;\r\n      gap: 20px;\r\n      padding: 20px;\r\n    }\r\n\r\n   #resetBtn {\r\n      margin: 10px;\r\n      padding: 6px 12px;\r\n      border-radius: 6px;\r\n      border: none;\r\n      background: #AE181E;\r\n      color: white;\r\n      cursor: pointer;\r\n    }\r\n    #resetBtn:hover {\r\n      background: #7d1014;\r\n    }\r\n\r\n  <\/style>\r\n<h1>M\u00e9thode de Monte-Carlo: \u00e9valuation de surface <\/h1>\r\n<div class=\"board-container\">\r\n  <div>\r\n    <div id=\"boxA\" class=\"jxgbox jxgboxA\"><\/div>\r\n    <button id=\"resetBtn\">\u27f2 Reset<\/button>\r\n  <\/div>\r\n  <div id=\"boxB\" class=\"jxgbox jxgboxB\"><\/div>\r\n<\/div>\r\n\r\n<script>\r\n    let boardA=null;\r\n\/\/ --- BoardA ---\r\n\/*let boardA = JXG.JSXGraph.initBoard('boxA', {\r\n  boundingbox: [-5, 5, 5, -5],\r\n  axis: true,\r\n  showNavigation: false,\r\n  showCopyright: false,\r\n  showFullscreen: true,\r\n  showInfobox:false,\r\n  keepAspectRatio: true\r\n});\r\nboardA.renderer.container.style.backgroundColor = '#C3ADC3';\r\n*\/\r\n\/\/ Grille de d\u00e9part\r\nlet intLabel, intValue;\r\nlet borderLabel, borderValue;\r\nlet pickFormula;\r\n\r\n\/\/ --- BoardB ---\r\nconst boardB = JXG.JSXGraph.initBoard('boxB', {\r\n  boundingbox: [-5, 5, 5, -5],\r\n  grid: false,\r\n  axis: false,\r\n  showNavigation: false,\r\n  showCopyright: false,\r\n  showFullscreen: true,\r\n  showInfobox:false,\r\n  keepAspectRatio: true\r\n});\r\nboardB.renderer.container.style.backgroundColor = '#C3ADC3';\r\n\r\n\r\nlet statsText = null; \/\/ Texte des statistiques sur boardB\r\nlet nbObus=1000;\r\n\/\/ Choix du pas\r\n\/\/ --- Treillis + Interaction ---\r\nlet pointsObus = [];\r\nlet selectedPoints = [];  \r\nlet insideCount = 0;\r\nlet borderCount = 0;\r\nlet POLY=null;\r\n\r\nconst sld0=boardB.create(\"slider\",[[-4,0],[-1,0],[250,250,2000]],{snapWidth: 250});\r\nsld0.on(\"drag\",function() {nbObus=sld0.Value()});\r\n\r\n\r\nconst affiche_resultat=function() {\r\n    \/\/ Compter points int\u00e9rieurs et sur le bord  \r\n    const inside = countPointsPolygon(POLY);\r\n    insideCount = inside;\r\n    borderCount = nbObus;\r\n\r\n    if (intLabel) boardB.removeObject(intLabel);\r\n    if (intValue) boardB.removeObject(intValue);\r\n    if (borderLabel) boardB.removeObject(borderLabel);\r\n    if (borderValue) boardB.removeObject(borderValue);\r\n    if (pickFormula) boardB.removeObject(pickFormula);\r\n\r\n    intLabel = boardB.create('text', [-4.5, 4.5, `\\\\(\\\\textrm{I = Points Int\u00e9rieurs = }\\\\)`], {\r\n      fontSize: 14, anchorX: 'left', anchorY: 'top', useMathJax: true\r\n    });\r\n    intValue = boardB.create('text', [0, 4.5, `\\\\(${insideCount}\\\\)`], {\r\n      fontSize: 14, anchorX: 'left', anchorY: 'top', useMathJax: true\r\n    });\r\n    \r\n    pickFormula = boardB.create('text', [0, 1.5, `\\\\(A = Aire*indice\/${nbObus} = ${100*insideCount\/nbObus}\\\\)`], {\r\n      fontSize: 18, anchorX: 'middle', anchorY: 'middle', useMathJax: true, color: \"#AE181E\"\r\n    });    \r\n\r\n    \/\/ R\u00e9initialisation\r\n    boardB.update();\r\n}\r\nfunction isPointInPoly(pt,po) { \r\n  let c = new JXG.Coords(JXG.COORDS_BY_USER, [pt.X(), pt.Y()], boardA);\r\n  return po.hasPoint(c.scrCoords[1], c.scrCoords[2])\r\n}\r\n\r\n\/\/ Compte points int\u00e9rieurs et sur le bord\r\nfunction countPointsPolygon(polygon) {\r\n  let insideTot = 0;\r\n  pointsObus.forEach(pt=> {\r\n      if (isPointInPoly(pt,POLY)) {\r\n       \tinsideTot++;\r\n      }\r\n  });\r\n  return insideTot ;\r\n}\r\n\r\nfunction generateBoard(pas) {\r\n  if (boardA!=null) {\r\n     JXG.JSXGraph.freeBoard(boardA);\r\n  }\r\n  boardA = JXG.JSXGraph.initBoard('boxA', {\r\n    boundingbox: [-5, 5, 5, -5],\r\n    axis: false,\r\n    showNavigation: false,\r\n    showCopyright: false,\r\n    showFullscreen: true,\r\n    showInfobox:false,\r\n    keepAspectRatio: true\r\n  });\r\n  boardA.renderer.container.style.backgroundColor = '#C3ADC3';\r\n  boardA.on('down',down);boardA.on('up', up);\r\n  \/\/boardA.update();\r\n}\r\n\r\ndocument.getElementById('resetBtn').addEventListener('click', () => {\r\n\tActif_exemple=false;\r\n    selectedPoints=[];\r\n    generateBoard(1);\r\n    trace=false;\r\n    pointsObus=[];\r\n});\r\n\r\nthpick = boardB.create('text', [0, 6.7, () =>  `\\\\(\\\\textrm{Th\u00e9or\u00e8me MonteCarlo}\\\\)`\r\n], {\r\n  fontSize: 16,\r\n  anchorX: 'middle',\r\n  anchorY: 'middle',\r\n  useMathJax: true,\r\n  color :'#AE181E'  \r\n});\r\n\r\n\r\nfunction randomIntFromInterval(min, max) { \/\/ min and max included \r\n  return Math.random() * (max - min + 1) + min;\r\n}\r\n\r\n    \/\/ generation des points de fa\u00e7on random sur le boardA\r\n  const genere_points=function() {\r\n        for (i=0;i<nbObus;i++) {\r\n            let x= randomIntFromInterval(-5,5);\r\n            let y= randomIntFromInterval(-5,5);\r\n            let ppObus = boardA.create('point', [x, y],{name:'',strokeColor:\"green\"});\r\n            pointsObus.push(ppObus);\r\n        }\r\n        affiche_resultat();\r\n  }\r\n  \r\n  boardB.create('button', [-4,-3.5,'Activer', function() {genere_points();} ],{cssClass:\"btnEx\",highlightCssClass: 'btnEx'});\r\n\r\n  boardB.create('text', [-4.5,-4.5, () => `\\\\(A= \\\\textrm{(aire du polygone)}\\\\)`]);\r\n  boardB.create('text', [-4.5,-5, () => `\\\\(\\\\textrm{(Cliquez sur un premier point, un second... et refermer}\\\\)`]);\r\n  boardB.create('text', [-4.5,-5.5, () =>`\\\\(\\\\textrm{le polygone en recliquant sur le premier point. On obtient}\\\\)`]);\r\n  boardB.create('text', [-4.5,-6, () => `\\\\(\\\\textrm{l'aire du polygone par la formule de Pick. En cas de doute,}\\\\)`]);\r\n  boardB.create('text', [-4.5,-6.5, () =>`\\\\(\\\\textrm{commencer par un rectangle, un carr\u00e9, un triangle...)}\\\\)`]);  \r\n\r\nconst trace_polygon=function(tabp){\r\n    POLY=boardA.create('polygon',tabp,{hasInnerPoints:true});\r\n    let grp=boardA.create('group',tabp);\r\n};\r\n\r\nvar getMouseCoords = function(e, i) { \r\n    var cPos = boardA.getCoordsTopLeftCorner(e, i), absPos = JXG.getPosition(e, i), dx = absPos[0]-cPos[0], dy = absPos[1]-cPos[1]; \r\n    return new JXG.Coords(JXG.COORDS_BY_SCREEN, [dx, dy], boardA); \r\n}\r\n    \r\nconst parent={};\r\nlet trace=false;\r\nparent.mode=\"Polygon\";\r\nfunction down(e) { \r\n    e.preventDefault();\r\n    if (trace) return; \r\n    var canCreate = true, i, coords, el; \r\n    if (e[JXG.touchProperty]) { i = 0; };\r\n    coords = getMouseCoords(e, i); \r\n    for (el in boardA.objects) { \r\n        if (JXG.isPoint(boardA.objects[el]) && boardA.objects[el].hasPoint(coords.scrCoords[1], coords.scrCoords[2])) {\r\n            canCreate = false; \r\n            selectedPoints.push(el); \r\n            trace=true;\r\n            break; \r\n        } \r\n    };\r\n    if (canCreate) { \r\n        let pp = boardA.create('point', [coords.usrCoords[1], coords.usrCoords[2]],{name:''});\r\n        selectedPoints.push(pp);\r\n        trace=false;\r\n    }else {\r\n        trace_polygon(selectedPoints);\r\n    }\r\n  }\r\n\r\n\r\nfunction up(e) { \r\n    \/*if (boardA.selectionPolygon) {\r\n        console.log('fonction up',boardA.selectionPolygon);\r\n        boardA.selectionPolygon.visProp.visible=true;\r\n        boardA.update();\r\n    }*\/\r\n};\r\n\r\n\/\/ G\u00e9n\u00e9ration initiale\r\ngenerateBoard(1);\r\n\r\n\r\n<\/script>\r\n\r\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-9941","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/spgoo.org\/index.php?rest_route=\/wp\/v2\/pages\/9941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/spgoo.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/spgoo.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/spgoo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/spgoo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9941"}],"version-history":[{"count":1,"href":"https:\/\/spgoo.org\/index.php?rest_route=\/wp\/v2\/pages\/9941\/revisions"}],"predecessor-version":[{"id":9942,"href":"https:\/\/spgoo.org\/index.php?rest_route=\/wp\/v2\/pages\/9941\/revisions\/9942"}],"wp:attachment":[{"href":"https:\/\/spgoo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}