Math Function Program

With this program you can show a mathematical function in a drawing area.

In the code the function y = 1/x is used.

You need two comanndbuttons and a drawing area to get the example going.

First the scale of the coordinate system is changed and the x and y axis is drawn.

Then the function y = 1/x is coded. The border values ( xmax, xmin, ymax and ymin) of your own coordinate system should be the same for both commandbuttons.

See how it looks like: ( in German) :

prgyfx.png

The Code:

PUBLIC SUB Button1_Click()
DIM dymax AS Integer
DIM dymin AS Integer 
DIM ymax AS Integer
DIM ymin AS Integer
DIM y AS Float
DIM dy AS Float
DIM dyi AS Integer
DIM dxmax AS Integer
DIM dxmin AS Integer 
DIM xmax AS Integer
DIM xmin AS Integer
DIM x AS Float
DIM dx AS Float
DIM dxi AS Integer

dymax = DrawingArea1.Height
dymin = 0 
ymax = 4
ymin = -1 

dxmax = DrawingArea1.Width
dxmin = 0 
xmax = 12
xmin = -1 

'x-axis si drawn 
FOR x = xmin TO xmax STEP 0.005
y = 0 
' formula to convert the scale 
'(dy - dymin) / (dymax - dymin) = (y - ymin) / (ymax - ymin )
'dy - dymin = (y - ymin) / (ymax - ymin ) * (dymax - dymin) 
dy = CFloat(y - ymin) / (ymax - ymin ) * (dymax - dymin) + dymin 
' formula 
'(dx - dxmin) / (dxmax - dxmin) = (x - xmin) / (xmax - xmin )
'dx - dxmin = (x - xmin) / (xmax - xmin ) * (dxmax - dxmin) 
dx = CFloat(x - xmin) / (xmax - xmin ) * (dxmax - dxmin) + dxmin 
'PRINT x,y,dx,dy
dyi = Fix(dy)
dxi = Fix(dx) 
Draw.Begin(DrawingArea1)
Draw.Point(dxi,DrawingArea1.Height- dyi)
Draw.End
NEXT 

'the y - axis is drawn 
FOR y = ymin TO ymax STEP 0.005
x = 0 
dy = CFloat(y - ymin) / (ymax - ymin ) * (dymax - dymin) + dymin 
dx = CFloat(x - xmin) / (xmax - xmin ) * (dxmax - dxmin) + dxmin 
dyi = Fix(dy)
dxi = Fix(dx) 
Draw.Begin(DrawingArea1)
Draw.Point(dxi,DrawingArea1.Height- dyi)
Draw.End
NEXT 
END 

'y = 1/x 
PUBLIC SUB Button1_Click()
DIM dymax AS Integer
DIM dymin AS Integer 
DIM ymax AS Integer
DIM ymin AS Integer
DIM y AS Float
DIM dy AS Float
DIM dyi AS Integer

DIM dxmax AS Integer
DIM dxmin AS Integer 
DIM xmax AS Integer
DIM xmin AS Integer
DIM x AS Float
DIM dx AS Float
DIM dxi AS Integer

dymax = DrawingArea1.Height
dymin = 0 
ymax = 4
ymin = -1 

dxmax = DrawingArea1.Width
dxmin = 0 
xmax = 12
xmin = -1 

FOR x = 0.01 TO xmax STEP 0.005
y = CFloat(1) / x
dy = CFloat(y - ymin) / (ymax - ymin ) * (dymax - dymin) + dymin 
dx = CFloat(x - xmin) / (xmax - xmin ) * (dxmax - dxmin) + dxmin 
dyi = Fix(dy)
dxi = Fix(dx) 
Draw.Begin(DrawingArea1)
Draw.Point(dxi,DrawingArea1.Height- dyi)
Draw.End
NEXT 
END

-- ReinerHoffmann - 05 Feb 2004