Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: A 2d array problem

1. ## A 2d array problem

Given a 2d array
and

4 variables that creates another 2d array.
(top, left) and (bottom,right)

(Those are the coordinates to form another 2d array.)
These variables can partially cover the given 2d array. So, negative and values greater than the width and height of the given 2d array is involved.

I have to figure out the overlapping 2d array created by these two 2d arrays and display it.
Can you give my some hints?  Reply With Quote

3. ## Re: A 2d array problem

Can you show us with the example, what exactly you want?  Reply With Quote

4. ## Re: A 2d array problem

How do you get a shape with only one variable. You need an x and y value to be able to define a shape with 4 points: top, bottom, left, right. If you have only an x value then you can only define locations on a line.  Reply With Quote

5. ## Re: A 2d array problem

(top, left) is the coordinates for the top left corner of the 2d array.
(bottom,right) is the coordinates for the bottom right corner of the 2d array.

So, we have 4 variables.
top = 1;

left = 1;
bottom = 2;

right = 2;

we'll have a 2x2 matrix.  Reply With Quote

6. ## Re: A 2d array problem

Ok, the 4 numbers define two points: x1,y1 and x2, y2
and x1 < x2 and y1 < y2

Where does the test for overlap come in?
Are there two sets of the two points?  Reply With Quote

7. ## Re: A 2d array problem

Sorry for the confusion.

The first 2d array is:
int[][] image1 = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9}, };
The second 2d array is:
top = 1;
bottom = 2;
left = 1;
right = 2;

It means the second is entirely overlapping the first 2d array.

5, 6
8, 9

is the overlapping value.

but I can't figure out what if the second 2d array is partially overlapping the first array.

eg. top = -4;
bottom = 2;
left = 1;
right = 2;

I have to find the overlapping values.
There are a lot of cases that I have to cover.
Is there a fast way of doing this?  Reply With Quote

8. ## Re: A 2d array problem

How are the points x,y value placed in the array?
Something like this: {{p1x, p1y}, {p2x,p2y}

What are the point's x,y values in this array:
int[][] image1 =
{ {1, 2, 3},
{4, 5, 6},
{7, 8, 9}, };

How many points are there in that array? Can you label the x,y values for each point?
pxi and pyi where i is the point number and pxi is the x value for point i

When you say overlap I think in terms of shapes drawn on a 2 dimensional (x,y) surface.  Reply With Quote

9. ## Re: A 2d array problem

{1, 2, 3},
{4, 5, 6},
{7, 8, 9}

1 is (0,0)
2 is (1,0)
3 is (2, 0)
4 is (0, 1)
.
.
.

So,

top = 1;
bottom = 2;
left = 1;
right = 2;

would be (top, left) and (bottom, right). These two coordinate are two corners of the second 2d array.  Reply With Quote

10. ## Re: A 2d array problem

Is this what you mean:
1 is at location (0,0)
2 is at location (1,0)
3 is at location (2, 0)
4 is at location (0, 1)
.

The values in () are the indexes to the location in the array??
{{(0,0) (1,0) (2, 0)}
{(0,1) (1,1) (2,1)}
etc

So,

top = 1;
bottom = 2;
left = 1;
right = 2;

would be (top, left) and (bottom, right). These two coordinate are two corners of the second 2d array.
What does that mean?  Reply With Quote

11. ## Re: A 2d array problem

I meant
4 is at location image1
4 is the value.

so,

top = 1;
bottom = 2;
left = 1;
right = 2;

Will give us the values:

5, 6
8, 9  Reply With Quote

12. ## Re: A 2d array problem

top = 1;
bottom = 2;
left = 1;
right = 2;

Will give us the values:

5, 6
8, 9
When you say top or bottom do you mean the column (or row)?
And then left or right is the row (or column)?
Then the element at top=1,left=1 is is on the first row in the first column

This would give ONE of the values (the x or the y?) of the four needed to define two points.
How do you get the other value (the y or the x) needed for the point.
How do you get the indexes for the other point(each point has an x and y value)  Reply With Quote