PDA

View Full Version : I can't find the problem!!!


JigPu
02-24-02, 11:42 PM
Well guys.... For one of the few times in my life, I'm stuck with a bug I can't find!

My program (written in Pascal) is supposed to plot a parabola on screen. What we are supposed to do is be able to define a "virtual window" of any size and at any position and plot the parabola accordingly on screen.

The problem is that for some reason my program hates the Y-Axis. Whenever you hit the (undocumented!!!) 'I' button, it will zoom in and then redraw the parabola so that the maximum and minimum X values have been halved along with the Y values. The result should be a zoom onto the center of the screen with everything twice as big. However, instead of zooming straight to the middle, it moves the Y axis up 1/2 of the remaining way up the screen.

This problem not only applies to the zoom function but to whenever you define a custom Y range. If the Y values aren't -240 and 240 it will not draw the graph correctly!

When you run the program, you will be asked for three numbers (the three coefficients [a, b, and c] in a quadradic equation [ax^2+bx+c]). I tend to use 9, -12, 5. Next you will be asked if you wish to use the default virtual window coordnites. After that, your parabola will be plotted. The two numbers on the screen are for debugging purposes for me and state the current corner coordnites (X1, Y1, X2, Y2). Press Q to quit along with an enter or two... :D

I just can't figure this darn bug out!! All the math seems to be correct, but something is obviously wong!!
JigPu

vandersl
02-25-02, 07:55 AM
Ok, I read through it and I think I found your problem.

When scaling the window, you are using:
X1:=X1/2
X2:=X2/2
Y1:=Y1/2
Y2:=Y2/2

This will scale and shift your window, as you are finding out. This will happen any time the original coordinates are not centered around zero. For example:
(-320,100),(320,200) --> (-160,50),(160,100)
The X axis worked OK, but the Y axis was scaled by 1/2 and shifted.

You need to keep your center point fixed while scaling. You can do this by computing the new center point and scale:
center := (Y1+Y2)/2
size := (Y1-Y2)/4
Y1 := center + size
Y2 := center - size
This assumes that Y1 > Y2, and losing 1/2 point resolution on your scale is not critical.

JigPu
02-25-02, 08:48 AM
Well, I typed it in and the problem still existed... After playing around with the signs and stuff, I managed to get it to work right with this...

Center:=(Y1+Y2);
Size:=ABS(Y1-Y2)/2; {ABS to get rid of the Y2 > Y1 problem}
X1:=X1/2;
X2:=X2/2;
Y1:=Center+Size;
Y2:=Center-Size;

Hmmmm.... Now I just have to study over how this worked at school... THANKS A TON MAN!! I've been debuging this problem for a week now!
JigPu

vandersl
02-25-02, 09:17 AM
No problem. But you will want to do the same thing for the X axis values, or you will run into the same problem if the original coordinates aren't centered on the origin.

JigPu
02-25-02, 07:29 PM
Uh oh... I ran the program and did some extra checking to make sure that really fixed the bug, but it turns out that it didn't!! It actually only zoomed the X axis, but not they Y. I'm sure your original code is correct and that there is something else wrong in the program.

My thoughts... Up where it changes X to the Window, or where it turns the Window's Y to the CRT's Y. Unfortunatly, I left my disk at school, so I can't upload a slightly modified version in which I tried to fix the bug further. Guess I'm stuck with what I uploaded! :D

JigPu