Righty-ho chaps, here's the situation:
I'm creating a program that simulates traffic flow. I'm currently implementing speed limit zones. A "Limit" object (below) contains the value for the speed and a value for the distance along the simulation at which this limit comes into effect (so a Limit with speed = 50 and pos = 500 would be a 50m/s limit starting at the 500m point in the simulation).
A Vehicle object contains, amongst other things, a value "X", representing the distance along the simulation. I intend to use Vehicle.X to check the limit at which the vehicle should be travelling.
The class LimitSet extends Vector as a collection of Limit objects. My problem lies within the method checkLimit. This method accepts a Vehicle object and uses its "X" value to determine what limit should be applied. It does this by running through all the Limit objects currently stored (in ascending order of "pos", with LimitSet[0].pos = 0) and returning the speed value of the approriate Limit object.
It first checks to see if there is more than one Limit object in the collection - if not then there is no need to perform any calculations and it simply returns the single limit. If there
are multiple Limits to check then it loops through until the right one if found.
I'm having problems, however, getting the algorithm to work in a simulation using more than one vehicle and more than one limit. Single vehicles can follow and limit
pattern, and convoys can follow one limit. Two or more cars going through different speed zones result in ArrayIndexOutOfBounds errors, wrong limits being obeyed and more, depending on what I change to try and fix it.
I was hoping someone could cast a more experienced eye over the algorithm and see if they can find a working, generic formula that gives the right value for any situation.
Cheers.