/ Published in: JavaScript
Expand |
Embed | Plain Text
<!DOCTYPE HTML> <html> <head> <title>Grapher</title> </head> <body> <canvas id="cvs" width="500" height="500"></canvas> <form onsubmit="loadF(); return false;"><input type="text" id="f"><input type="submit"></form> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script> <script type="text/javascript"> function loadF() { plotF( function(x) { var e = Math.exp(1), sin = Math.sin, cos = Math.cos, tan = Math.tan, parse = $('#f').val().replace(/([0-9])([a-zA-Z])/g, "$1*$2") //Change coeff to multiplication .replace(/([a-zA-Z0-9])\^(\S*?)x(\s|$)/g, "Math.pow($1, $2"+x+")$3") //Exponential Function .replace(/x\^(\S*?)(\s|$)/g, "Math.pow("+x+", $1)$2") //Polynomial Function .replace(/x/g, x); //Insert x console.log(parse); return eval(parse); }, document.getElementById('cvs'), { xMin: -8, xMax: 8, yMin: -8, yMax: 8 }); } function plotF( f, elem, scaleWindow ) { elem.width = elem.width; var scaleWindow = scaleWindow || { xMin: -25, xMax: 25, yMin: -25, yMax: 25 }; scaleWindow.yMax = [-scaleWindow.yMin, (scaleWindow.yMin = -scaleWindow.yMax)][0]; //Swap Max/Min to make more intuitive var pdy = elem.height/(scaleWindow.yMax-scaleWindow.yMin), pdx = elem.width/(scaleWindow.xMax-scaleWindow.xMin); axies = { y: (elem.width*(0-scaleWindow.xMin)/(scaleWindow.xMax-scaleWindow.xMin)), x: (elem.height*(0-scaleWindow.yMin)/(scaleWindow.yMax-scaleWindow.yMin)) }; // Always check for properties and methods, to make sure your code doesn't break // in other browsers. if (elem && elem.getContext) { // Get the 2d context. // Remember: you can only initialize one context per element. var context = elem.getContext('2d'); if (context) { context.beginPath(); //Axis Style context.strokeStyle = '#000'; context.lineWidth = 1; //y-axis context.moveTo( axies.y, 0 ); context.lineTo( axies.y, elem.height ); //x-axis context.moveTo( 0, axies.x ); context.lineTo( elem.width, axies.x ); console.log("Function exists"); context.stroke(); context.closePath(); context.beginPath(); //Plot style context.strokeStyle = '#f00'; context.lineWidth = 3; console.log("Start: ", axies.y/elem.width * -50 ); for( var i = axies.y/elem.width * -50; i < (elem.width-axies.y)/elem.width * 50; i++ ) { var fraction = function(n) { return (n - axies.y/elem.width * -50)/50; }, xMin = scaleWindow.xMin, xRange = scaleWindow.xMax - scaleWindow.xMin, yRange = scaleWindow.yMax - scaleWindow.yMin; context.moveTo(fraction(i)*elem.width, axies.x-f(xMin+fraction(i)*xRange)/yRange*elem.height); // give the (x,y) coordinates context.lineTo(fraction(i+1)*elem.width, axies.x-f(xMin+fraction(i+1)*xRange)/yRange*elem.height); } context.stroke(); context.closePath(); } } } </script> </body> </html> <!--<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script> <script type="text/javascript"> function process(str) { return eval(str); } function sanitize_reg(str) { return str.replace(/[\\\.\+\*\?\^\$\[\]\(\)\{\}\/\'\#\:\!\=\|]/ig, "\\$&"); } function evaluate_parenthetic(str) { arr = str.match(/[(][^)a-zA-Z]+[)]/g); for( key in arr ) { val = arr[key]; reg = RegExp( sanitize_reg(val), 'g' ); str = str.replace(reg, process(val)); } return str; } function main() { var inputValue = $('#expression').val(); inputValue = inputValue.replace(/(x)\^([0-9]+)/g, "1/($2+1) * $1^($2+1)") //Power rule .replace(/([0-9]*?)(x[^^])|x$/g, '$1/2 * x^2 ') //Plain x .replace(/(\s|^)([0-9]+)(\s|$)/g, '$2x ') //Plain constant .replace(/\s\//g, ' 1/'); //Fix lack of coeff with x console.log(inputValue); inputValue = evaluate_parenthetic(inputValue); $('#resp').text(inputValue); } </script> </head> <body><form onsubmit="main(); return false;"> <input type="text" id="expression"> <input type="submit"> </form><br> <span id="resp"></span> </body> </html>-->
You need to login to post a comment.
