0.1 Release Day!

So I’ve fallen back into old bad habits… basically, my previously closed source ways.  I procrastinated and failed to follow the mantra:  “Release early, release often”.  I did finish my object, without too much testing done, however.  I’ll give you some sample code from the actual object and then upload the code itself at the end of this post.  Despite my procrastination, I was able to finish the object in about two days.  This includes all the research for Javascript and some of the vector math needed to be implemented.  Now to show off some of my code work…

function PVector(){

	this.x = 0;
	this.y = 0;
	this.z = 0;

	if (arguments.length == 3){
		this.set(arguments[0], arguments[1], arguments[2]);
	}
	else if (arguments.length == 2){
		this.set(arguments[0], arguments[1], 0);
	}

	return this;

}

That was the main body of the PVector object.  Now, it looks a little horrible due to the formatting removal but it’ll have to do.  Now you know it’s an object, instead of a regular function, through the variables being created with the “this” command.  The variables are pretty self explanatory due to the nature of vectors.  The second part of the object are constructors or some type of overloaded constructors.  There is not a way to actually put different types of parameters into functions with the same name (which is ludicrous in my opinion because it’s a functional programming language).  However, I was able to work around that with the help of David Humphrey and Anna Sobiepanek.  They were discussing the very same thing in the #processing.js channel and Anna was kind enough to put up her findings in this post.  You will find a great many of the methods within this object require the system of using arguments because most of them are originally overloaded for many different uses.  In particular, I’ll show you one of the methods I struggled with… the mult() method.

PVector.prototype.mult = function(){

	if (arguments.length == 1){
		if (typeof (arguments[0]) == 'number'){
			this.x *= arguments[0];
			this.y *= arguments[0];
			this.z *= arguments[0];
		}
		else if (typeof (arguments[0]) == 'object'){
			var v = new PVector();
			v = arguments[0];
			this.x *= v.x;
			this.y *= v.y;
			this.z *= v.z;
		}
	}
	else if (arguments.length == 2){
		if (typeof (arguments[1]) == 'number'){
			var v = new PVector();
			v = arguments[0];
			v.mult(arguments[1]);
			return v;
		}
		else if (typeof (arguments[1]) == 'object'){
			var v1 = new PVector();
			var v2 = new PVector();
			v1 = arguments[0];
			v2 = arguments[1];
			v1.mult(v2);
			return v1;
		}
	}

}

I took some time to try and format this (I don’t know if it’ll work once I actually post it though), so that it’ll be somewhat easier to read.  Now, there were two main issues of concern for me while doing this method.  One of them was the fact of determining what kind of parameter was being passed through to the function.  This was due to the mult() function having the same number of parameters for different values.  I had to insert a nested if statement that would somehow figure out the type of parameter being passed.  I, initially, had no clue as to how to do this.  Fortunately, asking in IRC helped.  Our newest member, Daniel Hodgin, helped point me to the right direction of the typeof operator.  I, also, found a good example of usage at this site.  Now that I have that resolved, implementation wasn’t too difficult.  I, actually, had the same problem with the set() method but I decided to leave that one later and move on… but once I figured it out for mult, set was pretty simple as well.

Now, the second problem I came across is the fact that in the mult description (linking here again so you don’t have to scroll up) they were asking for two vectors to be multiplied into each other.  My first thought was “How is that even possible?”  A few years in engineering taught me that vector multiplication didn’t exist, aside from multiplying a scalar into a vector.  Research into the subject gave me the same thing.  All I kept finding were how-to’s on dot product and cross product.  Which, in the end, wasn’t what Processing was asking.  It took for me to look into the original Processing PVector source code to make sense of what was being asked.  Now, I may not be fond of what was being done, but I did it anyway because we’re only supposed to be transporting Processing to Javascript… not modifying it.  Basically, it asked to multiply each component of the vector into the components of the second vector (ie. x with x and y with y…)  to come up with a new vector.

After overcoming those two problems, the rest of the methods were pretty simple.  Some of them were as easy as a few lines like the following:

PVector.prototype.mag = function(){

	return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);

}

However, through looking at the source code, I found out that the references on the Processing site were a little outdated.  It wasn’t updated with some of the further overloaded functions that I saw and didn’t really have time to implement (ie the toString method).  I will implement these and fix any bugs found for the 0.2 release as well as taking up some more functions that need implementing.

I promised to have my source code uploaded, but I’m having trouble putting it up to Matrix at this point… so I will put it up in the near future.  As well, I will put up some sort of working demo for this as soon as I figure that out as well.  It’ll be soon, I promise.  One thing I can say about me working… once I DO eventually get started, I keep going until I have no more fight in me left.

*EDITED to make formatting easier to read.

Advertisements

One Response to 0.1 Release Day!

  1. Pingback: More like release week! « The Crazed Mind of an Aspiring Developer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: