Author Topic: graphs with QB64 and how to scroll the screen.  (Read 7673 times)

0 Members and 1 Guest are viewing this topic.

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
graphs with QB64 and how to scroll the screen.
« on: January 17, 2021, 12:07:27 pm »
Hi. I have just finished the excellent tutorials by Terry Ritchie.

I have an ambitious plan to do some engineering program with QB64. So, in QB64, there are some QB64 command in order to do cartesian graphs? For example, if I want to draw in a graph y=x^2.

An other question is: how to scroll the screen?
For example:

for i%=1 to 1000
print i%
next i%

How can I see all the values?

Thanks

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: graphs with QB64 and how to scroll the screen.
« Reply #1 on: January 17, 2021, 12:27:12 pm »
Screen scrolling with mouse, mouse wheel, arrows? https://www.tapatalk.com/groups/qbasic/i-m-working-on-a-scrollbar-rotine-t39417.html#p212427

You could make something simple, with just arrows, too.

I have improved that routine a lot, for a WP I'm working on, but it's a decent enough example in less lines, to see what's needed for someone who is making their own program. Maybe you can use some of it or most of it or wait for others to post key examples, etc. I have some of those someplace on this forum, as well. MAybe do a search for scrolling?

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: graphs with QB64 and how to scroll the screen.
« Reply #2 on: January 17, 2021, 08:09:14 pm »
Hi Bartok
you have said that you finished the tutorial at https://www.qb64sourcecode.com of Terry Ritchie....
well but you have done the homeworks? (at the end of each section there are homeworks)

First Homework
if I want to draw in a graph y=x^2 on a Cartesian cohordinates

however at task7 you find ScreenDemo.BAS and if you copy and run it  you get the more informations about graphic mode.
If you read these two keywords VIEW http://qb64.org/wiki/VIEW and WINDOWhttp://qb64.org/wiki/WINDOW so you can set the area of the screen to draw and you can set the cohordinates used on that area and moreover you can use the Cartesian cohordinates with the 0 at the bottom and the max value at the top

I think that now you have enough informations to solve your question about drawing a math equation!

Second Homework

 how to scroll the screen? ( you must realize that screen (visible output) can be moved in all 4 directions by code

for example:
Code: QB64: [Select]
  1. for i%=1 to 1000
  2. next i%
  3.  
  4.  
How can I see all the values?

well you have already the informations
read here the paragraph titled Your First Programhttps://www.qb64sourcecode.com/task3.html
specifically this
Quote
By default QB64 opens a pure text window (known as SCREEN 0) and runs programs in text mode. Figure 2 above shows the output of your program in an 80 character wide by 25 line high text screen.
in Qbasic/QB45/QB64 if you don't set any kind of SCREEN mode , it has set to SCREEN 0 and with  WIDTH 80, 25
here wiki reference for WIDTH http://qb64.org/wiki/WIDTH and SCREEN http://qb64.org/wiki/SCREEN
BUT as you have understood the video has 25 lines of text and using WIDTH you can reach 50 lines of text...
do you know what happens when you print more than the max number of text set to the screen?
The output that is already on the screen scrolls up and the new text output is showed at bottom of the screen.
If you repeat this action for so many times as the max number of line of text , you have changed the whole output!

With you code you can see always the last 25/43/50 lines of text depending by the settings made by WIDTH.
The informations that you want see has gone up out of the screen, they are lost! To perform a vertical scrolling of text you must store the informations (variables or file of text) and then you can use a pointer to the first line of informations showed to perform the vertical scrolling of text.
So to use variables you like to use Array that you have learnt here https://www.qb64sourcecode.com/Task10.html
and to use keyboard input and mouse input you can read again this lesson https://www.qb64sourcecode.com/Task9.html specifically you get informations from InkeyMenu.BAS

After too many words I say Welcome to the forum
and post here your solutions
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: graphs with QB64 and how to scroll the screen.
« Reply #3 on: January 18, 2021, 11:33:44 am »
Plotting any old function not simple because of scaling and errors like division by 0 or SQR(negative numbers)

Code: QB64: [Select]
  1. _TITLE "Plot > Point mouse for coordinates" ' started 2019-09-14 B+
  2. ' from: Remake Eval  2018-09-16 5:53 PM
  3. ' goals test remake eval and plot math functions
  4. '2019-09-14 basic plot working, now for sensable ticks on axis
  5. ' OK maybe do some replotting with mousedown and setting up new rectangle area
  6. ' On the other hand we could plot data from a file?
  7.  
  8.  
  9. CONST XMAX = 1200
  10. CONST YMAX = 700
  11.  
  12. SCREEN _NEWIMAGE(XMAX, YMAX, 32)
  13.  
  14. debug = 0
  15.  
  16. 'evaluate$ and evalW setup
  17. DIM SHARED DFlag AS _BIT, EvalErr$, GlobalX AS _FLOAT, RAD AS _FLOAT, DEG AS _FLOAT
  18. DFlag = 0
  19. EvalErr$ = ""
  20. GlobalX = 5 'changeable
  21. RAD = _PI / 180.0
  22. DEG = 180 / _PI
  23. REDIM SHARED fList(1 TO 1) AS STRING
  24. Split "int, sin, cos, tan, asin, acos, atan, log, exp, sqr, rad, deg,", ", ", fList()
  25. REDIM SHARED oList(1 TO 1) AS STRING
  26. Split "^, %, /, *, -, +, =, <, >, <=, >=, <>, or, and, not", ", ", oList()
  27.  
  28. 'DIM x
  29.  
  30. plot "x^2, -50, 50"
  31. 'plot "sin(x)^2 + sin(x), -6.3, 6.3"
  32.  
  33. SUB plot (pList$)
  34.     REDIM p(1 TO 1) AS STRING, table(1000)
  35.     DIM func$, LX, UX, dx, dy, x, y, LY, UY, clicks, midY, s$, mx, my, gx, gy
  36.     Split pList$, ",", p()
  37.     func$ = p(1): LX = VAL(p(2)): UX = VAL(p(3))
  38.     dx = (UX - LX) / 1000
  39.     FOR x = 0 TO 1000
  40.         GlobalX = LX + x * dx
  41.         table(x) = VAL(Evaluate$(func$))
  42.         IF x = 0 THEN
  43.             LY = table(x): UY = table(x)
  44.         ELSE
  45.             IF table(x) < LY THEN LY = table(x)
  46.             IF table(x) > UY THEN UY = table(x)
  47.         END IF
  48.     NEXT
  49.     dy = (UY - LY) / 500
  50.  
  51.     clicks = (UX - LX) / 20
  52.     COLOR &HFF000000, &HFFFFFFFF: CLS
  53.     yCP 42, "Plot " + func$
  54.     yCP 640, "For x = " + dp2$(LX) + " to " + dp2$(UX) + " steps every " + dp2$(clicks)
  55.     LINE (100, 100)-STEP(1000, 500), , B
  56.     FOR x = 0 TO 20
  57.         LINE (100 + x * 50, 595)-STEP(0, 10)
  58.     NEXT
  59.     IF 0 > LX AND 0 < UX THEN LINE ((0 - LX) / dx + 100, 100)-STEP(0, 500): _PRINTSTRING ((0 - LX) / dx + 100 - 12, 605), "x=0"
  60.  
  61.     clicks = (UY - LY) / 10
  62.     IF 0 > LY AND 0 < UY THEN
  63.         midY = 600 - (0 - LY) / dy
  64.         LINE (100, midY)-STEP(1000, 0): _PRINTSTRING (43, midY + -8), "F(x)=0"
  65.         FOR y = -500 TO 500 STEP 50
  66.             IF midY + y >= 100 AND midY + y <= 600 AND y <> 0 THEN
  67.                 LINE (95, midY + y)-STEP(10, 0)
  68.                 s$ = RIGHT$(SPACE$(11) + dp2$(clicks * y / -50), 11)
  69.                 _PRINTSTRING (0, midY + y - 8), s$
  70.             END IF
  71.         NEXT
  72.     ELSE
  73.         FOR y = 0 TO 10
  74.             LINE (95, 100 + y * 50)-STEP(10, 0)
  75.             s$ = RIGHT$(SPACE$(11) + dp2$(LY + clicks * y), 11)
  76.             _PRINTSTRING (0, 600 - y * 50 - 8), s$
  77.         NEXT
  78.     END IF
  79.     FOR x = 0 TO 1000
  80.         y = (table(x) - LY) / dy
  81.         LINE (x + 100, 600 - y)-STEP(2, 2), &HFF0000FF, BF
  82.         PSET (x + 100, 600 - y), &HFF0000FF
  83.     NEXT
  84.     WHILE _KEYDOWN(27) = 0
  85.         WHILE _MOUSEINPUT: WEND
  86.         mx = _MOUSEX: my = _MOUSEY
  87.         IF mx <= 1100 AND mx >= 100 THEN
  88.             IF my >= 100 AND my <= 600 THEN
  89.                 yCP 80, SPACE$(50)
  90.                 gx = (mx - 100) / 1000 * (UX - LX) + LX
  91.                 gy = (600 - my) / 500 * (UY - LY) + LY
  92.                 yCP 80, "X = " + dp2$(gx) + ", Y = " + dp2$(gy)
  93.             END IF
  94.         END IF
  95.         _DISPLAY
  96.         _LIMIT 200
  97.     WEND
  98.  
  99. 'this preps e$ string for actual evaluation function and makes call to it,
  100. 'checks results for error returns that or string form of result calculation
  101. 'the new goal is to do string functions along side math
  102. FUNCTION Evaluate$ (e$)
  103.     DIM b$, c$
  104.     DIM i AS INTEGER, po AS INTEGER
  105.     ' isolateNeg = 0
  106.     b$ = "" 'rebuild string with padded spaces
  107.     'this makes sure ( ) + * / % ^ are wrapped with spaces, on your own with - sign
  108.     FOR i = 1 TO LEN(e$) 'filter chars and count ()
  109.         c$ = LCASE$(MID$(e$, i, 1))
  110.         IF c$ = ")" THEN
  111.             po = po - 1: b$ = b$ + " ) "
  112.         ELSEIF c$ = "(" THEN
  113.             po = po + 1: b$ = b$ + " ( "
  114.         ELSEIF INSTR("+*/%^", c$) > 0 THEN
  115.             b$ = b$ + " " + c$ + " "
  116.         ELSEIF c$ = "-" THEN
  117.             IF LEN(b$) > 0 THEN
  118.                 IF INSTR(".0123456789abcdefghijklmnopqrstuvwxyz)", RIGHT$(RTRIM$(b$), 1)) > 0 THEN
  119.                     b$ = b$ + " " + c$ + " "
  120.                 ELSE
  121.                     b$ = b$ + " " + c$
  122.                 END IF
  123.             ELSE
  124.                 b$ = b$ + " " + c$
  125.             END IF
  126.         ELSEIF INSTR(" .0123456789abcdefghijklmnopqrstuvwxyz<>=", c$) > 0 THEN
  127.             b$ = b$ + c$
  128.         END IF
  129.         IF po < 0 THEN EvalErr$ = "Too many )": EXIT FUNCTION
  130.     NEXT
  131.     IF po <> 0 THEN EvalErr$ = "Unbalanced ()": EXIT FUNCTION
  132.     REDIM ev(1 TO 1) AS STRING
  133.     Split b$, " ", ev()
  134.     FOR i = LBOUND(ev) TO UBOUND(ev) 'subst constants
  135.         IF ev(i) = "pi" THEN
  136.             ev(i) = LTRIM$(STR$(_PI))
  137.         ELSEIF ev(i) = "x" THEN
  138.             ev(i) = LTRIM$(STR$(GlobalX))
  139.         ELSEIF ev(i) = "e" THEN
  140.             ev(i) = LTRIM$(STR$(EXP(1)))
  141.         END IF
  142.     NEXT
  143.     c$ = evalW$(ev())
  144.     IF EvalErr$ <> "" THEN Evaluate$ = EvalErr$ ELSE Evaluate$ = c$
  145.  
  146.  
  147. ' the recursive part of EVAL
  148. FUNCTION evalW$ (a() AS STRING)
  149.     IF EvalErr$ <> "" THEN EXIT FUNCTION
  150.  
  151.     DIM fun$, test$, innerV$, m$, op$
  152.     DIM pop AS INTEGER, lPlace AS INTEGER, i AS INTEGER, rPlace AS INTEGER
  153.     DIM po AS INTEGER, p AS INTEGER, o AS INTEGER, index AS INTEGER
  154.     DIM recurs AS INTEGER
  155.     DIM innerVal AS _FLOAT, a AS _FLOAT, b AS _FLOAT
  156.     IF debug THEN
  157.         PRINT "evalW rec'd a() as:"
  158.         FOR i = LBOUND(a) TO UBOUND(a)
  159.             PRINT a(i); ", ";
  160.         NEXT
  161.         PRINT: INPUT "OK enter"; test$: PRINT
  162.     END IF
  163.     pop = find%(a(), "(") 'parenthesis open place
  164.     WHILE pop > 0
  165.         IF pop = 1 THEN
  166.             fun$ = "": lPlace = 1
  167.         ELSE
  168.             test$ = a(pop - 1)
  169.             IF find%(fList(), test$) > 0 THEN
  170.                 fun$ = test$: lPlace = pop - 1
  171.             ELSE
  172.                 fun$ = "": lPlace = pop
  173.             END IF
  174.         END IF
  175.         po = 1
  176.         FOR i = pop + 1 TO UBOUND(a)
  177.             IF a(i) = "(" THEN po = po + 1
  178.             IF a(i) = ")" THEN po = po - 1
  179.             IF po = 0 THEN rPlace = i: EXIT FOR
  180.         NEXT
  181.         REDIM inner(1 TO 1) AS STRING: index = 0: recurs = 0
  182.         FOR i = (pop + 1) TO (rPlace - 1)
  183.             index = index + 1
  184.             REDIM _PRESERVE inner(1 TO index) AS STRING
  185.             inner(index) = a(i)
  186.             IF find%(oList(), a(i)) > 0 THEN recurs = -1
  187.         NEXT
  188.         IF recurs THEN innerV$ = evalW$(inner()) ELSE innerV$ = a(pop + 1)
  189.         innerVal = VAL(innerV$)
  190.  
  191.         SELECT CASE fun$
  192.             CASE "": m$ = innerV$
  193.             CASE "int": m$ = ls$(INT(innerVal))
  194.             CASE "sin": IF DFlag THEN m$ = ls$(SIN(RAD * innerVal)) ELSE m$ = ls$(SIN(innerVal))
  195.             CASE "cos": IF DFlag THEN m$ = ls$(COS(RAD * innerVal)) ELSE m$ = ls$(COS(innerVal))
  196.             CASE "tan": IF DFlag THEN m$ = ls$(TAN(RAD * innerVal)) ELSE m$ = ls$(TAN(innerVal))
  197.             CASE "asin": IF DFlag THEN m$ = ls$(_ASIN(RAD * innerVal)) ELSE m$ = ls$(_ASIN(innerVal))
  198.             CASE "acos": IF DFlag THEN m$ = ls$(_ACOS(RAD * innerVal)) ELSE m$ = ls$(_ACOS(innerVal))
  199.             CASE "atan": IF DFlag THEN m$ = ls$(ATN(RAD * innerVal)) ELSE m$ = ls$(ATN(innerVal))
  200.             CASE "log"
  201.                 IF innerVal > 0 THEN
  202.                     m$ = ls$(LOG(innerVal))
  203.                 ELSE
  204.                     EvalErr$ = "LOG only works on numbers > 0.": EXIT FUNCTION
  205.                 END IF
  206.             CASE "exp" 'the error limit is inconsistent in JB
  207.                 IF -745 <= innerVal AND innerVal <= 709 THEN 'your system may have different results
  208.                     m$ = ls$(EXP(innerVal))
  209.                 ELSE
  210.                     'what the heck???? 708 works fine all alone as limit ?????
  211.                     EvalErr$ = "EXP(n) only works for n = -745 to 709.": EXIT FUNCTION
  212.                 END IF
  213.             CASE "sqr"
  214.                 IF innerVal >= 0 THEN
  215.                     m$ = ls$(SQR(innerVal))
  216.                 ELSE
  217.                     EvalErr$ = "SQR only works for numbers >= 0.": EXIT FUNCTION
  218.                 END IF
  219.             CASE "rad": m$ = ls$(innerVal * RAD)
  220.             CASE "deg": m$ = ls$(innerVal * DEG)
  221.             CASE ELSE: EvalErr$ = "Unidentified function " + fun$: EXIT FUNCTION
  222.         END SELECT
  223.         IF debug THEN
  224.             PRINT "lPlace, rPlace"; lPlace, rPlace
  225.         END IF
  226.         arrSubst a(), lPlace, rPlace, m$
  227.         IF debug THEN
  228.             PRINT "After arrSubst a() is:"
  229.             FOR i = LBOUND(a) TO UBOUND(a)
  230.                 PRINT a(i); " ";
  231.             NEXT
  232.             PRINT: PRINT
  233.         END IF
  234.         pop = find%(a(), "(")
  235.     WEND
  236.  
  237.     'all parenthesis cleared
  238.     'ops$ = "% ^ / * + - = < > <= >= <> and or not" 'all () cleared, now for binary ops (not not binary but is last!)
  239.     FOR o = 1 TO 15
  240.         op$ = oList(o)
  241.         p = find%(a(), op$)
  242.         WHILE p > 0
  243.             a = VAL(a(p - 1))
  244.             b = VAL(a(p + 1))
  245.             IF debug THEN
  246.                 PRINT STR$(a) + op$ + STR$(b)
  247.             END IF
  248.             SELECT CASE op$
  249.                 CASE "%"
  250.                     IF b >= 2 THEN
  251.                         m$ = ls$(INT(a) MOD INT(b))
  252.                     ELSE
  253.                         EvalErr$ = "For a Mod b, b value < 2."
  254.                         EXIT FUNCTION
  255.                     END IF
  256.                 CASE "^"
  257.                     IF INT(b) = b OR a >= 0 THEN
  258.                         m$ = ls$(a ^ b)
  259.                     ELSE
  260.                         EvalErr$ = "For a ^ b, a needs to be >= 0 when b not integer."
  261.                         EXIT FUNCTION
  262.                     END IF
  263.                 CASE "/"
  264.                     IF b <> 0 THEN
  265.                         m$ = ls$(a / b)
  266.                     ELSE
  267.                         EvalErr$ = "Div by 0"
  268.                         EXIT FUNCTION
  269.                     END IF
  270.                 CASE "*": m$ = ls$(a * b)
  271.                 CASE "-": m$ = ls$(a - b)
  272.                 CASE "+": m$ = ls$(a + b)
  273.                 CASE "=": IF a = b THEN m$ = "-1" ELSE m$ = "0"
  274.                 CASE "<": IF a < b THEN m$ = "-1" ELSE m$ = "0"
  275.                 CASE ">": IF a > b THEN m$ = "-1" ELSE m$ = "0"
  276.                 CASE "<=": IF a <= b THEN m$ = "-1" ELSE m$ = "0"
  277.                 CASE ">=": IF a >= b THEN m$ = "-1" ELSE m$ = "0"
  278.                 CASE "<>": IF a <> b THEN m$ = "-1" ELSE m$ = "0"
  279.                 CASE "and": IF a <> 0 AND b <> 0 THEN m$ = "-1" ELSE m$ = "0"
  280.                 CASE "or": IF a <> 0 OR b <> 0 THEN m$ = "-1" ELSE m$ = "0"
  281.                 CASE "not": IF b = 0 THEN m$ = "-1" ELSE m$ = "0" 'use b as nothing should be left of not
  282.             END SELECT
  283.             arrSubst a(), p - 1, p + 1, m$
  284.  
  285.             IF debug THEN
  286.                 PRINT "a() reloaded after " + op$ + " as:"
  287.                 FOR i = LBOUND(a) TO UBOUND(a)
  288.                     PRINT a(i); ", ";
  289.                 NEXT
  290.                 PRINT: PRINT
  291.             END IF
  292.  
  293.             p = find%(a(), op$)
  294.         WEND
  295.     NEXT
  296.     fun$ = ""
  297.     FOR i = LBOUND(a) TO UBOUND(a)
  298.         fun$ = fun$ + " " + a(i)
  299.     NEXT
  300.     evalW$ = LTRIM$(fun$)
  301.  
  302. SUB arrSubst (a() AS STRING, substLow AS LONG, substHigh AS LONG, subst AS STRING)
  303.     DIM i AS LONG, index AS LONG
  304.     a(substLow) = subst: index = substLow + 1
  305.     FOR i = substHigh + 1 TO UBOUND(a)
  306.         a(index) = a(i): index = index + 1
  307.     NEXT
  308.     REDIM _PRESERVE a(LBOUND(a) TO UBOUND(a) + substLow - substHigh)
  309.  
  310. 'notes: REDIM the array(0) to be loaded before calling Split '<<<<<<<<<<<<<<<<<<<<<<< IMPORTANT!!!!
  311. SUB Split (mystr AS STRING, delim AS STRING, arr() AS STRING)
  312.     ' bplus modifications of Galleon fix of Bulrush Split reply #13
  313.     ' http://www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=1612.0
  314.     ' this sub further developed and tested here: \test\Strings\Split test.bas
  315.     ' 2018-09-16 modified for base 1 arrays
  316.     DIM copy AS STRING, p AS LONG, curpos AS LONG, arrpos AS LONG, lc AS LONG, dpos AS LONG
  317.     copy = mystr 'make copy since we are messing with mystr
  318.     'special case if delim is space, probably want to remove all excess space
  319.     IF delim = " " THEN
  320.         copy = RTRIM$(LTRIM$(copy))
  321.         p = INSTR(copy, "  ")
  322.         WHILE p > 0
  323.             copy = MID$(copy, 1, p - 1) + MID$(copy, p + 1)
  324.             p = INSTR(copy, "  ")
  325.         WEND
  326.     END IF
  327.     REDIM arr(1 TO 1) 'clear it
  328.     curpos = 1
  329.     arrpos = 1
  330.     lc = LEN(copy)
  331.     dpos = INSTR(curpos, copy, delim)
  332.     DO UNTIL dpos = 0
  333.         arr(arrpos) = MID$(copy, curpos, dpos - curpos)
  334.         arrpos = arrpos + 1
  335.         REDIM _PRESERVE arr(1 TO arrpos + 1) AS STRING
  336.         curpos = dpos + LEN(delim)
  337.         dpos = INSTR(curpos, copy, delim)
  338.     LOOP
  339.     arr(arrpos) = MID$(copy, curpos)
  340.     REDIM _PRESERVE arr(1 TO arrpos) AS STRING
  341.  
  342. 'assume a() is base 1 array so if find comes back as 0 then found nothing
  343. FUNCTION find% (a() AS STRING, s$)
  344.     DIM i%
  345.     FOR i% = LBOUND(a) TO UBOUND(a)
  346.         IF a(i%) = s$ THEN find% = i%: EXIT FUNCTION
  347.     NEXT
  348.  
  349. 'ltrim a number float
  350.     ls$ = LTRIM$(STR$(n))
  351.  
  352. 'FUNCTION xDP$ (x, DP)
  353. '    DIM xs$, dot AS INTEGER
  354.  
  355. '    IF x < 0 THEN xs$ = STR$(x - .5 * 10 ^ -DP)
  356. '    IF x > 0 THEN xs$ = STR$(x + .5 * 10 ^ -DP)
  357. '    IF INSTR(xs$, "D") > 0 OR INSTR(xs$, "E") > 0 THEN EXIT FUNCTION 'not dealing with exponents today
  358. '    dot = INSTR(xs$, ".")
  359. '    IF xs$ = "" OR ABS(x) < .5 * 10 ^ -DP THEN xs$ = "0"
  360. '    IF dot THEN xDP$ = MID$(xs$, 1, dot) + MID$(xs$, dot + 1, DP) ELSE xDP$ = xs$
  361. 'END FUNCTION
  362.  
  363. SUB yCP (y, s$) 'for xmax pixel wide graphics screen Center Print at pixel y row
  364.     _PRINTSTRING ((_WIDTH - LEN(s$) * 8) / 2, y), s$
  365.  
  366. FUNCTION dp2$ (n)
  367.     dp2$ = _TRIM$(STR$(INT(n * 100) / 100))
  368.  
  369.  
  370.  
  371.  

  [ You are not allowed to view this attachment ]  


But I suggest study of WINDOW command to create your own convenient Coordinate System.



« Last Edit: January 18, 2021, 11:40:20 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: graphs with QB64 and how to scroll the screen.
« Reply #4 on: January 18, 2021, 11:38:19 am »
Quote
for i%=1 to 1000
print i%
next i%

This is easy! If don't insist on screen scrolling but view 20 lines at a time, a little mod and you can move up and down an array list, perfect for newbies!
FOR i% = 1 TO 1000
    PRINT i%
    IF i% MOD 20 = 0 THEN SLEEP: CLS
NEXT i%

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: graphs with QB64 and how to scroll the screen.
« Reply #5 on: January 18, 2021, 11:43:38 am »
This is easy! If don't insist on screen scrolling but view 20 lines at a time, a little mod and you can move up and down an array list, perfect for newbies!
FOR i% = 1 TO 1000
    PRINT i%
    IF i% MOD 20 = 0 THEN SLEEP: CLS
NEXT i%

Like this:

Code: QB64: [Select]
  1.  DIM Array(10) AS STRING
  2. DATA Apple,Banana,Cherry,Date,Fig,Grape,Huckleberry,Iced Fruit,Jambolan,Kiwi,Lemon
  3. FOR i = 0 TO 10
  4.     READ Array(i)
  5.  
  6.  
  7.  
  8. choice = 0
  9.     CLS
  10.     DisplayList 10, 10, 20, 5, choice, Array(), -1
  11.     k = _KEYHIT
  12.     SELECT CASE k
  13.         CASE 18432
  14.             choice = choice - 1
  15.             IF choice < 0 THEN choice = 0
  16.         CASE 20480
  17.             choice = choice + 1
  18.             IF choice > 10 THEN choice = 10
  19.     END SELECT
  20.     _DISPLAY
  21.     _LIMIT 30
  22.  
  23.  
  24.  
  25. SUB DisplayList (x AS INTEGER, y AS INTEGER, w AS INTEGER, l AS INTEGER, s AS INTEGER, choices() AS STRING, numbered AS _BYTE)
  26.     'x/y location to place the start of our list
  27.     'w is the width of our list on the screen
  28.     'l is the length of the list items we want to display at a time
  29.     's is the starting element that we want to display on the screen
  30.     'choices() is the array that holds the actual list for us
  31.     'numbered is the toggle for if we want to autonumber our list or not.
  32.     '     0 says we don't want to number the list; just display it.
  33.     '     A value less than 0 says we want to display the number index of the visible list
  34.     '     A value greater than 0 says we want to display the number of visible elements from the list on the screen.
  35.  
  36.  
  37.     'Some basic error checking is in need here
  38.     IF s < LBOUND(choices) THEN s = LBOUND(choices)
  39.     IF s + l - 1 > UBOUND(choices) THEN l = UBOUND(choices) - s + 1
  40.  
  41.     LOCATE x
  42.     start = s: finish = s + l - 1
  43.     FOR i = start TO finish
  44.         counter = counter + 1
  45.         IF numbered > 0 THEN counter$ = LTRIM$(STR$(counter)) + ") "
  46.         IF numbered < 0 THEN counter$ = LTRIM$(STR$(counter + start - 1)) + ") "
  47.         LOCATE , y: PRINT counter$ + LEFT$(choices(i), w - LEN(counter$))
  48.     NEXT
  49.  
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: graphs with QB64 and how to scroll the screen.
« Reply #6 on: January 18, 2021, 01:29:10 pm »
Oh, you want easy?

_WIDTH 80, 1001

Then you don't have to scroll the screen, just the window! :D

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: graphs with QB64 and how to scroll the screen.
« Reply #7 on: January 18, 2021, 04:08:46 pm »
Hi everyone,
TempodiBasic,
Quote
well but you have done the homeworks? (at the end of each section there are homeworks)
[...]
and post here your solutions

yes, I have done all the tasks. If not, I wouldn't have been able to do that:

Code: QB64: [Select]
  1. CONST L% = 1024
  2. CONST H% = 768
  3. CONST rosso = _RGB32(255, 0, 0)
  4. CONST bianco = _RGB32(255, 255, 255)
  5.  
  6. TYPE funzione
  7.     x AS LONG
  8.     y AS LONG
  9.  
  10. DIM SHARED schermo&, grafico1&, grafico2&
  11. DIM keypress$
  12.  
  13. schermo& = _NEWIMAGE(L%, H%, 32)
  14. grafico1& = _NEWIMAGE(800, 600, 32)
  15. grafico2& = _NEWIMAGE(750, 550, 32)
  16.  
  17. disegno
  18.     keypress$ = INKEY$
  19.     IF keypress$ = CHR$(13) THEN
  20.         disegno
  21.     END IF
  22. LOOP UNTIL keypress$ = CHR$(27)
  23.  
  24. _FREEIMAGE grafico1&
  25. _FREEIMAGE grafico2&
  26.  
  27. SUB disegno ()
  28.     CONST A% = 5
  29.     CONST k% = 2
  30.     DIM titolo$, N$, istruzioni$
  31.     DIM punti%, i%, s%
  32.     REDIM funzione(0) AS funzione
  33.     s% = 5 * k%
  34.     DO
  35.         SCREEN schermo&
  36.         _CLEARCOLOR rosso, grafico2&
  37.         _DEST schermo&
  38.         CLS
  39.         titolo$ = "RAPPRESENTAZIONE DELLA FUNZIONE y = kxý (x>=0 ; k ="
  40.         LOCATE 2, ((L% / 8 - LEN(titolo$) - 3) \ 2): PRINT titolo$; STR$(k%); ")"
  41.         PRINT
  42.         _DEST grafico1&
  43.         CLS
  44.         LINE (0, 0)-(799, 599), rosso, B
  45.         LINE (49, 49)-(49, 549), bianco
  46.         LINE -(749, 549), bianco
  47.         PSET (49, 49), bianco: DRAW "F20": PSET (49, 49), bianco: DRAW "G20": LOCATE 50 \ 16, 50 \ 8 + 1: PRINT "y"
  48.         PSET (749, 549), bianco: DRAW "G20": PSET (749, 549), bianco: DRAW "H20": LOCATE 550 \ 16 + 1, 750 \ 8 + 2: PRINT "x"
  49.         LOCATE 550 \ 16 + 1, 50 \ 8: PRINT "0"
  50.         _PUTIMAGE ((L% - 800) \ 2, (H% - 600) \ 2), grafico1&, schermo&
  51.         _DEST schermo&
  52.         N$ = "Inserire il n. di punti in ascissa: "
  53.         LOCATE 4, ((L% / 8 - LEN(N$) - 1) \ 2)
  54.         PRINT N$;
  55.         INPUT "", punti%
  56.     LOOP WHILE LTRIM$(STR$(punti%)) = "0"
  57.     _DEST grafico2&
  58.     funzione(0).x = 0
  59.     funzione(0).y = 0
  60.     FOR i% = 1 TO punti%
  61.         REDIM _PRESERVE funzione(i%) AS funzione
  62.         funzione(i%).x = i%
  63.         funzione(i%).y = k% * (funzione(i%).x) ^ 2
  64.         LINE (s% * A% * funzione(i% - 1).x, A% * funzione(i% - 1).y)-(s% * A% * funzione(i%).x, A% * funzione(i%).y), rosso
  65.         CIRCLE (s% * A% * funzione(i%).x, A% * funzione(i%).y), 3, rosso
  66.         PAINT (s% * A% * funzione(i%).x + 1, A% * funzione(i%).y + 1), rosso
  67.     NEXT i%
  68.     _PUTIMAGE (49, 599 - 49)-(799, 49), grafico2&, grafico1&, (0, 0)-(749, 549)
  69.     _PUTIMAGE ((L% - 800) \ 2, (H% - 600) \ 2), grafico1&, schermo&
  70.     _DEST schermo&
  71.     istruzioni$ = "Premere INVIO per un nuovo calcolo, ESC per terminare."
  72.     LOCATE 45, ((L% / 8 - LEN(istruzioni$)) \ 2): PRINT istruzioni$
  73.     FOR i% = 0 TO punti%
  74.         LOCATE 10 + (punti% - (punti% - i%)), 80
  75.         PRINT "x ("; i%; ") ="; funzione(i%).x; "; y ("; i%; ") ="; funzione(i%).y
  76.     NEXT i%
  77.  

The tutorials are focused to make games, not mathematical graphs. I want to plot graphs like the one attached, so I asked help just in order to know more specific QB64 commands, like just VIEW and WINDOWS. So, thanks for the tip.

Quote
After too many words I say Welcome to the forum

Thanks!

bplus,
Quote
Plotting any old function not simple because of scaling and errors like division by 0 or SQR(negative numbers)
[...]
But I suggest study of WINDOW command to create your own convenient Coordinate System.


thank you and thank you all: I will study your codes carefully.
« Last Edit: January 18, 2021, 04:15:13 pm by bartok »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: graphs with QB64 and how to scroll the screen.
« Reply #8 on: January 18, 2021, 07:11:33 pm »
Hi Bartok
again welcome into QB64 forum.
fine to see some your good code
It works fine! I see that you have chosen to plot the positive quadrant of Cartesian plane.
Good job!

PS (se devo proprio cercare il pelo nell'uovo)
If you like to get feedback you can see how your program works if the user type a negative number or a numer more than 33.
The fun is that if you type a negative number it has no results for these line of code
Code: QB64: [Select]
  1.   FOR i% = 1 TO punti%
the FOR as default has STEP a +1, if you had used another loop for output of points the result wouldn't be safe from error.
For the second issue I think that you can improve the output chosing if you want to adapt the area of the output by using WINDOW to the number of point typed from user and  a large range of points to draw or if you want to set a low range of points to draw (1-33 is the actual range)
PSS
if you use WINDOW you'll try interestin PMAP http://qb64.org/wiki/PMAP
Again good job and thanks to share your ideas and code.
« Last Edit: January 18, 2021, 07:14:39 pm by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: graphs with QB64 and how to scroll the screen.
« Reply #9 on: January 19, 2021, 10:50:43 am »
Hi Tempodi Basic,
I like feedback and "fai bene a cercare il pelo nell'uovo"):D. I have improved the code on the base of my current  knowledge, with your remarks. This only a kind of outline in order to more complicated things.
Code: QB64: [Select]
  1. CONST L% = 1024
  2. CONST H% = 768
  3. CONST rosso = _RGB32(255, 0, 0)
  4. CONST bianco = _RGB32(255, 255, 255)
  5.  
  6. TYPE funzione
  7.     x AS LONG
  8.     y AS LONG
  9.  
  10. DIM SHARED schermo&, grafico1&, grafico2&
  11. DIM keypress$
  12.  
  13. schermo& = _NEWIMAGE(L%, H%, 32)
  14. grafico1& = _NEWIMAGE(800, 600, 32)
  15. grafico2& = _NEWIMAGE(800, 600, 32)
  16.  
  17. disegno
  18.     keypress$ = INKEY$
  19.     IF keypress$ = CHR$(13) THEN
  20.         disegno
  21.     END IF
  22. LOOP UNTIL keypress$ = CHR$(27)
  23. _FREEIMAGE grafico1&
  24. _FREEIMAGE grafico2&
  25.  
  26. SUB disegno ()
  27.     CONST A% = 1 'per prendere un punto ogni A% pixel.
  28.     CONST k! = 1 ' coefficiente della x.
  29.     CONST m% = 2 'esponente della x.
  30.     CONST s! = 5 * k! 'amplificazione di scala delle ascisse.
  31.     DIM titolo1$, titolo2$, punti$, istruzioni$
  32.     DIM punti%, i%, n% 'valori di x considerati da -punti% a +punti%, i% e n% sono contatori.
  33.     REDIM funzione(0) AS funzione
  34.     i% = 1
  35.     DO 'disegna cartiglio, assi, scrive istruzioni e chiede il n. di punti. non accetta come valore "0".
  36.         SCREEN schermo& 'visualizza schermo&.
  37.         _CLEARCOLOR rosso, grafico2&
  38.         _DEST schermo& 'le istruzioni che seguono agiscono su schermo&, che e' visualizzato.
  39.         CLS
  40.         titolo1$ = "RAPPRESENTAZIONE DELLA FUNZIONE y = kx^m (k =" + STR$(k!) + " ; m =" + STR$(m%) + ")"
  41.         LOCATE 2, ((L% / 8 - LEN(titolo1$)) \ 2): PRINT titolo1$
  42.         titolo2$ = "(Fattore di amplificazione delle ascisse: s = " + STR$(s!) + ")"
  43.         LOCATE 3, ((L% / 8 - LEN(titolo2$)) \ 2): PRINT titolo2$
  44.         _DEST grafico1& 'le istruzioni che seguono agiscono in background su grafico1&.
  45.         CLS
  46.         LINE (0, 0)-(799, 599), rosso, B
  47.         LINE (_WIDTH(grafico1&) \ 2 - 1, 31)-(_WIDTH(grafico1&) \ 2 - 1, 568), bianco
  48.         LINE (31, _HEIGHT(grafico1&) \ 2)-(768, _HEIGHT(grafico1&) \ 2), bianco
  49.         PSET (_WIDTH(grafico1&) \ 2, 31), bianco: DRAW "F20": PSET (_WIDTH(grafico1&) \ 2, 31), bianco: DRAW "G20": LOCATE 32 / 16, _WIDTH(grafico1&) \ 2 \ 8 + 1: PRINT "y"
  50.         PSET (768, _HEIGHT(grafico1&) \ 2), bianco: DRAW "G20": PSET (768, _HEIGHT(grafico1&) \ 2), bianco: DRAW "H20": LOCATE _HEIGHT(grafico1&) \ 2 \ 16 + 1, 768 \ 8 + 2: PRINT "x"
  51.         LOCATE _HEIGHT(grafico1&) \ 2 \ 16 + 2, _WIDTH(grafico1&) \ 2 \ 8 - 1: PRINT "0"
  52.         _PUTIMAGE ((L% - 800) \ 2, (H% - 600) \ 2), grafico1&, schermo&
  53.         _DEST schermo& 'le iscruzioni che seguono agiscono su schermo&, che e' visualizzato.
  54.         punti$ = "Inserire il n. di punti in ascissa: "
  55.         LOCATE 5, ((L% / 8 - LEN(punti$) - 1) \ 2)
  56.         PRINT punti$;
  57.         INPUT "", punti%
  58.     LOOP WHILE LTRIM$(STR$(punti%)) = "0"
  59.     _DEST grafico2& 'le istruzioni che seguono agiscono in background su grafico2&.
  60.     n% = -punti%
  61.     DO UNTIL n% = punti% + 1 ' crea il vettore con i valori di x e y i cui elementi vanno da -punti% a +punti%. disegna un cerchio colorato per ogni punto, traslando il grafico in modo che l'orgine non sia in alto a sinistra, ma su "0".
  62.         REDIM _PRESERVE funzione(i%) AS funzione
  63.         funzione(i%).x = n%
  64.         funzione(i%).y = k! * (funzione(i%).x) ^ m
  65.         CIRCLE (s! * A% * funzione(i%).x + 399, A% * funzione(i%).y + 299), 2, rosso
  66.         PAINT (s! * A% * funzione(i%).x + 399 + 1, A% * funzione(i%).y + 299 + 1), rosso
  67.         i% = i% + 1
  68.         n% = n% + 1
  69.     LOOP
  70.     FOR i% = 1 TO UBOUND(funzione) - 1 ' congiunge i punti.
  71.         LINE (s! * A% * funzione(i%).x + 399, A% * funzione(i%).y + 299)-(s! * A% * funzione(i% + 1).x + 399, A% * funzione(i% + 1).y + 299), rosso
  72.     NEXT i%
  73.     _PUTIMAGE (0, 599)-(799, 0), grafico2&, grafico1&, (0, 0)-(799, 599) 'mette grafico2& su grafico1&, invertendo le x, in modo che le y postive siano verso l'alto e non verso il basso.
  74.     _PUTIMAGE ((L% - 800) \ 2, (H% - 600) \ 2), grafico1&, schermo& 'mette grafico1& su schermo&, il quale, essendo visualizzato, permette di vedere il grafico definitivo.
  75.     _DEST schermo& 'le iscruzioni che seguono agiscono su schermo&, che e' visualizzato.
  76.     istruzioni$ = "Premere INVIO per un nuovo calcolo, ESC per terminare."
  77.     LOCATE 45, ((L% / 8 - LEN(istruzioni$)) \ 2): PRINT istruzioni$
  78.     SELECT CASE UBOUND(funzione)
  79.         CASE IS <= 40
  80.             FOR i% = 1 TO UBOUND(funzione)
  81.                 LOCATE 6 + (UBOUND(funzione) - (UBOUND(funzione) - i%)), 16
  82.                 PRINT STR$(i%) + ") x = " + STR$(funzione(i%).x) + "; y = " + STR$(funzione(i%).y)
  83.             NEXT i%
  84.         CASE ELSE
  85.             FOR i% = 1 TO 40
  86.                 LOCATE 6 + (UBOUND(funzione) - (UBOUND(funzione) - i%)), 16
  87.                 PRINT STR$(i%) + ") x = " + STR$(funzione(i%).x) + "; y = " + STR$(funzione(i%).y)
  88.             NEXT i%
  89.     END SELECT
  90.  

so, it is time to analyze bplus and SMcNeill codes.

Pete,
Quote
Oh, you want easy?

_WIDTH 80, 1001

Then you don't have to scroll the screen, just the window! :D

Pete

I don't have understood this use of _WIDTH.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: graphs with QB64 and how to scroll the screen.
« Reply #10 on: January 19, 2021, 12:03:50 pm »
That's because I was making a joke. Apparently I want a new QB64 keyword called: _WIDTH where the user could scroll the whole window instead of the screen!

The actual existing WIDTH command (no underscore) enlarges a window to the number of rows given in the second parameter, but you can only drag that window to the top of your screen, and therefore you can't see all the print outs, because you never get to see the entire window. You can see this by trying...

Code: QB64: [Select]
  1. WIDTH 80, 200
  2. FOR i% = 1 TO 200
  3.     PRINT i%


BTW, the default screen height in QB64 and QB is 25, but you can get nice results with WIDTH 80, 42 and a few other height parameters that won't mash the font too much. See WIDTH in the Wiki, for full details.

Joking aside though, if you want work with advanced screen scrolling techniques, have a look at the code I wrote in that  link I posted. If you have any questions about how it works, just post your questions. I'm not sure if this is a homework assignment, or if you are learning to code for your own benefit; but if you really want to be able to do a lot of things with your programs, eventually you will need to learn or discover some of these methods to advance your abilities. Anyone here at the forum would be happy to help you along the way. Keep in mind, BASIC is a programming language to help coders make their routines. It's a teaching language, as opposed to a library heavy language which might have more keywords to do things like scroll anything written to the screen. In other words, that code I posted could be made into a library, and called by a keyword made for the language. The only downside to these types of languages is lack of customization.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: graphs with QB64 and how to scroll the screen.
« Reply #11 on: January 19, 2021, 12:52:29 pm »
Hi Pete,
Quote
oking aside though, if you want work with advanced screen scrolling techniques, have a look at the code I wrote in that  link I posted.

thanks for reminding me, because I didn't noticed it!

Quote
https://www.tapatalk.com/groups/qbasic/i-m-working-on-a-scrollbar-rotine-t39417.html#p212427

very very interesting for my puposes.

Quote
Screen scrolling with mouse, mouse wheel, arrows?

In principle, with arrows: keyboard only for my taste.

Quote
I'm not sure if this is a homework assignment, or if you are learning to code for your own benefit;

the second one. Mixing business with pleasure, I want to became good enough to use QB64 for work. For example, the excel image I posted is a sheet of a file finalized to modeling a flood hydrogram. I have to manage a lot of parameters and sheets to do that with excel. Although I could also use specific programs as HEC-HMS, my purpose is to make a QB64 program that automatically calculates output.

Quote
Keep in mind, BASIC is a programming language to help coders make their routines. It's a teaching language, as opposed to a library heavy language which might have more keywords to do things like scroll anything written to the screen. In other words, that code I posted could be made into a library, and called by a keyword made for the language. The only downside to these types of languages is lack of customization.

At the University, I have practiced with Matlab, which (correct me if I'm wrong) is an example of those library heavy languages. I remeber that I liked Matlab very much, so my initial idea was to taking up Matlab again, but apart from the fact that it is not free, its size is actually exploded in the last 10 years: it was about 4 Gb (which was already not cheap), but now it is something like 15-20 Gb: really "too heavy language" for me! I prefer the opposite: do on my own, without libraries, with an handy language. At the end it's for fun. The approximately 700 Mb of QB64 is a decent size. I don't have to do anything for the NASA.
   
And last but not least, Basic was my very first passion in coding, with Commodore 64, when I was child, and QBasic soon after.

Quote
Anyone here at the forum would be happy to help you along the way.

I will definitely take advantage of it! Thank you.
« Last Edit: January 19, 2021, 01:02:05 pm by bartok »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: graphs with QB64 and how to scroll the screen.
« Reply #12 on: January 19, 2021, 03:24:30 pm »
That's because I was making a joke. Apparently I want a new QB64 keyword called: _WIDTH where the user could scroll the whole window instead of the screen!

You can do that with the console now, in windows.  WIDTH now takes 4 parameters: x, y, vx, vy
x/y is the size of your console buffer
vx, vy is the VIEWABLE size of the console.

So WIDTH 80, 1000, 80, 25 would let you print to an 80x1000 screen, while displaying 80x25 at a time, with scroll bars to scroll up and down to view the results.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: graphs with QB64 and how to scroll the screen.
« Reply #13 on: January 19, 2021, 11:13:31 pm »
Is that in v1.4 or the upcoming v1.5? I'm still using v1.3.

Either way, thanks for mentioning it. That "feature" would be fun to play around with. Flip screens are nearly as much fun as tab screens. I've always thought Windows should go to a tabbed desktop or a gesture left or right to view different desktop arrangements.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: graphs with QB64 and how to scroll the screen.
« Reply #14 on: January 19, 2021, 11:48:49 pm »
Is that in v1.4 or the upcoming v1.5? I'm still using v1.3.

Either way, thanks for mentioning it. That "feature" would be fun to play around with. Flip screens are nearly as much fun as tab screens. I've always thought Windows should go to a tabbed desktop or a gesture left or right to view different desktop arrangements.

Pete

Quick test is to just:

$CONSOLE:ONLY
_DEST _CONSOLE: _SOURCE _CONSOLE
COLOR 4
PRINT “Hello World”

If your version prints the above in red, you’ve got a version where all the enhancements were pushed into the console.  I’m certain 1.4 has it, but the changes have been in the language for quite some time.  Your 1.3 may have the changes, if it’s a development version, as I think it was sometime between 1.3 and 1.4 when I added it.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!