Matrix translation

  • Thread starter Thread starter Crirus
  • Start date Start date
C

Crirus

I have a 512x512 matrix.
I need a rutine that give me a transformed matrix from the curent one, with
a given point in the middle of the matrix, I mean 256x256.

Any thoughts?

Crirus
 
Crirus said:
I have a 512x512 matrix.
I need a rutine that give me a transformed matrix from the curent one, with
a given point in the middle of the matrix, I mean 256x256.

Do you use the 'System.Drawing.Drawing2D.Matrix' class?
 
OK, let me understand you correctly please.

You start with

Dim yourArray( 512,512) as Point

'and end up with

Dim transformedArray( 256,256) as Point

And you say that you want a point at the centre ?

My Questions

1.) What transformation is taking/supposed to place? and are you asking how
to acheive this ?

2.) With an even numbered x,y lengths you cannot acheive true centre, you
would need 257 or 255 ( with a centre of 229 or 228 ).

Please clarify


OHM
 
Ok, few mistakes...
The array is one and only 512 x512 starting from 0 so tere are 513 elements
on each dimension
I need another 512x512 array with a specified element in the 128x128
position

like this:

suppose (x,y) is the element at x,y position in the array

First 5x5 array()

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

Let say I need (1,3) element at the center

so the new array will be:


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


that transformation is what I need...
 
I did it!
Thanks for help intention:

This is the ideea:

For i As Integer = 0 To Width - 1
For j As Integer = 0 To Height - 1
x = i + (((Width - 1) / 2) - thisPoint.X)
y = j + (((Height - 1) / 2) - thisPoint.Y)
If x < 0 Then x = Width + x
If y < 0 Then y = Height + y
If x >= Width Then x = x - Width
If y >= Height Then y = y - Height
oldArray(x, y)=NewArray(i, j)
Next
Next
 
you could try something like this (code not tested) but look out for index
out of bound errors

dim x2,y2 as integer
for x as int = p1 -128 to p1 +128
for y as int = p2 -128 to p2 + 128
arr2(x2,y2) = arr1(x,y)
y2+=1
loop
x2 +=1
loop

Crirus said:
Ok, few mistakes...
The array is one and only 512 x512 starting from 0 so tere are 513 elements
on each dimension
I need another 512x512 array with a specified element in the 128x128
position

like this:

suppose (x,y) is the element at x,y position in the array

First 5x5 array()

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

Let say I need (1,3) element at the center

so the new array will be:


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


that transformation is what I need...
 
Hi Crirus,

There is unlikely to be anything in the Framework.

Can you give us any more information about what kind of transform you are
talking about - out of a universe of possibilities? The more you can specify,
the more likely someone will understand the requirement.

Regards,
Fergus
 
Hi,

This seems to be simple as long as allocating yet another couple of
megabytes shouldn't be an issue. So, allocate a destination array of the
same size. Then, start looping over rows and columns of the source array
(i.e. outer and inner loops). Within the inner loop body,
assuming that loop indexes were named row and col:

Dim newRow As Integer = (row + 128) Mod 512
Dim newCol As Integer = (col + 128) Mod 512

NewArray(newRow, newCol) = OldArray(row, col)

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE


Crirus said:
Ok, few mistakes...
The array is one and only 512 x512 starting from 0 so tere are 513 elements
on each dimension
I need another 512x512 array with a specified element in the 128x128
position

like this:

suppose (x,y) is the element at x,y position in the array

First 5x5 array()

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

Let say I need (1,3) element at the center

so the new array will be:


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


that transformation is what I need...
 
Hi Crirus,

Here's a version using Modulo arithmetic rather than Ifs to do the
wraparound. I've done two flavours - one if you prefer rows and columns, the
other if you prefer Xs and Ys. ;-)

Regards,
Fergus

<code info="Row, Col version>
Public Sub MoveToCentre (ByRef aVals(,) As Object, _
CentreRow As Integer, CentreCol As Integer)
Dim Width As Integer = aVals.GetUpperBound(0)
Dim Height As Integer = aVals.GetUpperBound(1)

Dim RowMod As Integer = Width + 1
Dim ColMod As Integer = Height + 1

Dim RowOffset As Integer = Width \ 2 + CentreRow + 1
Dim ColOffset As Integer = Height \ 2 + CentreCol + 1

Dim aNewVals (Width, Height) As Object
Dim Row As Integer
Dim Col As Integer
For Row = 0 To Height
For Col = 0 To Width
Dim SrcRow As Integer = (Row + RowOffset) Mod RowMod
Dim SrcCol As Integer = (Col + ColOffset) Mod ColMod
aNewVals (Row, Col) = aVals (SrcRow, SrcCol)
Next
Next
aVals = aNewVals
End Sub
</code>

<code info="X, Y version>
Public Sub MoveToCentre (ByRef aVals(,) As Object, _
CentreX As Integer, CentreY As Integer)
Dim W As Integer = aVals.GetUpperBound(0)
Dim H As Integer = aVals.GetUpperBound(1)

Dim XMod As Integer = W + 1
Dim YMod As Integer = H + 1

Dim dX As Integer = W \ 2 + CentreX + 1
Dim dY As Integer = H \ 2 + CentreY + 1

Dim aNewVals (W, H) As Object
Dim I As Integer
Dim J As Integer
For I = 0 To W
For J = 0 To H
aNewVals (I, J) _
= aVals ((I + dX) Mod XMod, (J + dY) Mod YMod)
Next
Next
aVals = aNewVals
End Sub

</code>
 
Back
Top