Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<points.length;i++){
let x = points[i][0];
let y = points[i][1];
let rcp = Math.abs((x-0.5))/y;
if (rcp < rct && y < height) {
nOfIn++;
}
}
return nOfIn/points.length;
}

function generateRandomPoints(n){ //The points are generated within [0;1[ interval for both x and y coords
for(let i=0;i<n; i++)
points.push([(Math.random()), (Math.random())]);
Expand Down Expand Up @@ -224,5 +257,7 @@ $(document).ready(function(){
$('#radius_number').on('change', function() {cleanResultsAndPoints();});
$('#side_a').on('change', function() {cleanResultsAndPoints();});
$('#side_b').on('change', function() {cleanResultsAndPoints();});
$('#triangle_base').on('change', function() {cleanResultsAndPoints();});
$('#triangle_height').on('change', function() {cleanResultsAndPoints();});
updateFields();
})