Re: Converting qbasic to Euphoria?
- Posted by Matt Lewis <matthewwalkerlewis at yahoo.com> Jul 28, 2004
- 588 views
Ed Davis wrote: > > Can the following qbasic code be easily converted to Euphoria? > Note that I am not looking for an exact translation. My goal is > to be able to use Euphoria to graph some simple algebraic > equations. Using DOS (as opposed to Linux or Windows) is ok too. > > Thanks for any help! > > DIM a, b, c, x AS DOUBLE > SCREEN (12) > WINDOW (-8, 6)-(8, -6) > LINE (-8, 0)-(8, 0) 'X axis > LINE (0, 6)-(0, -6) 'Y axis > FOR x = -8 TO 8 STEP .025 > a = x ^ 2 > b = SIN(x) > c = COS(x) > PSET (x, a) > PSET (x, b) > PSET (x, c) > NEXT x You could do this fairly easily using matheval. You'll need to update the grapheval.e file: The line towards the top (add CONSTANT, VAR):
integer INVALID, DATA, GRAPH, CGRAPH, RANGE, CONSTANT, VAR
And graph_init() should look like:
procedure graph_init() INVALID = resolve_math_ref( "INVALID" ) DATA = resolve_math_ref( "DATA" ) CONSTANT = resolve_math_ref( "CONSTANT" ) VAR = resolve_math_ref( "VAR") GRAPH = reg_math_func( "GRAPH", routine_id("Graph") ) CGRAPH = reg_math_func( "CGRAPH", routine_id("CGraph") ) RANGE = reg_math_func( "RANGE", routine_id("Range") ) end procedure
Then try the following (uses Win32Lib):
without warning include win32lib.ew include matheval.e include grapheval.e constant win = create( Window, "Graph", 0, Default, Default, 500, 420, 0 ) sequence graph, func, colors integer GRAPH procedure win_paint( integer self, atom event, sequence params ) setPenColor( win, BrightWhite ) drawPolygon( win, 1, { 10, 10, 490, 10, 490, 390, 10, 390 } ) for i = 1 to 3 do drawLines( win, colors[i] & graph[i] ) end for setPenColor( win, Black ) drawPolygon( win, 0, { 10, 10, 490, 10, 490, 390, 10, 390 } ) drawLine( win, 250, 10, 250, 390) drawLine( win, 10, 200, 490, 200) end procedure setHandler( win, w32HPaint, routine_id("win_paint")) procedure setup() sequence g matheval_init() GRAPH = resolve_math_ref( "GRAPH" ) func = { Parse( "x^2" ), Parse( "sin(x)" ), Parse( "cos(x)" ) } colors = {Blue, Green, Red} graph = repeat( 0, 3 ) SetScreen( {480, 380}) SetOrigin({10,10}) for i = 1 to 3 do g = Evaluate({ GRAPH, { func[i], "X"}, {-8, 8, 0.1, -6, 6} }) g = floor(g[2]) g[2] = g[1] & g[2] graph[i] = g[2..length(g)] end for end procedure setup() WinMain( win, Normal )
Matt Lewis