diff --git a/script.js b/script.js index 7a25136..df94f79 100644 --- a/script.js +++ b/script.js @@ -49,9 +49,17 @@ function draw() { else rect((cWidth-(cWidth*r))/2,0,cWidth*r,cHeight); break; case 'triangle': - textSize(32); - fill(255,0,0); - text('Not yet implemented', cWidth/3, cWidth/2); + fill(color(255,255,100)); + let tw = parseFloat($('#triangle_base').val()); + let th = parseFloat($('#triangle_height').val()); + let size=500; + let ctx = canvas.getContext('2d'); + ctx.beginPath(); + ctx.moveTo(cWidth/10*tw/2, 0); + ctx.lineTo(cWidth/10*tw, cHeight/10*th); + ctx.lineTo(0, cHeight/10*th); + ctx.lineTo(cWidth/10*tw/2, 0); + ctx.fill(); break; default: break; @@ -89,6 +97,16 @@ function solve() { ratio = evaluateRatioRectangle(rect_width, rect_height, n); trueArea = rect_width*rect_height; foundArea = Math.pow(max(rect_width, rect_height),2)*ratio; + //TODO : généraliser foundArea avec un truc genre intSizeOfSquare + break; + case 'triangle': + let base = parseInt($('#triangle_base').val()); + let height = parseInt($('#triangle_height').val()); + ratio = evaluateRatioTriangle(base, height); + trueArea = 0.5 * base * height; + let halfBase = base/2; + let slanted = Math.sqrt( Math.pow(halfBase, 2) + Math.pow(height, 2) ); + foundArea = ratio*trueArea*2; break; } precisionInPercent = (1-Math.abs(trueArea-foundArea)/trueArea)*100; @@ -117,6 +135,21 @@ function evaluateRatioRectangle(rect_width, rect_height){ return nOfIn/points.length; } +function evaluateRatioTriangle(base, height){ + //TODO: this only works for equal triangle + let nOfIn=0; + let rct = Math.abs((base/2))/height; + for(let i=0;i