Results 1 to 4 of 4
- 02-27-2012, 07:50 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
get a previous or following element with a indentifier
I have a robot with an orientation ("up","down","left","right"). He must be able to turn clockwise and counter-clockwise.
My first solution was to write an if-statement for every possible orientation and, depending on the current orientation(saved in a private variable), to simply use a setter to change orientation in both a turnClockwise method as a turnCounterClockwise method. This is not really elegant..
Then I thought that it might be possible to put my elements in an arraylist or something like that and save them in a clockwise sequence like "up","right","down","left".
Then I would use a method to get the next or previous element depending on how i'm turning. But I don't know how to retrieve the last element when my index is zero (or vice versa). This is the case when I turn counter-clockwise from "up" to "left". Is this even possible with an array list or should I use something else? Alternative, more elegant solutions are also welcome.
Thanks!
- 02-27-2012, 07:57 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 81
- Rep Power
- 0
Re: get a previous or following element with a indentifier
I think there's a way to do this with enums but I'm not sure.
What I might do is, rather than have a variable to keep track of direction, have two int variables for x velocity and y velocity of the robot. Depending on the specifics of your problem, this may or may not be a good way of doing it, but here's how you'd do it:
The four directions and their corresponding (xvel, yvel) pairs would be:
right: (1, 0)
down: (0, 1)
left: (-1, 0)
up: (0, -1)
So, to rotate clockwise, you would do the following:
Java Code:int temp=xvel; xvel=-yvel; yvel=temp;
Java Code:int temp=yvel; yvel=-xvel; xvel=temp;
- 02-27-2012, 09:34 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
Re: get a previous or following element with a indentifier
thanks man thats a really nice way to solve it!
- 02-29-2012, 07:44 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
Re: get a previous or following element with a indentifier
Now I'm using int orientation (0,1,2,3) fo up right down left. I'm turning like this
public void turnClockwise (){
assert isValidEnergyLevel((energyLevel-100),getMAX_ENERGYLEVEL());
setOrientation(this.orientation+1);
withdrawEnergy(100);
}
public void turnCounterClockwise (){
assert isValidEnergyLevel((energyLevel-100),getMAX_ENERGYLEVEL());
setOrientation(this.orientation-1);
withdrawEnergy(100);
}
public void setOrientation(int orientation) {
this.orientation = Math.abs(orientation)%4;
}
It fits better with the GUI that I'm using.
Similar Threads
-
How to get previous line rather than next?
By Cylab in forum New To JavaReplies: 5Last Post: 08-11-2010, 08:14 AM -
How to get previous element from collection and correctly synchronize threads?
By atch in forum New To JavaReplies: 1Last Post: 02-08-2010, 02:33 PM -
compare newly added Vector Element with previous elements
By nidhirastogi in forum Advanced JavaReplies: 10Last Post: 09-10-2008, 02:32 AM -
add previous jar library to new one
By Farzaneh in forum EclipseReplies: 1Last Post: 08-23-2008, 03:31 PM -
Next and Previous Buttons
By JavaNewb in forum New To JavaReplies: 1Last Post: 05-09-2008, 02:23 AM
Bookmarks