/* Copyright (c) 2004-2006, The Dojo Foundation All Rights Reserved. Licensed under the Academic Free License version 2.1 or above OR the modified BSD license. For more information on Dojo licensing, see: http://dojotoolkit.org/community/licensing.shtml */ dojo.provide("dojo.math.curves"); dojo.require("dojo.math"); /* Curves from Dan's 13th lib stuff. * See: http://pupius.co.uk/js/Toolkit.Drawing.js * http://pupius.co.uk/dump/dojo/Dojo.Math.js */ dojo.math.curves = { //Creates a straight line object Line: function(start, end) { this.start = start; this.end = end; this.dimensions = start.length; for(var i = 0; i < start.length; i++) { start[i] = Number(start[i]); } for(var i = 0; i < end.length; i++) { end[i] = Number(end[i]); } //simple function to find point on an n-dimensional, straight line this.getValue = function(n) { var retVal = new Array(this.dimensions); for(var i=0;i= 1) return this.p[this.p.length-1]; // if step>=1 we must be at the end of the curve if(step <= 0) return this.p[0]; // if step<=0 we must be at the start of the curve var retVal = new Array(this.p[0].length); for(var k=0;j= this.p.length) i1 = this.p.length-1; var i2 = node+2; if(i2 >= this.p.length) i2 = this.p.length-1; var u = progress; var u2 = progress*progress; var u3 = progress*progress*progress; var retVal = new Array(this.p[0].length); for(var k=0;k 2D point for center of arc // radius => scalar quantity for radius of arc // start => to define an arc specify start angle (default: 0) // end => to define an arc specify start angle CenteredArc : function(center, radius, start, end) { this.center = center; this.radius = radius; this.start = start || 0; this.end = end; this.getValue = function(n) { var retVal = new Array(2); var theta = dojo.math.degToRad(this.start+((this.end-this.start)*n)); retVal[0] = this.center[0] + this.radius*Math.sin(theta); retVal[1] = this.center[1] - this.radius*Math.cos(theta); return retVal; } return this; }, // Special case of Arc (start = 0, end = 360) Circle : function(center, radius) { dojo.math.curves.CenteredArc.call(this, center, radius, 0, 360); return this; }, Path : function() { var curves = []; var weights = []; var ranges = []; var totalWeight = 0; this.add = function(curve, weight) { if( weight < 0 ) { dojo.raise("dojo.math.curves.Path.add: weight cannot be less than 0"); } curves.push(curve); weights.push(weight); totalWeight += weight; computeRanges(); } this.remove = function(curve) { for(var i = 0; i < curves.length; i++) { if( curves[i] == curve ) { curves.splice(i, 1); totalWeight -= weights.splice(i, 1)[0]; break; } } computeRanges(); } this.removeAll = function() { curves = []; weights = []; totalWeight = 0; } this.getValue = function(n) { var found = false, value = 0; for(var i = 0; i < ranges.length; i++) { var r = ranges[i]; //w(r.join(" ... ")); if( n >= r[0] && n < r[1] ) { var subN = (n - r[0]) / r[2]; value = curves[i].getValue(subN); found = true; break; } } // FIXME: Do we want to assume we're at the end? if( !found ) { value = curves[curves.length-1].getValue(1); } for(var j = 0; j < i; j++) { value = dojo.math.points.translate(value, curves[j].getValue(1)); } return value; } function computeRanges() { var start = 0; for(var i = 0; i < weights.length; i++) { var end = start + weights[i] / totalWeight; var len = end - start; ranges[i] = [start, end, len]; start = end; } } return this; } };