Monthly Archives: September 2013

Getting Schooled

I’m not sure how much the predators are getting schooled but the prey are using nature’s age old defense mechanism of safety in numbers. So here is a refactored version of a flocking/schooling behavioral model that I had posted on

Also I had a request recently to provide the source code via GitHub. You can now access the source code via the following link.

PredatorPrey Source Code Via GitHub

So I’ve rewritten my predator and prey pursuit JavaScript code. Not to be confused with the predator prey population model. I’ve been intrigued by flocking, schooling, and herding algorithms since running across the Boids web site.

Link to the Boids page.

Ben Dowling at coderholic implemented the Boids behavior model in JavaScript and you can check out his post at the following link.

Link to the JavaScript Boids page.

Basically my code is based on Ben’s code with a predator thrown in to pursue the prey. I was inspired by watching sharks swim through a school of fish as shown the images below. I added the avoidance behavior to the prey’s Boids behavior model. The predator just has a “chase prey” behavior with a basic tendency to stay on the screen, from which the prey also inherit that same behavior.



I migrated my original code from using the Raphaël-JavaScript Graphics Library to using KineticJS which seems to be a little more widely used HTML5 Canvas framework. The grouping behavior might need more work so that the prey don’t occupy the same space. I’d also like to abstract the graphics library in the event it needs to change at some point in the future. I’d also like to visually model the shark and fish a bit more realistically.

Update: It took me some time to get around to this but I finally updated the code for the flocking behavior models.  Previously the avoidance function was not working very well. I spent some time and did some major refactoring, cleaning up the code a bit and fixing the avoidance function.  I’m fairly happy with the results now.

Despite all my rage I am still just a rat in a maze

Wait, … I don’t think I have the lyrics quite right. In any event I thought I’d post a maze solving solution that I came up with after being asked to provide one during an interview.

I was once asked on an interview to come up with a solution to solve a maze. The maze was represented by a matrix of 10 rows by 10 columns.  A value of 0 in a given element in the matrix meant that that coordinate of the maze was open and a value of 1 represented a closed off, or walled, coordinate. Unfortunately I wasn’t able to come up with a solution on the spot. I am notoriously awful at producing source code for solutions, on the spot in an interview situation. I guess I get too nervous or anxious and just don’t tend to do well in those situations.

In any event the problem was interesting enough to me that once I got home and thought about the problem without being stressed for time, coming up with an eloquent solution on the spot, and with the stress of wanting to get a job offer, I did come up with a solution using recursion. Basically I took the maze data in a matrix concept and extended the maze from a 2D matrix to a 3D matrix with the last matrix dimension used to store whether that coordinate was visited or not during the analysis, a value of 1 or 0. Then once the solution was found marking the maze coordinates that are part of the solution path with a value of 2 in the 3rd dimension.

I also did some research on maze generation algorithms and found the recursive backtracker.

Maze generation algorithm: Recursive backtracker

I combined the maze generation algorithm with my maze solver solution and created a solution using JavaScript to create a maze and provide the solution as well.
See the results below.

Update: I recently had a request to make the source code for this available on GitHub. You can now access the source code via the following link.

MazeCreatorSolver Source Code Via GutHub

An engineer is only as good as his or her resources

Early on in my aerospace engineering career I was once told this, (the title of this post) by a more seasoned engineer. Since then I have taken this to heart. I tend to buy books often and I am a firm believer in preparing for a new project before hand. I also don’t think you can fully commit everything to memory but I often find myself remembering a topic I read and then going back to a resource to refresh my memory.

I recently inherited an internal, large scale, JavaScript application at work after one of our front end web developers left for another company. He was a very decent developer but unfortunately the application, or suite of applications had various developers maintaining it over a period of years and the applications had been refactored a few times with no real direction or encompassing architecture in place.

I had previously been predominately a Java developer but I had some limited experience with JavaScript and recently with jQuery. Upon inheriting a large code base in JavaScript and coming to understand the larger design goals from the project manager and stakeholders, I realized I needed to be able to provide the structure and architecture this suite of applications had been missing.

The code base suffered from poorly formatted code to poorly implemented or misused design patterns. There were a lot of opportunities for code reuse and a need for consistency in the user interface as a whole. Also the project build processes needed to be recreated from scratch since significant parts of it did not work and what did work couldn’t be easily automated. The code base did use the Model, View, Controller pattern, but not very well. The view objects contained most of the code for the application logic and the controllers did very little.

Each application in the suite is launched from an encompassing application via an iframe. This gave the applications a lot of flexibility but each application also had its own copy of dependent libraries. Also the iframes would make communication between the applications more difficult.

So in preparation for this task I have read through the following resources and I highly recommend them to anyone who is working on a large scale JavaScript project.

To say the least these reference have provided the tools required to build an architecture for these applications and described the necessary design patterns as they are implemented in JavaScript. I am thoroughly impressed with each of these books. Around 200 pages or less for each, they are a great reference and a quick read to get up to speed on a JavaScript project.

So my first task was to identify parts of the applications that were reused throughout the code base and using the Module Pattern, create reusable modules that could easily be used by any of the applications in the suite. previously these components had been copied and pasted in the code where they were used. They were also inconsistent from place to place and bug fixes had to be applied to multiple locations in the code base.

I have also been decoupling the JavaScript, CSS, HTML, and configuration data. Configuration data for UI components were mixed in with the UI code. Large segments of static HTML were also buried with the JavaScript and CSS styles were applied inconsistently in JavaScript as well. I also created a factory object for creating consistent dialogs. I will do the same for other frequently used UI components.

With these basic reusable pieces in place, any new application in the suite can easily utilize them and the existing applications will be refactored over time to begin using them. Each application will also use the module pattern so that eventually, when all the applications have been refactored they can be included in a new encompassing application using the Sandbox Pattern.

In my previous Java projects we had used CruiseControl and ANT build scripts for continuos integration and automated testing. I really appreciate the peace of mind and constant feedback that CI provides so I plan on using Jenkins CI for continuos integration and create new ANT build scripts for each application.

Coming up next in my reading list are the following. I hope they are as impressive as the previous.

Anyway, I thought I’d share this list of resources in case anyone finds themselves in a similar situation.

A New Blog is Born

So I’ve had a blog at Blogger for some time that was basically for my wife and I to post updates for family and friends.  I would also, on occasion, post some software development related posts.  Unfortunately Blogger makes it quite difficult to post pages with content that uses JavaScript or custom HTML and CSS.  So I’ve created a blog just for my development interests.  I’m using WordPress and hosting through my domain registrar which gives me the flexibility I want for hosting custom pages that use JavaScript.  So here’s hoping that I’ll have some interesting posts in the near future.