## Visual Basic

Moderators: None (Apply to moderate this forum)
Number of posts: 55343

This Forum Only

rotating a 100*100 grid/array/matrix Posted by dazzlepecs on 20 Feb 2008 at 6:53 AM
Anyone have an idea how to do this?

Have a 100*100 grid where the user can draw in an image of black pixels (so can be 1s or 0s). Was going to feed this into an array

I have to then rotate this around (the info in the array), but with many degrees of freedom not just 90 deg shifts

anyone have an idea??
Re: rotating a 100*100 grid/array/matrix Posted by BitByBit_Thor on 20 Feb 2008 at 8:57 AM
: Anyone have an idea how to do this?
:
: Have a 100*100 grid where the user can draw in an image of black
: pixels (so can be 1s or 0s). Was going to feed this into an array
:
: I have to then rotate this around (the info in the array), but with
: many degrees of freedom not just 90 deg shifts
:
:
:
: anyone have an idea??
:

The biggest problem with rotation around a point (x,y) with an angle t is that you are working with pixels rather than a continuum. It's easy to calculate where in the continuum a pixel would end up, but it's not possible to put a pixel in a pixel. You'll need to 'smear' each pixel out over multiple destination pixels (atleast, that's one approach).
Basically, what you do is consider the pixel as a square in the continuum, then rotate it around (x,y) with angle t. It'll come out the same pixel only then itself also turned over the angle t. Then, you'll need to 'pixelize' the angled square. For instance, you could say the turned pixel affects the destination pixels equal to the amount of how much of the turned pixel is contained within the destination pixel.
I've created a MS Paint illustrative picture, and attached it to this post.
The left side shows what happens to a pixel when it is rotated in the continuum (actually I drew it wrong, but you get the idea: the square itself rotates). The left shows what I mean by 'smearing' each rotated pixel over the destination pixels. The grid shows the destination pixels and the red shows how the converted pixel would look when it is rotated in the continuum (actually, here too I drew it wrong, and also the red pixel is way too big, because it should actually be the same size as the raster, but the point, I hope, is clear). The numbers there are the percentages at which the rotated pixel affects the final color of the destination pixel. It's equal to the percentage of overlap. So in the centers, we get a 100% influence, but at the edges only around 10%.
The idea is to do this for each pixel and remember the influences. Then, when all these influences are calculated, each destination pixel will have a collection of influences. Then you arithmatically balance these influences to determine the final color:
If 70% influence from a red pixel, and also 15% from a blue, the final pixel should be such that the red pixel has more effect on the color, thus more red than blue. More exactly: 70/85 part red, 15/85 part blue. Then you turn this into an RGB color for the pixel (this is all pretty sketchy still; if you want I could help you with how to precisely implement this).
Note that it's also possible that a pixel has no influences, which means the rotation 'frees up' the pixel (or: when rotating, this pixel is placed somewhere else but no other pixel comes on this spot). You'll need to give these pixels the standard background color.

Also, when using a 100*100 pixel picture, the rotated picture can take up as much as 100 * Sqrt(2) ( = Sqrt(100² + 100²) = size of the diagonals) pixels squared (ie 100*Sqrt(2) x 100*Sqrt(2) ).

This is just a thought, and is not ready to be programmed yet. You'll need to furtherly develop it. One thing you need to do is calculate the destination of the corner points of each pixel (using Sines and Cosines; with this I can help if you want). Then, you'll need to develope a function that calculates, given 4 points which represent the corners of the deformed pixel, the percentage of area that is filled in a destination pixel by this deformed pixel. Then do this for each pixel (calculate the position of it's deformed corner pieces and then calculate the influence on each of the pixels it overlaps). After that, you need to calculate the final color for each destination pixel by looking at the influences and color of all the pixels that influence this destination pixel.

Anyway, sounds like fun :) I don't know how clear my explanation was, so don't hesitate to ask questions about it.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Re: rotating a 100*100 grid/array/matrix Posted by dazzlepecs on 20 Feb 2008 at 2:28 PM
: : Anyone have an idea how to do this?
: :
: : Have a 100*100 grid where the user can draw in an image of black
: : pixels (so can be 1s or 0s). Was going to feed this into an array
: :
: : I have to then rotate this around (the info in the array), but with
: : many degrees of freedom not just 90 deg shifts
: :
: :
: :
: : anyone have an idea??
: :
:
: The biggest problem with rotation around a point (x,y) with an angle
: t is that you are working with pixels rather than a continuum. It's
: easy to calculate where in the continuum a pixel would end up, but
: it's not possible to put a pixel in a pixel. You'll need to 'smear'
: each pixel out over multiple destination pixels (atleast, that's one
: approach).
: Basically, what you do is consider the pixel as a square in the
: continuum, then rotate it around (x,y) with angle t. It'll come out
: the same pixel only then itself also turned over the angle t. Then,
: you'll need to 'pixelize' the angled square. For instance, you could
: say the turned pixel affects the destination pixels equal to the
: amount of how much of the turned pixel is contained within the
: destination pixel.
: I've created a MS Paint illustrative picture, and attached it to
: this post.
: The left side shows what happens to a pixel when it is rotated in
: the continuum (actually I drew it wrong, but you get the idea: the
: square itself rotates). The left shows what I mean by 'smearing'
: each rotated pixel over the destination pixels. The grid shows the
: destination pixels and the red shows how the converted pixel would
: look when it is rotated in the continuum (actually, here too I drew
: it wrong, and also the red pixel is way too big, because it should
: actually be the same size as the raster, but the point, I hope, is
: clear). The numbers there are the percentages at which the rotated
: pixel affects the final color of the destination pixel. It's equal
: to the percentage of overlap. So in the centers, we get a 100%
: influence, but at the edges only around 10%.
: The idea is to do this for each pixel and remember the influences.
: Then, when all these influences are calculated, each destination
: pixel will have a collection of influences. Then you arithmatically
: balance these influences to determine the final color:
: If 70% influence from a red pixel, and also 15% from a blue, the
: final pixel should be such that the red pixel has more effect on the
: color, thus more red than blue. More exactly: 70/85 part red, 15/85
: part blue. Then you turn this into an RGB color for the pixel (this
: is all pretty sketchy still; if you want I could help you with how
: to precisely implement this).
: Note that it's also possible that a pixel has no influences, which
: means the rotation 'frees up' the pixel (or: when rotating, this
: pixel is placed somewhere else but no other pixel comes on this
: spot). You'll need to give these pixels the standard background
: color.
:
: Also, when using a 100*100 pixel picture, the rotated picture can
: take up as much as 100 * Sqrt(2) ( = Sqrt(100² + 100²) = size of the
: diagonals) pixels squared (ie 100*Sqrt(2) x 100*Sqrt(2) ).
:
: This is just a thought, and is not ready to be programmed yet.
: You'll need to furtherly develop it. One thing you need to do is
: calculate the destination of the corner points of each pixel (using
: Sines and Cosines; with this I can help if you want). Then, you'll
: need to develope a function that calculates, given 4 points which
: represent the corners of the deformed pixel, the percentage of area
: that is filled in a destination pixel by this deformed pixel. Then
: do this for each pixel (calculate the position of it's deformed
: corner pieces and then calculate the influence on each of the pixels
: it overlaps). After that, you need to calculate the final color for
: each destination pixel by looking at the influences and color of all
: the pixels that influence this destination pixel.
:
: Anyway, sounds like fun :) I don't know how clear my explanation
:
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry

wow!!!! thank you SO much!!! You went above & beyond the call of duty!!! I will have to print this out and show it to my dad as he is helping (I am a n00b at visual basic)

The program is for a neural network and recognising the smiley face at any angle, they work on a fuzzy type of principle so it doesnt have to be super-accurate rotation at all, maybe the rotated pixel can just "snap" into the nearest fit?.

Re: rotating a 100*100 grid/array/matrix Posted by BitByBit_Thor on 20 Feb 2008 at 3:03 PM
: : : Anyone have an idea how to do this?
: : :
: : : Have a 100*100 grid where the user can draw in an image of black
: : : pixels (so can be 1s or 0s). Was going to feed this into an array
: : :
: : : I have to then rotate this around (the info in the array), but with
: : : many degrees of freedom not just 90 deg shifts
: : :
: : :
: : :
: : : anyone have an idea??
: : :
: :
: : The biggest problem with rotation around a point (x,y) with an angle
: : t is that you are working with pixels rather than a continuum. It's
: : easy to calculate where in the continuum a pixel would end up, but
: : it's not possible to put a pixel in a pixel. You'll need to 'smear'
: : each pixel out over multiple destination pixels (atleast, that's one
: : approach).
: : Basically, what you do is consider the pixel as a square in the
: : continuum, then rotate it around (x,y) with angle t. It'll come out
: : the same pixel only then itself also turned over the angle t. Then,
: : you'll need to 'pixelize' the angled square. For instance, you could
: : say the turned pixel affects the destination pixels equal to the
: : amount of how much of the turned pixel is contained within the
: : destination pixel.
: : I've created a MS Paint illustrative picture, and attached it to
: : this post.
: : The left side shows what happens to a pixel when it is rotated in
: : the continuum (actually I drew it wrong, but you get the idea: the
: : square itself rotates). The left shows what I mean by 'smearing'
: : each rotated pixel over the destination pixels. The grid shows the
: : destination pixels and the red shows how the converted pixel would
: : look when it is rotated in the continuum (actually, here too I drew
: : it wrong, and also the red pixel is way too big, because it should
: : actually be the same size as the raster, but the point, I hope, is
: : clear). The numbers there are the percentages at which the rotated
: : pixel affects the final color of the destination pixel. It's equal
: : to the percentage of overlap. So in the centers, we get a 100%
: : influence, but at the edges only around 10%.
: : The idea is to do this for each pixel and remember the influences.
: : Then, when all these influences are calculated, each destination
: : pixel will have a collection of influences. Then you arithmatically
: : balance these influences to determine the final color:
: : If 70% influence from a red pixel, and also 15% from a blue, the
: : final pixel should be such that the red pixel has more effect on the
: : color, thus more red than blue. More exactly: 70/85 part red, 15/85
: : part blue. Then you turn this into an RGB color for the pixel (this
: : is all pretty sketchy still; if you want I could help you with how
: : to precisely implement this).
: : Note that it's also possible that a pixel has no influences, which
: : means the rotation 'frees up' the pixel (or: when rotating, this
: : pixel is placed somewhere else but no other pixel comes on this
: : spot). You'll need to give these pixels the standard background
: : color.
: :
: : Also, when using a 100*100 pixel picture, the rotated picture can
: : take up as much as 100 * Sqrt(2) ( = Sqrt(100² + 100²) = size of the
: : diagonals) pixels squared (ie 100*Sqrt(2) x 100*Sqrt(2) ).
: :
: : This is just a thought, and is not ready to be programmed yet.
: : You'll need to furtherly develop it. One thing you need to do is
: : calculate the destination of the corner points of each pixel (using
: : Sines and Cosines; with this I can help if you want). Then, you'll
: : need to develope a function that calculates, given 4 points which
: : represent the corners of the deformed pixel, the percentage of area
: : that is filled in a destination pixel by this deformed pixel. Then
: : do this for each pixel (calculate the position of it's deformed
: : corner pieces and then calculate the influence on each of the pixels
: : it overlaps). After that, you need to calculate the final color for
: : each destination pixel by looking at the influences and color of all
: : the pixels that influence this destination pixel.
: :
: : Anyway, sounds like fun :) I don't know how clear my explanation
: :
: : Best Regards,
: : Richard
: :
: : The way I see it... Well, it's all pretty blurry
:
:
:
: wow!!!! thank you SO much!!! You went above & beyond the call of
: duty!!! I will have to print this out and show it to my dad as he is
: helping (I am a n00b at visual basic)
:
: The program is for a neural network and recognising the smiley face
: at any angle, they work on a fuzzy type of principle so it doesnt
: have to be super-accurate rotation at all, maybe the rotated pixel
: can just "snap" into the nearest fit?.
:
:

That's also very possible, but I couldn't directly think of a way to make sure every pixel has a place, and for small resolutions (like 100*100 or less) every pixel lost matters.
But you could surely try it, and if it works out well enough then you don't have to get it complicated.
In that case, I suggest you take the center, calculate it's new position and check in which pixel it ends up.
The problem I had with this is that two different pixels could end up at the same spot and overwrite eachother. An easier fix than the long-story-idea would be to also here average the pixels.

I guess it all depends on the precision you need from it. Neither solution is perfect, and probably each solution has pictures that work well on that one but not on the other, but also pictures that work better on the other.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Re: rotating a 100*100 grid/array/matrix Posted by dazzlepecs on 26 Feb 2008 at 1:48 PM
: That's also very possible, but I couldn't directly think of a way to
: make sure every pixel has a place, and for small resolutions (like
: 100*100 or less) every pixel lost matters.
: But you could surely try it, and if it works out well enough then
: you don't have to get it complicated.
: In that case, I suggest you take the center, calculate it's new
: position and check in which pixel it ends up.
: The problem I had with this is that two different pixels could end
: up at the same spot and overwrite eachother. An easier fix than the
: long-story-idea would be to also here average the pixels.
:
: I guess it all depends on the precision you need from it. Neither
: solution is perfect, and probably each solution has pictures that
: work well on that one but not on the other, but also pictures that
: work better on the other.
:
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry

I almost got something!!!

I got the rotating experiment down, so i can now rotate it later on using the bit of script.. just that!! I dont know how to save the pixels into an array!!! Arg!!!!

'Redraws the image when the window is covered/uncovered
picCanvas.AutoRedraw = True

'The canvas look and drawing behaviour
picCanvas.DrawWidth = 2 'Lines are 2 units thick
picCanvas.ForeColor = vbBlue 'Lines are blue
picCanvas.BackColor = vbWhite 'Background canvas is white
Dim MatrixN(3000, 3000) As Integer

End Sub

Private Sub picCanvas_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
picCanvas.PSet (X, Y)

Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim D As Integer
Let C = 180 / (180 / 3.14159)

A = ((X - 1507) * Cos(C)) - ((Y - 1507) * Sin(C))
B = ((X - 1507) * Sin(C)) + ((Y - 1507) * Cos(C))
picCanvas.PSet (A + 1507, B + 1507)

I guess i want the array input here (like Let Matrix(X,Y)=1) while its in the mouse-held-down loop... But i cant keep the data or it wont even let me call the function!! *cry*
Let D = D + 2
End If
Debug.Print D
End Sub

sorry to just chuck a bunch of code in here

I just realised the grid is waaaaaay too big. I need maybe 100*100 at most lol, i can rescale but then its impossible to draw xD any scaling routines so i can have a big pixellated area to input in the values for an array?
Re: rotating a 100*100 grid/array/matrix Posted by BitByBit_Thor on 27 Feb 2008 at 1:50 AM
:
: I almost got something!!!
:
: I got the rotating experiment down, so i can now rotate it later on
: using the bit of script.. just that!! I dont know how to save the
: pixels into an array!!! Arg!!!!
:
:
:
:
:
:
: 'Redraws the image when the window is covered/uncovered
: picCanvas.AutoRedraw = True
:
: 'The canvas look and drawing behaviour
: picCanvas.DrawWidth = 2 'Lines are 2 units thick
: picCanvas.ForeColor = vbBlue 'Lines are blue
: picCanvas.BackColor = vbWhite 'Background canvas is white
: Dim MatrixN(3000, 3000) As Integer
:
: End Sub
:
: Private Sub picCanvas_MouseMove(Button As Integer, Shift As Integer,
: X As Single, Y As Single)
: If Button = 1 Then
: picCanvas.PSet (X, Y)
:
:
: Dim A As Integer
: Dim B As Integer
: Dim C As Integer
: Dim D As Integer
: Let C = 180 / (180 / 3.14159)
:
: A = ((X - 1507) * Cos(C)) - ((Y - 1507) * Sin(C))
: B = ((X - 1507) * Sin(C)) + ((Y - 1507) * Cos(C))
: picCanvas.PSet (A + 1507, B + 1507)
:
: I guess i want the array input here (like Let Matrix(X,Y)=1) while
: its in the mouse-held-down loop... But i cant keep the data or it
: wont even let me call the function!! *cry*
Let D = D +
: 2
: End If
: Debug.Print D
: End Sub

:
:
: sorry to just chuck a bunch of code in here
:
: I just realised the grid is waaaaaay too big. I need maybe 100*100
: at most lol, i can rescale but then its impossible to draw xD any
: scaling routines so i can have a big pixellated area to input in the
: values for an array?

What you need to do is declare the variable outside the functions to make it a member variable of the form. That way, you can call it from any function and it's value will be preserved between function calls.
Also, for testing purposes, you might want to put the rotation code under a button rather than a mouse move. This will make it easier to test.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic