spirit_warrior
April 10th, 1999, 10:23 PM
OK, I am new and need help. I know how to draw many shapes in my basic MFC program but do not know how to put in an axis and parabola.
I must be missing something simple - trying too hard or something. Can anyone show me the way?
Note, this is simple, basic MFC, nothing complicated. Thanks in advance!
deb
Jerry Coffin
April 11th, 1999, 03:13 PM
This is a little more complex than it might initially appear. The problem is that a parabola normally involves drawing with floating point coordinates. Unfortunately, Windows uses integers for all coordinates.
You'll quickly find that with mapping modes, there are a number of ways of accomplishing almost any one thing. I'll present ONE way of handling this, with the assurance that it's not the only one, and probably not even the neatest one...
First of all, I'd start with a normal SDI project with its view derived from CView -- the default. Then I'd add a couple of variables to the view, named x_limit and y_limit, both of them private. I'd initialize y_limit to 32767 and x_limit to y_limit/50. Since we're working with integers, we'll see quite a lot of the parabola, which means it'll look very tall and narrow if we don't scale the x and y axes differently from each other.
Now we need to set up that scaling. To do this, we add the following to OnPrepareDC:
GetClientRect(&rect);
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetViewportExt(rect.Width(), -rect.Height());
pDC->SetWindowExt(x_limit, y_limit);
pDC->SetViewportOrg(rect.Width()/2, rect.Height()-3);
This basically sets up a system that's x_limit units wide and y_limit units high, with the origin set to the middle of the window from left to right, and 3 pixels up from the bottom (so we can see the origin line).
Now, we need to draw an axis and parabola. We start with the axes:
pDC->MoveTo(-x_limit/2, 0);
pDC->LineTo(x_limit/2, 0);
pDC->MoveTo(0,0);
pDC->LineTo(0, y_limit);
Hopefully that should be pretty self-explanatory. Drawing the parabola is fairly simple as well -- we just step along the x axis, and figure the y as the square of the x value:
for (long x=0; x<x_limit/2; x++) {
long y = x*x;
if ( y > y_limit)
break;
pDC->SetPixelV(x, y, RGB(0, 0, 0));
pDC->SetPixelV(-x, y, RGB(0, 0, 0));
}
We could figure the positive and negative values of x separately from each other, but I've cheated and depended on knowing they're symmetrical, so I just draw each point, and then it's mirror image on the negative X side.
As I noted above: you can do a LOT of different things with mapping modes, and you can often accomplish the same thing in quite a few different ways. You can also skip using mapping modes at all. For example, you might prefer to use floating point calculations for the parabola, and convert from floating point to integers yourself. This would allow you to avoid the (rather grungy) disparity between X and Y values, instead drawing a parabola over a much smaller range of values.
The universe is a figment of its own imagination.