1 
  2 
  3 /*
  4 Create our svg stub.
  5 foreach function convert to path
  6 if polynomial rank greater than 3 do nothing
  7 convert function to parametric and scale for t=[0,1]
  8 then take coefficient and plug them into matrices and solve for control points
  9 scale all points to container.
 10 */
 11 /**
 12 	A class for generating SVG elements representing functions
 13 	@class
 14 	@constructor
 15 	@param {integer} h The height in pixels of the element
 16 	@param {integer} w The width in pixels of the element
 17 	@param {Range} xr The range of x values represented in the graph. 
 18 	@param {Range} yr The range of the y values represented 
 19 **/
 20 function SVG(h, w, g) {
 21 	this.height = h;
 22 	this.width = w;
 23 	this.funcs = [];
 24 	this.graph = g;
 25 
 26 }
 27 
 28 SVG.prototype = {
 29 	/**
 30 		Store functions to be drawn.
 31 		@param {[Polynomial | PiecewiseFunction]} curves Array of Polnomials and or Piecewise Functions
 32 	**/
 33 	addCurves: function(curves) {
 34 		//test if curve is piecewise or other
 35 		//if so generate parametric expression
 36 		//if quadratic or cubic find control points
 37 		//generate xml and attach to object
 38 		for(var i =0; i < curves.length; i++) {
 39 			console.log("yes")
 40 			var fn = curves[i];
 41 			if(fn instanceof PiecewiseFunction) {
 42 				console.log("Better");
 43 				var unitspacepoints = fn.generateBezierPaths();
 44 				var pixelspacepoints = [];
 45 				for(var j=0; j < unitspacepoints.length; j++) {
 46 					var newps = [];
 47 					for(var k=0; k < unitspacepoints[j].length; k++) {
 48 						newps.push(this.graph.pointToPixel(unitspacepoints[j][k]));
 49 					}
 50 					pixelspacepoints.push(newps);   
 51 				}
 52 				this.funcs.push(pixelspacepoints);
 53 			}else if(fn instanceof Polynomial) {
 54 				console.log("?");	
 55 			}else{
 56 				//what the heck!
 57 				console.log("!!?#*$%");	
 58 			}
 59 		}
 60 		//if other test if degree <= 3
 61 
 62 
 63 	},
 64 
 65 	/**
 66 		Generates the SVG XML
 67 
 68 	**/
 69 	toXML: function() {
 70 		//for each funcc append xml to xml string
 71 		var xml = '<svg version="1.1" baseProfile="full" width="'+canvas.width+'" height="'+canvas.height+'" xmlns="http://www.w3.org/2000/svg">';
 72 		for(var i = 0; i < this.funcs.length; i++ ) {
 73 			for(var j = 0; j< this.funcs[i].length; j++) {
 74 				var cv = this.funcs[i][j];
 75 				xml +='<path d="M '+cv[0].x+' '+cv[0].y+' Q '+cv[1].x+' '+cv[1].y+', '+cv[2].x+' '+cv[2].y+'" stroke="orange" fill="transparent"/>';  
 76 			}
 77 		}
 78 		return xml+'</svg>';
 79 	},
 80 }
 81