This source code enables you to plot the solutions to scientific and engineering problems. The Visual C++ programming environment was chosen for realization of numerical computation techniques. Visual C++ includes many advanced features that make it especially suitable for scientific and engineering computations. Visual C++ is also widely used; there is a wealth of information available on how to program with it. Many of the features of Visual C++ enable you to write computer programs that can perform useful numerical computations. It includes an extensive set of mathematical functions. You may define classes of data together with methods to perform abstact operations such as vector and matrix arithmetic.
A new project can be set up in the Visual C++ 6.0 environment using the following steps:
Enter, or copy, the following code into the empty application source file created using the process above. Here, the plotter class files are stored in a common directory so that they can be re-used for several plotting programs. You must add the plotter source file plotter.cpp to the Visual C++ project in order for the program to compile and link successfully.
#include "stdafx.h"
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <windowsx.h>
#include "..\plotter\plotter.h"
plotter np( "Plotter", "Numerical Plotter" );
/*----------------------------------------------------------------------------*
** "WinMain" is the program entry point
**
** returns: message parameter
*/
int APIENTRY WinMain( HINSTANCE inst, // window instance
HINSTANCE prev, // previous window
LPSTR cmdl, // command line string
int show ) // initial display
{
np.initialize( inst );
if( !np.window_class() )
return 0;
np.create( show );
return np.message_loop();
}
/*----------------------------------------------------------------------------*
** "WindowFunc" processes messages to this application
**
** returns: 0 if message processed, else default value
*/
LRESULT CALLBACK WindowFunc( HWND hwnd, // handle to window of message
UINT msg, // message code
WPARAM wp, // short message amplifier
LPARAM lp ) // long message amplifier
{
switch( msg )
{
case WM_CREATE:
np.on_create( hwnd );
break;
case WM_DESTROY:
np.on_destroy();
break;
case WM_SIZE:
np.on_size( hwnd );
break;
case WM_HSCROLL:
np.on_hscroll( hwnd, wp );
break;
case WM_VSCROLL:
np.on_vscroll( hwnd, wp );
break;
case WM_PAINT:
np.on_paint( hwnd );
break;
case WM_COMMAND:
np.on_command( hwnd, wp );
break;
default:
return DefWindowProc( hwnd, msg, wp, lp );
}
return 0;
}
/*----------------------------------------------------------------------------*\
** Put your mathematical functions to plot here. **
\*----------------------------------------------------------------------------*/
void run()
{
.
see below for a discussion of this function
.
}
Plotting Points
Visual C++ includes many built-in functions and data structures that support drawing in a window. One of the data types used here in the POINT structure that defines the x- and y- coordinates of a point:
typedef struct tagPOINT
{
long x;
long y;
}
POINT;
For drawing curves it is convenient to declare an array of point to be plotted using:
POINT curve[points];
To draw a curve in a window, the Polyline( hDC, curve, points ) function is used in the Numerical Plotter. It draws a series of line segments by connecting the points in an array of POINT structures. The plotter class uses this function for drawing curves.
The plotter class
This class handles all user input, output, drawing, and display functions so that you only need to write code for performing calculations to create a sequence of points to define a curve to plot. Complete source code is included below.
The plotter class includes several public functions to simplify the task of plotting curves:
void plotter::plot_region( double left, // left of origin is negative
double right, // right of origin is positive
double top, // above origin is positive
double bottom ) // below origin is negative
This must be called first in order to set screen parameters used in plotting your curves. The region is defined in dimensionless units measured from the plot origin.
void plotter::draw_axes()
This function is called to draw axes on the plot region from the origin to the edge of the screen. Tick marks are added at each unit of length from the origin.
POINT plotter::plot_point( double x, // horizontal location
double y ) // vertical location
This function converts a mathematical point to be plotted into the screen coordinate frame. It returns the point in screen coordinates.
void plotter::red_curve( POINT *curve, int points ) void plotter::green_curve( POINT *curve, int points ) void plotter::blue_curve( POINT *curve, int points )
These functions draw colored curves on the screen from an array of points. It makes use of the Polyline( hDC, curve, points ) function.
The run() function
The plotter class requires an entry point into your computational algorithm. The entry point is a C function declared as:
void run( void );
You must define the internals of this function. For example, to plot a single red curve, the following code may be used:
void run()
{
POINT curve[100];
int i;
/*
* first, define a plot region
* this must be done before calculating points to be plotted
*/
np.plot_region( -0.2, // left of origin is negative
4.0, // right of origin is positive
1.5, // above origin is positive
-0.5 ); // below origin is negative
i = calculate( curve, 100 ); // a curve of 100 points
np.draw_axes(); // of coordinate frame
np.red_curve( curve, i ); // plot the results
}
The vector class
This class enables you to encode vector arithmetic functions useful in solving differential equations represented with state variables. An example of its use is shown below. Complete source code for the vector class is included below.
vector::vector();
The default constructor initializes elements of the vector to zero.
vector::vector( const vector & );
The copy constructor initializes elements of the vector to be equal to the values of the argument vector.
double &vector::operator [] ( int );
This operator [] assigns value to an element of the vector on left of an arithmetic operation.
const double &vector::operator[]( int ) const;
This operator [] function creates a value to be assigned to an element of the vector on the right of an arithmetic operation.
const vector &vector::operator = ( const vector & );
This operator = function copies this vector to the right hand side of an assignment statement.
vector vector::operator + ( const vector & );
The operator + function performs vector addition.
vector vector::operator * ( const double );
The operator * function multiplies the vector by a real number.