Open main menu

CDOT Wiki β

Changes

User:Dhhodgin

2,802 bytes added, 22:28, 27 October 2009
code blocks
==== shorten() ====
Specification for shorten() in processing [http://processing.org/reference/shorten_.html here].<br />
Arrays in JS have no type , the elements in them can contain any type and do not all have to match. Arrays are also passed by reference which means a reference to the object is passed in not the entire object. so my code creates a new array and then copies the passed in array first and then pops one element off the new array and the newary object is returned. This is built to accept a processing type of String, int, boolean, char, byte, and float. Support for arrays of objects will be added in 0.2.
p.shorten = function( ary ) {
Example of this function and test is [http://matrix.senecac.on.ca/~dhhodgin/dps909/shorten_test.html here].<br />
'''Known issues:''' This has not been tested with arrays of objects. I'm assuming it will copy object elements in an array by reference and not produce a proper deep copy. I plan to fix this by 0.2. (confirmed, needs deep copy support for arrays of objects). ==== expand() ====Specification for expand() in processing [http://processing.org/reference/expand_.html here].<br />Expand takes an array as its argument and returns a copy of the array with its length doubled. There is an optional 2nd parameter to specify the new size of the array as well.  p.expand = function( ary, newSize ) { var newary = new Array(); for ( var i = 0; i < ary.length; i++ ) { newary[ i ] = ary[ i ]; } if (arguments.length == 1) { // double size of array newary.length *= 2; } else if (arguments.length == 2) { // size is newSize newary.length = newSize; } return newary; } Example of this function and test is [http://matrix.senecac.on.ca/~dhhodgin/dps909/expand_test.html here].<br />'''Known issues:''' Not yet tested with arrays of objects. ==== unhex() ====Specification for shorten() in processing [http://processing.org/reference/unhex_.html here].<br />unhex takes a string representing a 8 digit hex code as its only argument and returns an int representation of the string. JavaScript supports 64 bit floats as var's so it took a little number crunching to make it output an exact replication of the Java implementation with signed int's.  p.unhex = function( str ) { var value = 0; var multiplier = 1; var num = 0; for (var i = str.length-1; i >= 0; i--){ try{ switch(str[i]){ case "0": num = 0; break; case "1": num = 1; break; case "2": num = 2; break; case "3": num = 3; break; case "4": num = 4; break; case "5": num = 5; break; case "6": num = 6; break; case "7": num = 7; break; case "8": num = 8; break; case "9": num = 9; break; case "A": case "a": num = 10; break; case "B": case "b": num = 11; break; case "C": case "c": num = 12; break; case "D": case "d": num = 13; break; case "E": case "e": num = 14; break; case "F": case "f": num = 15; break; default:return 0; break; } value += num * multiplier; multiplier *= 16; }catch(e){;} // correct for int overflow java expectation if (value > 2147483647) { value -= 4294967296; } } return value; } Example of this function and test is [http://matrix.senecac.on.ca/~dhhodgin/dps909/unhex_test.html here].<br />
== Week 1 stuff ==
1
edit