# A 2d array problem

• January 24th, 2012, 04:06 AM
IAmHere
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?
• January 24th, 2012, 05:44 AM
Mr.777
Re: A 2d array problem
Can you show us with the example, what exactly you want?
• January 24th, 2012, 07:05 AM
Norm
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.
• January 24th, 2012, 11:39 AM
IAmHere
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.
• January 24th, 2012, 11:42 AM
Norm
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?
• January 24th, 2012, 01:40 PM
IAmHere
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?
• January 24th, 2012, 01:45 PM
Norm
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.
• January 24th, 2012, 01:56 PM
IAmHere
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.
• January 24th, 2012, 02:11 PM
Norm
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

Quote:

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?
• January 24th, 2012, 02:19 PM
IAmHere
Re: A 2d array problem
I meant
4 is at location image1[0][1]
4 is the value.

so,

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

Will give us the values:

5, 6
8, 9
• January 24th, 2012, 02:35 PM
Norm
Re: A 2d array problem
Quote:

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)