Calling an Array within a program (java) - java

I'm currently putting together a basic world viewer program. At each viewpoint, I have 4 pictures, each at 90 degrees to one another. I have created an array of photos for each viewpoint and an String currentPoint which will hold the name of the viewpoint. I think there is a way to take the name of the current array and call the contents of it so I need only write one set of code for the turn left ad turn right functions below, but I can't think what it would be. How is it done?

Suppose that your array array is arranged in the order [north-facing, east-facing, south-facing, west-facing], and that you have an index i indicating the index of the current view (0 for north, 1 for east, 2 for south, 3 for west). Then to "turn left" is to decrement i modulo 4 and to "turn right" is to increment i modulo 4.
With this approach, turning right is straightforward: i = (i + 1) % 4.
Turning left is a little bit tricker, because % is actually defined as a remainder operator in Java, rather than as a modulus operator, so i = (i - 1) % 4 will not work (it will leave i == -1 when it should be i == 3). Instead, you can write: i = (i + 3) % 4.
(With this in mind, we can define a full set of absolute directional constants — NORTH as 0, EAST as 1, SOUTH as 2, WEST as 3 — and relative directional constants — FRONT as 0, RIGHT as 1, BACK as 2, LEFT as 3. Then, for example, WEST == (NORTH + LEFT) % 4 and NORTH == (WEST + RIGHT) % 4.)

Related

Recreate a binary matrix with least "XOR" operations

This was a highschool-degree coding contest question a while back. The basic idea was to recreate a painting of black and white with only rectangular XOR operations, or that's what they called it.
The problem
Let us assume we have this painting we are trying to recreate (represented as a binary matrix, 0 being black and 1 being white):
1 0 0
1 1 1
1 0 1
One way of recreating the painting would be the following operations:
(0, 0) (2, 2)
(1, 0) (2, 0)
(1, 2) (1, 2)
The operations are in the form of (xStart, yStart) (xEnd, yEnd)
Thus the above operations would do the following, if we start from an all-black canvas:
beginning:
0 0 0
0 0 0
0 0 0
after (0, 0) (2, 2) :
1 1 1
1 1 1
1 1 1
after (1, 0) (2, 0) :
1 0 0
1 1 1
1 1 1
after (1, 2) (1, 2) :
1 0 0
1 1 1
1 0 1
Technicalities about the assignment:
Winner has the least operations.
One operation should be in the form of (xStart, yStart) (xEnd, yEnd).
There are no time or space restraints.
In the assignment, the painting we were trying to recreate was 200x200 in size, and generated with 2000 random XOR operations.
My own ideas
I have come up with a couple of ways to do this. I'll list them here in order of worse to best.
XOR all the pixels:
We can recreate the painting by simply writing 1 to a blank canvas where a 1 resides in the painting we are trying to recreate. This solution is the simplest and most obvious of all. The number of operations needed is basically the amount of white pixels in the painting.
XOR all horizontally adjacent whites:
This is a substantial improvement from the first solution, but still very simple and obvious. In this method we simply XOR all horizontally adjacent whites. In this way, for example the operations
(0, 0) (0, 0)
(1, 0) (1, 0)
(2, 0) (2, 0)
would be diminished to (0, 0) (2, 0).
XOR rectangles:
This I think is a clear follow-up of the previous method, which we can see as XORing rectangles with height of 1 - Now we just add a second dimension to the rectangles, further improving our results. I determined the XORable area by getting the rectangle with the most whites. The improvement is still good.
XOR the biggest difference:
This is a slight change from the above methods, and a bit more brute-force. In this method I find the rectangle with the biggest difference to the painting, and XOR it. For example, if we have the painting
1 0 1
0 1 1
0 1 0
and an all-black canvas, the biggest difference would be in the rectangle (0, 0) (2, 1), which has a difference of 2. I calculate the difference by getting all the non-same colors of the painting, which is 4 in the above situation, and then subtracting the amount of same colors from it, which in the above situation is 2. So different_colors - same_colors = difference.
In the above painting and a blank canvas, there are many rectangles that produce the same difference. One other is (1, 0) (2, 2).
This method gave the smallest improvement from the previous one with large paintings, but an improvement nonetheless. Interstingly, this method sometimes came up with a worse solution than the previous one with small paintings (can't remember how small, though).
Any code I had for the described methods above have been long since lost. Can you come up with breath-taking solutions? Is there a magical approach from outer space? I find this question (post) pretty interesting and would like to see if anyone can come up with anything.
About the tags
I have tagged this with both Java and C++, not because this question concerns particularly those languages, but because I can easily understand any code written in those languages, and with languages with similar syntax.
I think, to complete this task, we require to find the coordinates of maximum size sub-matrix containing only zeros.
I can explain that algorithm but I think following link has the best explanation :
Maximum size sum-matrix with all 1s in binary matrix
Here solution is for all 1s, we can modify it for all 0s.
Then all we need to do is find the coordinate from maximum value and we can do operation.
I'll update if I can think of some better method.

Java permutations of offsets

Had a question regarding generating a list of 10-digit phone numbers on a PhonePad, given a set of possible moves and a starting number.
The PhonePad:
1 2 3
4 5 6
7 8 9
* 0 #
Possible moves:
The same number of moves a Queen in chess can make (so north, south, east, west, north-east, north-west, south-east, south-west... n-spaces per each orientation)
Starting number: 5
So far I have implemented the PhonePad as a 2-dimensional char array, implemented the possible moves a Queen can make in a HashMap (using offsets of x and y), and I can make the Queen move one square using one of the possible moves.
My next step is to figure out an algorithm that would give me all 10-digit permutations (phone numbers), using the possible moves in my HasMap. Repetition of a number is allowed. * and # are not allowed in the list of phone numbers returned.
I would imagine starting out with
- 5555555555, 5555555551, 5555555552... and so on up to 0,
- 5555555515, 5555555155, 5555551555.. 5155555555.. and with the numbers 2 upto 0
- 5555555151, 5555551515, 5555515155.. 5151555555.. and with numbers 2 upto 0
... and so on for a two digit combination
Any suggestions on a systematic approach generating 10-digit combinations? Even a pseudocode algorithm is appreciated! Let me know if further clarification is required.
Thanks in advance! :)
In more detail, the simplest approach would be a recursive method, roughly like:
It accepts a prefix string initially empty, a current digit (initially '5'), and a number of digits to generate (initially 10).
If the number of digits is 1, it will simply output the prefix concatenated with the current digit.
If the number of digits is greater than 1, then it will make a list of all possible next digits and call itself recursively with (prefix + (current digit), next digit, (number of digits)-1 ) as the arguments.
Other approaches, and refinements to this one, are possible of course. The "output" action could be writing to a file, adding to a field in the current class or object, or adding to a local variable collection (List or Set) that will be returned as a result. In that last case, the (ndigits>1) logic would have to combine results from multiple recursive calls to get a single return value.

recursive game plan function

I want to write a simple recursive function for a game plan in java.
I have 2^k teams and want an output like this (e.g. 4 teams):
(team)(day1)(day2)(day3)
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
My idea was to call the function recursive with half of its original size, but I can't figure out how to code it properly. If called with n/2, the output has to go into the upper left corner of the plan, the output PLUS n/2 has to go to the lower left corner and the rest is symmetric to the center.
Thanks
My code so far
public void plan(int size) {
if(size==2){}
else{}
}
make a Set for each day (unique)
then use n(max number)
itterate a loop n times and each itteration
newRandomNumber % n (fetch a random number a limit it to 0 to (n-1)
now add the (generatedValue+1) to the set
if it already exists (do check ) then increment the value till its new value then add to set
note: i dont get ur symetric requirement

2D collision detection of images with transparent parts ignored

So I decided to look up some collision detection but I've been having trouble finding proper information
regarding 2d collision detection between two images that includes how to properly avoid detecting the transparent
areas of an image but I did find one post which I got myself attached to but the problem is that I don't
really understand the post nor do I understand why he does some of those things...
Here's the post in question: https://stackoverflow.com/posts/336615/revisions
So first of all I want to ask if this solution is actually a good one / proper or if I should just look elsewhere.
Secondly I wonder, in his post, he mentions using integer arrays, not 2d arrays either it seems, to
set 1 and 0 to decide whether or not the pixel is transparent or not but I don't really know how I am supposed
to achieve this. At first I thought it could be achieved by just forming a string of 1 and 0s and convert it to a Long
but even with a mere image width of 25, the Long, gets... too long...
I also tried this with no luck, since the code does not function with this array:
long[] array = new long[30*30]; // height * width of the image
int x = 0;
int y = 0;
for(int i = 0; i<30*30; i++){
if(image.getRGB(x,y) == 0){
array[i] = 0;
}
else{ array[i] = 1; }
x++;
if (x==30){
y++;
x=0;
}
}
Thirdly, I was hoping someone could maybe explain the whole process and why the things he does, are necessary.
By the way, I do know how those bit wise operators work!
In other words, I don't understand his train of thought / motivation for doing the all things in the code and I would like to gain an understanding of all this!
I don't really know how to proceed right now hehe...
The result of the bitwise AND operation (&) is true (1) for each each bit where the the corresponding bit is true in both operands, and false (0) otherwise.
The idea he's using is to create a version of the image (a mask) where each non-transparent pixel in the original image is stored as a '1' bit, and each transparent pixel as a '0' bit. These are packed into a single integer, which can be tested against the mask for another image with a single AND operation (before the AND he calculates the horizontal distance between the two images and shifts one of the masks if necessary).
For example, let's assume that we have the following two 4x1 pixel images:
5, 0, 0, 5
8, 8, 8, 8
Although I placed them on separate rows here for practical purposes, you should view them as being on the same row, so the last two pixels of the left image overlap with the first two of the right image.
The masks for the rows when viewed in binary representation would be:
1001
1111
The distance between the left and right image is -2, so we shift the first mask left by 2 bits:
1001 << 2 => 100100
So now we have these masks:
100100
001111
ANDing these gives us:
000100
The non-zero result tells us that we have a collision.

Determine if there are conflicts in set

I have a Java HashMap containing 5 entries. Mapped to each entry is an object containing:
an x value
a y value
a length
an orientation (either "horizontal" or "vertical")
Think of the game battleship with a 10 x 10 board. The x / y coordinates in each entry corresponds to the top left corner of a ship on the board, and the length and orientation correspond, as one would expect, to the ships length and orientation from that point.
I'm trying to think of a way to rip through the 5 ships and check if there are any "overlapping ships", known as conflicts, on the board. I can't figure out how to do this. Any help would be much appreciated.
One way to do it is:
1) assign each cell in your board a number, 1-100 (or 0-99).
2) Add a method to the things in your hashmap that returns a List of the unique cell ids that are covered. So if x == 1, y==1, length == 3, orientation == horizontal, you would return the three 1-100 values that represent the cell over which your submarine resides.
You will be able to calculate the first unique id by doing something like rowNumber*10 + columnNumber. You might have to tweak it depending on if you are 0 or 1 based, and or if your range is likewise 0 or 1 based..From there, if horizontal you just add 1 for each unit of length. Or you add 10 for each unique id if your piece is vertical.
3) Now you can have a collision detector class with a static method, that takes two pieces. It can call the method you created in step 2, get the two lists, and if there are any overlaps, you would find the same number in both lists.
I don't know if this is the best way to do it, but it is one way.

Categories