Introduction

I wanted to draw a 3D math function on a static control like "MatLab" does. Finally, I found a solution.

Consider that you want to plot a 3D Pixel Q (a,b,c) on a plane with X and Y directions. In (figure 1), I illustrate this. At first, you must move in X direction from O to P1. It is important to know |P1 O| = b. Then you must move in Y direction from P1 to P2 and |P2 P1| = c. Now you must move in 't' direction from P2 to Q. And |Q P2 | = a. In 3D space, 't' direction is X-axis, X direction is Y-axis, and Y direction is Z-axis.

Figure 1 – Plot a 3D Point on a 2D page.

We want to obtain Q(x,y), so …

Eq. 1

Using the code

First of all, add 3DStatic.h and 3DStatic.cpp files to your project. Select Resource tab from Workspace window, and select your dialog that you want to add a display static. Select Static Control from Control toolbox and draw it on the dialog (Figure 2). Change its ID from IDC_STATIC to IDC_MyStatic.

Figure 2 - Add Static and Button Control to your dialog.

Now it's time to add a member variable to your dialog class. Call Class Wizard to do it for you. Figure 3 shows you how to do it. In this case, we add a member variable m_MyStatic with type CStatic.

Figure 3 - Add member variable to your dialog class.

OK, open your dialog class header file, add this line on top of your class definition:

How does it work?

One of the important functions is Set3DPixel.This function changes a point in 3D space to a 2D point. It is a polymorphism function and in its second form, it plots the obtained point on static. It uses equation 1.

In the constructor function, we can set a virtual scale for static width by defining x_Max and x_Min, and for static height by defining y_Max and y_Min. Note that variable Oldx must be between x_Min and y_Min and similarly for Oldy.

The two next functions are SetHighLowColor and Set3DColor. The first attributes maximum value of MyFunc (MAXp) to pure red color and minimum value of MyFunc (MINp) to pure blue color. The second one sets a color for a given value of Mathfunc. Finally using Set3DPixel, we plot a pixel on the static.

Have fun.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Share

About the Author

I live in Iran . I started hardware programming when I was young. I designed and built some ISA cards for attaching to PC.like ocsiloscope and signal generator. Now I am working for a engineering company and Manager of some project.

Your code seems to write any one point in signal, do it?if so do you plane to make it possible to draw only visible points (to save some time). Hm... can you tell me how to rotate, if possible, the view point of graphics.

It's not terribly hard really. Draw the points from the front to back and keep track of what the highest point (smallest y coordinate) for every given x coordinate is. For each point if its y coordinate is below the highest value for that x coordinate then it is not visible so it is not drawn. I have used this approach in the past and it worked well.

You have to determine how to traverse the grid according its rotation in order to draw front to back.

One more thing - you can optimize many of your calculations by keeping values like tan(steep) and cos(steep) in temporary variables instead of calculating them for every pixel. I see many repeatedly computed values that do not change for every pixel. You could see a potentially large improvement in speed by doing this.