Notes:
Computers are great at doing Monte Carlo Simulations. In this app, we use that technique to compute the area of the red disk and then compare our result to that found with a traditional area calculation.
To see the results, we just open the Chrome Development area (Shift-Control-J) and the results will be there, thanks to our trusty 'console.log' function.
Features:
| Comments: |
We used some 'utility' methods to help us do our work. They are listed below for your convenience. |
|---|---|
| Even More: |
function isPointInColor(pt, colr){
var thePixel = new TNTPixel(myTNTCanvas, UL, LR, pt);
Debugger.log(pt.x + ", " + pt.y + "..." + thePixel.px + ", " + thePixel.py);
var myPixelData =
myTNTCanvas.context.getImageData(Math.floor(thePixel.px), Math.floor(thePixel.py), 1, 1).data;
var theRedTint = myPixelData[0];
var theGreenTint = myPixelData[1];
var theBlueTint = myPixelData[2];
Debugger.log(theRedTint);
if(theRedTint == colr.rt && theGreenTint == colr.gt && theBlueTint == colr.bt) return true;
else return false;
}//end isPointInColor
function randomCanvasPoint(){
var x,y;
x = (XMAX-XMIN)*Math.random() + XMIN;
y = (YMAX-YMIN)*Math.random() + YMIN;
return new TNTPoint(x,y);
}//end randomCanvasPoint
function doesPointLandInTNTColor(pt, tcacolr){
var thePixel = new TNTPixel(myTNTCanvas, UL, LR, pt);
var myPixelData =
myTNTCanvas.context.getImageData(thePixel.px, thePixel.py, 1, 1).data;
var theRedTint = myPixelData[0];
var theGreenTint = myPixelData[1];
var theBlueTint = myPixelData[2];
if(theRedTint == tcacolr.rt &&
theGreenTint == tcacolr.gt &&
theBlueTint == tcacolr.bt) return true;
else return false;
}//end doesPointLandInTNTColor
function doesPointLandInTNTColors(pt, tcacolr1, tcacolr2){
var thePixel = new TNTPixel(myTNTCanvas, UL, LR, pt);
var myPixelData =
myTNTCanvas.context.getImageData(thePixel.px, thePixel.py, 1, 1).data;
var theRedTint = myPixelData[0];
var theGreenTint = myPixelData[1];
var theBlueTint = myPixelData[2];
if(theRedTint == tcacolr1.rt &&
theGreenTint == tcacolr1.gt &&
theBlueTint == tcacolr1.bt) return true;
else if(theRedTint == tcacolr2.rt &&
theGreenTint == tcacolr2.gt &&
theBlueTint == tcacolr2.bt) return true;
else return false;
}//end doesPointLandInTNTColors
function calculateCanvasArea(){
return (XMAX-XMIN)*(YMAX-YMIN);
}//end calculateCanvasArea
|
