40530Re: [libertybasic] Re: plot using EXCEL
- Apr 1, 2010Hope the following makes sense- I'd next want to add numbers and minor
Just copy & paste into LB/JB.
Try altering bits you understand a tiny bit at a time, and check what
Reverse anything that fails to do what you want!
We all started simple, and learned from others' code.
The Help files are your first source- for eg. the names of available
' We don't need the text-only 'mainwin' but it may help debugging
' by allowing us to see values.
' But we DO need a graphicbox area in a 'Window'
' Choose size of this window (allow for scrollbars)
WindowWidth = 530
WindowHeight = 580
' Fit a 500 by 500 box to draw graphics with a nice border gap.
graphicbox #w.g, 10, 10, 500, 500
' Open the window showing this empty graphicbox
open "Simple Graph" for window as #w
' Make sure clicking 'close' icon at top RH exits cleanly.
#w, "trapclose [quit]"
' Tell LB to put printing 'pen' down on page.
' Tell LB we want a rather bigger dot size than the normal 1 pixel.
#w.g, "size 2"
' Work out how far apart lowest & highest x and y values are
xMax = 20
yMin = ( 1 /2 * -10 +1)
yMax = ( 1 /2 * 20 +1)
yRange = yMax -yMin
xRange = xMax -xMin
print xRange; " "; yRange
' step through the x range with small steps ( try bigger/smaller
for x= -10 to 20 step 0.1
' Work out the function's value at eac x position.
y = 1 /2 *x +1
' Now you need to change so x_min plots at 0 and x_max at 500
xScreen = int( ( x -xMin) /xRange *500)
' and same for y. BUT y in LB/MS counts down from top...
' so we subtract it from 500 so it counts up from bottom.
yScreen =500 -int( ( y -yMin) /yRange *500)
' Send a 'set x y' command- it plots a dot at ( x_screen,
#w.g, "set "; xScreen; " "; yScreen
' This is hard to interpret, without axes drawn. We need range (
-10, -5) to
' ( 20, 11) to be shown, with zero axes through ( 0, 0),
' but as before we scale, and invert y direction.
' First, horiz. axis thru. origin.
' Choose thicker line in colour red.
#w.g, "size 4 ; color red"
#w.g, "up ; goto 0 "; 500 -int( ( 0 -yMin) /yRange *500)
#w.g, "down ; goto 500 "; 500 -int( ( 0 -yMin) /yRange *500)
' Now the vertical axis
#w.g, "up ; goto "; int( ( 0 -xMin) /xRange *500); " 0"
#w.g, "down ; goto "; int( ( 0 -xMin) /xRange *500); " 500"
' Should see now it goes from (-10, -4) through ( 0, 1) to ( 20, 11)
' This makes the graphics 'stick'. Otherwise if covered and then
' uncovered they are eerased and not re-drawn.
' Hang around for 10 seconds ( 10000 ms)
timer 10000, [on]
' Put up a message for you to confirm (or not) printout.
confirm "Send to printer?"; answer
' If wanted, do a printout..
if answer then #w.g, "print vga"
' Wait, with graphic on-screen.wait
' Come to here only if 'close' icon clicked.
' Shut the window
' Tell LB the program has ended!
- << Previous post in topic Next post in topic >>