Sunday, September 25, 2011

C++ FLTK GRAPHING ALGEBRAIC FUNCTIONS TUTORIAL









REVISED: Friday, March 22, 2013




CONTENTS:
I.    INTRODUCTION
II.  HORIZONTAL LINE
III. SLOPING LINE
IV. PARABOLA

YOU WILL LEARN HOW TO GRAPH THE FOLLOWING ALGEBRAIC FUNCTIONS USING C++ AND FLTK:
1. HORIZONTAL LINE
2. SLOPING LINE
3. PARABOLA

I. INTRODUCTION


Welcome to the “C++ FLTK GRAPHING ALGEBRAIC FUNCTIONS TUTORIAL.”

This tutorial is a very brief overview of information presented by Dr. Bjarne Stroustrup in his book “Programming Principles and Practices Using C++,” Addison-Wesley 2009, ISBN 978-0321543721.

The web site for his book and the appropriate FLTX .h header files and .cpp files is:

http://www.stroustrup.com/Programming/


Please copy and paste the following example program into your IDE; then build and debug the example program.

//***********************************
//C++ FLTK GRAPHING ALGEBRAIC
//FUNCTIONS TUTORIAL
//***********************************
#include "Simple_window.h"
#include "Graph.h"

double one(double)
{
    return 1;
}

double slope(double x)
{
    return x/2;
}

double square(double x)
{
    return x*x;
}

int main( int argc, char* argv[] )
{
    using namespace Graph_lib;
    Point tl(100, 100);

    const int xmax = 600;
    const int ymax = 400;

    const int x_orig = xmax/2;
    const int y_orig = ymax/2;
    const Point orig(x_orig,y_orig);

    const int r_min = -10;
    const int r_max = 11;

    const int n_points = 400;

    const int x_scale = 30;
    const int y_scale = 30;
    Simple_window win(Point(100,100),xmax,ymax,"FUNCTION GRAPHING");

    Function s(one,r_min,r_max,orig,n_points,x_scale,y_scale);
    Function s2(slope,r_min,r_max,orig,n_points,x_scale,y_scale);
    Function s3(square,r_min,r_max,orig,n_points,x_scale,y_scale);

    win.attach(s);
    win.attach(s2);
    win.attach(s3);
   
    Text ts(Point(100,y_orig-40),"one");
    Text ts2(Point(100,y_orig+y_orig/2-20),"x/2");
    Text ts3(Point(x_orig-100,20),"x*x");
    win.set_label("Function Graphing: Label Functions");
    win.wait_for_button();

    win.attach(ts);
    win.attach(ts2);
    win.attach(ts3);
    win.wait_for_button();

    const int xlength = xmax-40;
    const int ylength = ymax-40;

    Axis x(Axis::x,Point(20,y_orig),xlength,xlength/x_scale,"one notch == 1");
    Axis y(Axis::y,Point(x_orig,ylength+20),
        ylength,ylength/y_scale,"one notch == 1");

    x.set_color(Color::dark_red);
    y.set_color(Color::dark_red);

    win.attach(x);
    win.attach(y);

    win.wait_for_button();
}

II. HORIZONTAL LINE


A line parallel to the x-axis is called a horizontal line. A horizontal line is parallel to the x-axis because the y value never changes.

We get a horizontal line by graphing the following function:

double one(double)
{
    return 1;
}


The line is defined by (x,y) == (x,1) for all x. In other words, we get the y value 1 for every x passed as an argument to the function one().

The following code specifies how it is to be drawn in the window:

Function s(one,r_min,r_max,orig,n_points,x_scale,y_scale);


The function specifies how its first argument, a function of one double argument returning a double, is to be drawn in the window.

The second argument and the third argument give the range of x, the argument to the function to be graphed.

The fourth argument, orig, tells the function where origin (0,0) is to be located within the window.

The remaining three arguments are default arguments. The function constructor arguments n_points, xscale and yscale were given the following initializers in the declaration. The default argument values are used if the caller does not specify values.

const int n_points = 400;

const int x_scale = 30;
const int y_scale = 30;

The graph label is as follows:

Text ts(Point(100,y_orig-40),"one");


The following code attaches the function and the graph label to win.

win.attach(s);
win.attach(ts);

III. SLOPING LINE


The slope of a line is referred to as the rise over the run, or given two points on the line, the change in the y coordinates over the change in x coordinates.

We get a sloping line by graphing the following function:

double slope(double x)
{
    return x/2;
}


The line is defined by (x,y) == (x,x/2). For every x we get the y value x/2.

The point where the two lines cross is (2,1).

The following code specifies how it is to be drawn in the window:

Function s2(slope,r_min,r_max,orig,n_points,x_scale,y_scale);


The function specifies how its first argument, a function of one double argument returning a double, is to be drawn in the window.

The second argument and the third argument give the range of x, the argument to the function to be graphed.

The fourth argument, orig, tells the function where origin (0,0) is to be located within the window.

The remaining three arguments are default arguments. The function constructor arguments n_points, and xscale were given the following initializers in the declaration. The default argument values are used if the caller does not specify values.

const int n_points = 400;

const int x_scale = 30;

The graph label is as follows:

Text ts2(Point(100,y_orig+y_orig/2-20),"x/2");


The following code attaches the function and the graph label to win.

win.attach(s2);
win.attach(ts2);

IV. PARABOLA


We get a parabola by graphing the following function:

double square(double x)
{
    return x*x;
}


The line is defined by (x,y) == (x,x*x). The lowest point where the parabola touches the sloping line is (0,0). The parabola is symmetric on the y axis.

The following code specifies how it is to be drawn in the window:

Function s3(square,r_min,r_max,orig,n_points,x_scale,y_scale);


The function specifies how its first argument, a function of one double argument returning a double, is to be drawn in the window.

The second argument and the third argument give the range of x, the argument to the function to be graphed.

The fourth argument, orig, tells the function where origin (0,0) is to be located within the window.

The remaining three arguments are default arguments. The function constructor arguments n_points were given the following initializer in the declaration. The default argument value is used if the caller does not specify a value.

const int n_points = 400;

The graph label is as follows:

Text ts3(Point(x_orig-100,20),"x*x");


The following code attaches the function and the graph label to win.

win.attach(s3);
win.attach(ts3);

YOU HAVE LEARNED HOW TO GRAPH THE FOLLOWING ALGEBRAIC FUNCTIONS USING C++ AND FLTK:
1. HORIZONTAL LINE
2. SLOPING LINE
3. PARABOLA

Elcric Otto Circle






-->

-->

-->




How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"




No comments:

Post a Comment