April 26, 2024, 10:36:21 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Howto : Draw the function y = f(x) --> a * x + b in graph presentation

Started by Techno, July 15, 2015, 02:53:33 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Techno

Hi all,

How the linear function graphically presented on the screen by a curve?
The user give the parameters : a, b
The application draw the function
I don't know how I can do this in iwbasic.
A short example is very useful for learn it

Thank you
Stephane

LarryMc

Unlike the same exact question you posted on the powerbasic forums today(with pb source code) you need to define your question better here.

1)Do you want to display it in a console or a window?

2)Do you already know the range of x?

3)What are the max values of x and y to be displayed?

4)Is the value of x to be input directly by the user?

5)What are the values of a and b to be?

6)Are the values of a & b to be input directly by the user?

First you need to answer the preceding 6 questions.
Then you need to read the IWBasic help file (I would suggest choosing windows over console and the Graphics section of the help file.  That shows you how to open a window and plot points.)

Then I suggest you take your best shot at writing a program(should be VERY easy after reading that SMALL section of the help file) and if you then have problems post that program and we'll see if we can help you.

An alternative would be for you to purchase the Custom Chart Designer and Library and implement the F(x) chart type.
http://www.ionicwind.com/forums/index.php?topic=4190.msg32707#msg32707
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Andy

Larry,

I agree, even I do try to read the help file and look for examples first.

The custom chart designer has saved me so much time (Egil  ;)
and it is so easy to adapt into a program you are writting.

It's definitely worth it.

Why re-invent the wheel?

Andy.
:)

Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Techno

Quote from: LarryMc on July 15, 2015, 03:42:27 AM
Unlike the same exact question you posted on the powerbasic forums today(with pb source code) you need to define your question better here.

1)Do you want to display it in a console or a window?  --> window application

2)Do you already know the range of x?                      --> the user can enter the range

3)What are the max values of x and y to be displayed? --> the user can entered from x[-10..+10] and y[-5..+5]

4)Is the value of x to be input directly by the user?      --> the user enter the x value

5)What are the values of a and b to be?                     --> integers

6)Are the values of a & b to be input directly by the user? --> yes

First you need to answer the preceding 6 questions.
Then you need to read the IWBasic help file (I would suggest choosing windows over console and the Graphics section of the help file.  That shows you how to open a window and plot points.)

Then I suggest you take your best shot at writing a program(should be VERY easy after reading that SMALL section of the help file) and if you then have problems post that program and we'll see if we can help you.

An alternative would be for you to purchase the Custom Chart Designer and Library and implement the F(x) chart type.
http://www.ionicwind.com/forums/index.php?topic=4190.msg32707#msg32707

I have no problem to write an GUI application but how I implemented with this x and y values, the scales, grids
I do have plans to buy this library when I have money and this happens end of august 2015

Kind regards
Stephane

LarryMc

read the graphics section of the help file

with the LINE and/or LINETO commands  or the RECT command mark off the area that will contain your chart
the lower left corner will be x=0 y=0 so whatever pixel point that location is will have to be used to adjust your calculated x y values

use the LINE command to draw tic marks where you want them
use PRINT commands to put text  (scales, axis labels, titles) where you want them.
then feed x values to equation to get y value; adj x/y values for pixel location then use PSET to put dot on screen

that's all you need READ THE HELP FILE AND MAKE AN EFFORT TO TRY TO DO IT YOURSELF

then ask for help with your code - not to write your code.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Andy

I agree Larry,

It is the ONLY way to learn something.

1. Find an example of code
2. Take time to read the help file
3. Take time to try and understand how the example code works
4. Try writting your own program based on the example to begin with

It may seem that I post here a lot, but I only do so when I am truely stuck.

We all try and help each other, maybe sticking to one problem at a time will help you learn the language.

Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Techno

Quote from: andy1966 on July 15, 2015, 05:07:55 AM
I agree Larry,

It is the ONLY way to learn something.

1. Find an example of code
2. Take time to read the help file
3. Take time to try and understand how the example code works
4. Try writting your own program based on the example to begin with

It may seem that I post here a lot, but I only do so when I am truely stuck.

We all try and help each other, maybe sticking to one problem at a time will help you learn the language.

Andy.
:)

My first try but it fails and dont draw the function ax + b. Why



DEF hWnd1 as WINDOW

'Create parent window
OPENWINDOW hWnd1, 0, 0, 350, 350,@MINBOX|@MAXBOX|@SIZE,NULL,"First Graphic Example",&WinProc
DEF x AS int

'Create Graphic Window
RECT(hWnd1,0,0,350,350,RGB(255,0,0), RGB(0,0,255))

SETFONT(hWnd1, "Ariel", 12, 700, @SFITALIC)
MOVE(hWnd1,20,20)
PRINT hWnd1, "ax + b = 0"
'Draw Y Axis
line(hWnd1,20,200,20,20) 
'Draw Y Axis
LINE(hWnd1,20,200,200,200)

'Draw the function y = f(x) --> ax + b
FOR x = 0 to 180
LINE(x - 1 + 20, 200 - f(x - 3, a, b), x + 20, 200 - f(x,a,b))
NEXT x

WAITUNTIL hWnd1 = 0
END

SUB WinProc(),INT
IF @CLASS = @IDCLOSEWINDOW
REM closes the window and sets w1 = 0
CLOSEWINDOW hWnd1
   ENDIF
RETURN 0
ENDSUB


SUB f(x as int, a AS INT, b AS INT),int
RETURN(a * x + b)
ENDSUB


Brian

I don't see where you are calling the function SUB f anywhere

Brian

LarryMc

He is calling it in the loop.
Problems:
Never defined a and b and assigned values
In the LINE command inside loop left out the window parameter
Never declared the sub function (easiest way in this case was to put GLOBAL in front of SUB)

I also fixed handler so it also centers your window

Here's corrected code
DEF hWnd1 as WINDOW
int a,b
a=3
b=6
'Create parent window
OPENWINDOW hWnd1, 0, 0, 350, 350,@MINBOX|@MAXBOX|@SIZE,NULL,"First Graphic Example",&WinProc
DEF x AS int

'Create Graphic Window
RECT(hWnd1,0,0,350,350,RGB(255,0,0), RGB(0,0,255))

SETFONT(hWnd1, "Ariel", 12, 700, @SFITALIC)
MOVE(hWnd1,20,20)
PRINT hWnd1, "ax + b = y"
'Draw Y Axis
line(hWnd1,20,200,20,20,RGB(255,255,0)) 
'Draw Y Axis
LINE(hWnd1,20,200,200,200,RGB(255,255,0)) 


'Draw the function y = f(x) --> ax + b
FOR x = 0 to 180
LINE(hWnd1,(x - 1 + 20), (200 - f(x - 3, a, b)), (x + 20), (200 - f(x,a,b)))
NEXT x

WAITUNTIL hWnd1 = 0
END

SUB WinProc(),INT
select @CLASS
case @idcreate
centerwindow hWnd1
case @IDCLOSEWINDOW
REM closes the window and sets w1 = 0
CLOSEWINDOW hWnd1
   ENDselect
RETURN 0
ENDSUB


global SUB f(x as int, a AS INT, b AS INT),int
RETURN(a * x + b)
ENDSUB
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Techno

I try to show the exp and log function but very strange result

This is my code



DEF hWnd1 as WINDOW
int a,b
a=3
b=6
'Create parent window
OPENWINDOW hWnd1, 0, 0, 350, 350,@MINBOX|@MAXBOX|@SIZE,NULL,"First Graphic Example",&WinProc
DEF x AS int

'Create Graphic Window
RECT(hWnd1,0,0,350,350,RGB(255,0,0), RGB(0,0,255))

SETFONT(hWnd1, "Ariel", 12, 700, @SFITALIC)
MOVE(hWnd1,20,20)
PRINT hWnd1, "ax + b = y"
'Draw Y Axis
line(hWnd1,20,200,20,20,RGB(255,255,0))  
'Draw Y Axis
LINE(hWnd1,20,200,200,200,RGB(255,255,0))  


'Draw the function y = f(x) --> ax + b
FOR x = 0 to 180
'LINE(hWnd1,(x - 1 + 20), (200 - f(x - 3, a, b)), (x + 20), (200 - f(x,a,b)))
LINE(hWnd1,(x - 1 + 20), (200 - fn_exp(x - 3, a, b)), (x + 20), (200 - fn_exp(x,a,b)))
NEXT x

WAITUNTIL hWnd1 = 0
END

SUB WinProc(),INT
select @CLASS
case @idcreate
centerwindow hWnd1
case @IDCLOSEWINDOW
REM closes the window and sets w1 = 0
CLOSEWINDOW hWnd1
  ENDselect
RETURN 0
ENDSUB


global SUB f(x as int, a AS INT, b AS INT),int
RETURN(a * x + b)
ENDSUB

GLOBAL SUB fn_exp(x as int, a AS INT, b AS INT), int
RETURN(EXP(a * x + b))
endsub



What wrong here?

LINE(hWnd1,(x - 1 + 20), (200 - fn_exp(x - 3, a, b)), (x + 20), (200 - fn_exp(x,a,b)))

GLOBAL SUB fn_exp(x as int, a AS INT, b AS INT), int
   RETURN(EXP(a * x + b))
endsub

LarryMc

Your biggest problem is that you used the EXP function without reading the help file
The EXP  function returns a DOUBLE, not an INT.
Then using the preset values of a and b  causes the graph to basically draw a straight line up yor y axis and since you are using the same color for everything you can't see it.

In the corrected code below at least you can see that it works and that the scaling needs to be be changed when you change functions in order to have graphs that display nicely.  Are better yet have separate graphs for different functions so that when you had your scales they will appear correctly.

DEF hWnd1 as WINDOW
int a,b
a=1
b=0
'Create parent window
OPENWINDOW hWnd1, 0, 0, 350, 350,@MINBOX|@MAXBOX|@SIZE,NULL,"First Graphic Example",&WinProc
DEF x AS int
openconsole
'Create Graphic Window
RECT(hWnd1,0,0,350,350,RGB(255,0,0), RGB(0,0,255))

SETFONT(hWnd1, "Ariel", 12, 700, @SFITALIC)
MOVE(hWnd1,200,20)
FRONTPEN hWnd1,RGB(255,0,255)
backpen hWnd1,RGB(0,0,255)
PRINT hWnd1, "ax + b = y"

MOVE(hWnd1,30,20)
FRONTPEN hWnd1,RGB(0,255,255)
PRINT hWnd1, "EXP(ax + b = y)"

'Draw Y Axis
line(hWnd1,20,200,20,20,RGB(255,255,0)) 
'Draw Y Axis
LINE(hWnd1,20,200,200,200,RGB(255,255,0)) 


'Draw the function y = f(x) --> ax + b
FOR x = 0 to 180

LINE(hWnd1,(x - 1 + 20), (200 - f(x - 3, a, b)), (x + 20), (200 - f(x,a,b)),RGB(255,0,255))
LINE(hWnd1,(x - 1 + 20), (200 - f_exp(x - 3, a, b)), (x + 20), (200 - f_exp(x,a,b)),RGB(0,255,255))
NEXT x

WAITUNTIL hWnd1 = 0
closeconsole
END

SUB WinProc(),INT
select @CLASS
case @idcreate
centerwindow hWnd1
case @IDCLOSEWINDOW
REM closes the window and sets w1 = 0
CLOSEWINDOW hWnd1
   ENDselect
RETURN 0
ENDSUB


global SUB f(x as int, a AS INT, b AS INT),int
RETURN(a * x + b)
ENDSUB

global SUB f_exp(x as int, a AS INT, b AS INT),double
RETURN EXP(a * x + b)
ENDSUB
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library