Posted by: programmervb on: July 25, 2008
Mirroring and scaling algorythms
I am sure you know what scaling is, but you might ask me where the mirroring comes in. Well, the scaling algorythm is similar to the mirroring algorythm, except the second one is incredibly simple. Let’s start with the beginning. I first heard of mirroring when I saw some pictures on the net, some mirror-like images. I downloaded a couple,and they were beautyfull. Then an idea came to me: why don’t I make a program that will be able to do any kind of mirror-effects ? So I started working…For that I used the DirectQB library, because I didn’t want to waste time onfiguring out how to load bmp or pcx pictures, thought I’ve done that before.That was simple enough. Now what do you need to be able to apply those effectson an image? This :
Now if you can do all those, let us move on to the math…
1.Mirroring
Now let’s say we are into 320×200x256 , and we have something drawn on the screen. We want to mirror the whole screen. First we set up the other page.Then we GET every (vertical or horizontal) line of the screen (that means X1=X2 – vertical lines or Y1=Y2 – horizontal lines) and put them on the other page, at 320-X (vertical lines) or 200-Y (horizontal lines). If you did not understand that, take a look at this
This is the first example. You will not be able to run this if you copy itin QB, but you might be able to modify it to work with your program :
X = 1
DO
GET X,1,X,200, …
PUT 320-X,1, …
X = X + 1
LOOP WHILE X < 320
If you understood that then you now know how mirroring is done. Yes,you’re right… it IS simple. Now to get one half of the screen and put it overthe other, you get half of the lines on the screen, and put them on the otherhalf, the same as before. I have an example for you :
The second example. You can copy and paste this into QB, and then run it.Hope you like it :
SCREEN 13
DIM pic(203) AS INTEGER ‘ I need this array for GET/PUT’ draw blue circle
FOR y = 1 TO 30
CIRCLE (80, 60), y, 1
NEXT y‘ draw red circle
FOR x = 1 TO 14
CIRCLE (50, 45), x, 4
NEXT x‘ draw yellow circle
FOR z = 1 TO 14
CIRCLE (110, 45), z, 14
NEXT z‘ use this if you want to know how big must be the array used with GET/PUT
‘X1 = 1
‘X2 = 1
‘Y1 = 1
‘Y2 = 199
‘BITSPERPIXELPERPLANE = 8
‘PLANES = 1
‘arraysize = 4 + INT(((X2 – X1 + 1) * (BITSPERPIXELPERPLANE) + 7) /* PLANES * ((Y2 – Y1) + 1)
‘PRINT arraysizex = 1
DO
GET (x, 1)-(x, 190), pic(1)
PUT (320 – x, 1), pic(1)
x = x + 1
LOOP WHILE x < 320 / 2
How about the scaling ? To scale an image, you use GET/PUT, but kind ofdifferent from what we did before. Let’s say we have an image, and we want to scale it to half it’s size… This is how I’d do this:
X = 1
Y = 1
DO
GET X, 1, X, 199, …
PUT Y, 1, …
X = X + 2
Y = Y + 1
LOOP WHILE X < 319
Got it? You take one line, you skip one line, and so on, but when you put them, you put them in order… And now you can scale it however you like!To scale it at 1/3 of the original size, you take 2, skip 1, take 2, skip 1…That’s all there is to it ! Now a final example:
SCREEN 13
DIM pic(203) AS INTEGER ‘ I need this array for GET/PUT‘ draw blue circle
FOR y = 1 TO 30
CIRCLE (80, 60), y, 1
NEXT y‘ draw red circle
FOR x = 1 TO 14
CIRCLE (50, 45), x, 4
NEXT x‘ draw yellow circle
FOR z = 1 TO 14
CIRCLE (110, 45), z, 14
NEXT z‘ use this if you want to know how big must be the array used with GET/PUT
‘X1 = 1
‘X2 = 1
‘Y1 = 1
‘Y2 = 199
‘BITSPERPIXELPERPLANE = 8
‘PLANES = 1′arraysize = 4 + INT(((X2 – X1 + 1) * (BITSPERPIXELPERPLANE) + 7) /* PLANES * ((Y2 – Y1) + 1)
‘PRINT arraysizex = 1
y = 160
DO
GET (x, 1)-(x, 190), pic(1)
PUT (y, 1), pic(1)x = x + 2y = y + 1
LOOP WHILE x < 320 / 2
As you will see, it only scales it horizontaly, so to scale it verticalyyou’ll have to get the horizontal lines (X1 = 1, Y2 = 1, X2 = 319, Y2 = 1) andthen put them (skipping some of them, offcourse…). To scale an image bothways, do the following :
I know it’s simple, but maybe you never thought about it, so youmight have never discovered it. F
Thanx for reading this and good lick with whatever project you’reworking on. Bye.