Author Topic: Subroutines to draw axes automatically scaled.  (Read 3081 times)

0 Members and 1 Guest are viewing this topic.

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Subroutines to draw axes automatically scaled.
« on: October 27, 2021, 08:22:18 am »
As extract from the program that I posted, I post here a sequence of subroutine that I think it can be usefull in order to draw axes.

N1!, N2!: they are examples of maximum values in abscissa and ordinate.
N3!, N4!: example of pair of values to put in the graph.

The program has these features:
- the origin of the axes is 40 pixels in X and Y direction from the lower left corner, these distances are named dx! and dy!.
- the axes are drawn in this way, speaking for example about Y axe: it starts at dy! distance from the loer border, it goes at dy! distance beyond N2! and it is spaced by dy! from the upper border. to do it the process is iterative.

Generally, the subroutine ScalaOrigine is CALLed by DisegnaAssi, but in more rare cases ScalaOrigine may be CALLed alone and
scala~% = 0 AND origine~% = 1.
It may happen when we have to work on images already created and scaled and it is necessary only to restore the origin of the axes.

Code: QB64: [Select]
  1.  
  2. CONST R& = _RGB32(255, 0, 0)
  3. CONST G& = _RGB32(0, 255, 0)
  4. CONST B& = _RGB32(0, 0, 255)
  5. CONST W& = _RGB32(255, 255, 255)
  6. CONST YE& = _RGB32(255, 255, 0)
  7.  
  8. DIM schermo&
  9. DIM N1!, N2!, N3!, N4!
  10.  
  11. schermo& = _NEWIMAGE(800, 600, 32)
  12. SCREEN schermo&
  13.  
  14. N1! = 113.4
  15. N2! = 34.44
  16. N3! = 43
  17. N4! = 22.2
  18.  
  19. CALL DisegnaAssi("X", "Y", N1!, N2!, schermo&, YE&, W&, B&, G&)
  20. CALL DisegnaValore(N1!, N2!, R&, YE&, G&)
  21. CALL DisegnaValore(N3!, N4!, R&, YE&, G&)
  22.  
  23. '--------------------------------------------------------------------------------------------------------------------------------------------------
  24. SUB ScalaOrigine (scala~%, origine~%, X!, Y!, immagine&)
  25.  
  26.     SHARED dx%, dy%
  27.     SHARED dx!, dy!
  28.  
  29.     DIM fattoreX!, fattoreY!
  30.  
  31.     fattoreX! = 1
  32.     fattoreY! = 1
  33.     IF scala~% = 0 AND origine~% = 1 THEN GOSUB origine
  34.     IF scala~% = 1 AND origine~% = 1 THEN
  35.         DO
  36.             GOSUB scala
  37.             GOSUB origine
  38.             IF Y! * fattoreY! >= dy! + Y! + dy! + dy! THEN
  39.                 IF X! * fattoreX! >= dx! + X! + dx! + dx! THEN
  40.                     EXIT DO
  41.                 ELSE
  42.                     fattoreX! = fattoreX! + 0.01
  43.                 END IF
  44.             ELSE
  45.                 fattoreY! = fattoreY! + 0.01
  46.             END IF
  47.         LOOP
  48.     END IF
  49.     EXIT SUB
  50.     '--------------------------------------------------------------------------------------------------------------------------------------------------
  51.     scala:
  52.  
  53.     WINDOW
  54.     WINDOW (0, 0)-(X! * fattoreX!, Y! * fattoreY!)
  55.     RETURN
  56.     '--------------------------------------------------------------------------------------------------------------------------------------------------
  57.     origine:
  58.  
  59.     dx% = 39
  60.     dx! = PMAP(dx%, 2)
  61.     dy% = _HEIGHT(immagine&) - 1 - dx%
  62.     dy! = PMAP(dy%, 3)
  63.     RETURN
  64. '--------------------------------------------------------------------------------------------------------------------------------------------------
  65. '--------------------------------------------------------------------------------------------------------------------------------------------------
  66. SUB DisegnaAssi (X$, Y$, X!, Y!, immagine&, ColoreCartiglio&, ColoreAssi&, ColoreTacca&, ColoreNumeri&)
  67.  
  68.     SHARED dx%, dy%
  69.     SHARED dx!, dy!
  70.  
  71.     DIM x%, y%
  72.     DIM taccaX!, taccaY!
  73.  
  74.     _DEST immagine&: CLS
  75.     SELECT CASE X!
  76.         CASE IS >= 12
  77.             taccaX! = X! \ 6
  78.         CASE IS <= 1
  79.             taccaX! = 0.25
  80.         CASE ELSE
  81.             taccaX! = 0.5
  82.     END SELECT
  83.     SELECT CASE Y!
  84.         CASE IS >= 12
  85.             taccaY! = Y! \ 6
  86.         CASE IS <= 1
  87.             taccaY! = 0.25
  88.         CASE ELSE
  89.             taccaY! = 0.5
  90.     END SELECT
  91.     WINDOW
  92.     LINE (0, 0)-(_WIDTH(immagine&) - 1, _HEIGHT(immagine&) - 1), ColoreCartiglio&, B
  93.     CALL ScalaOrigine(1, 1, X!, Y!, immagine&)
  94.     LINE (dx!, dy! + Y! + dy!)-(dx!, dy!), ColoreAssi&: LINE -(dx! + X! + dx!, dy!), ColoreAssi&
  95.     PSET (dx!, dy! + Y! + dy!), ColoreAssi&: DRAW "F20": PSET (dx!, dy! + Y! + dy!), ColoreAssi&: DRAW "G20"
  96.     PSET (dx! + X! + dx!, dy!), ColoreAssi&: DRAW "G20": PSET (dx! + X! + dx!, dy!), ColoreAssi&: DRAW "H20"
  97.     COLOR ColoreNumeri&
  98.     _PRINTSTRING (PMAP(dx! + X! + dx!, 0), dy%), X$
  99.     _PRINTSTRING (dx%, PMAP(dy! + Y! + dy!, 1) - dx% \ 2), Y$
  100.     _PRINTSTRING (dx% \ 4, dy% - 7), "0"
  101.     i% = 1
  102.     WHILE i% * taccaX! <= X!
  103.         LINE (dx! + i% * taccaX!, PMAP(dy% + 5, 3))-(dx! + i% * taccaX!, PMAP(dy% - 5, 3)), ColoreTacca&
  104.         x% = PMAP(dx! + i% * taccaX!, 0)
  105.         _PRINTSTRING (x% - 3 * LEN(_TRIM$(STR$(i% * taccaX!))), _HEIGHT(immagine&) - 1 - dx% + 5), _TRIM$(STR$(i% * taccaX!))
  106.         i% = i% + 1
  107.     WEND
  108.     i% = 1
  109.     WHILE i% * taccaY! <= Y!
  110.         LINE (PMAP(dx% - 5, 2), dy! + i% * taccaY!)-(PMAP(dx% + 5, 2), dy! + i% * taccaY!), ColoreTacca&
  111.         y% = PMAP(dy! + i% * taccaY!, 1)
  112.         _PRINTSTRING (dx% \ 4, y% - 7), _TRIM$(STR$(i% * taccaY!))
  113.         i% = i% + 1
  114.     WEND
  115. '--------------------------------------------------------------------------------------------------------------------------------------------------
  116. '--------------------------------------------------------------------------------------------------------------------------------------------------
  117. SUB DisegnaValore (X!, Y!, ColoreLinea&, ColorePunto&, ColoreTesto&)
  118.  
  119.     SHARED dx%, dy%
  120.     SHARED dx!, dy!
  121.  
  122.     DIM x%, y%
  123.  
  124.     CIRCLE (dx! + X!, dy! + Y!), PMAP(3, 2), ColorePunto&
  125.     PAINT (dx! + X! + PMAP(0.5, 2), dy! + Y! + PMAP(0.5, 2)), ColorePunto&
  126.     LINE (dx!, dy! + Y!)-(dx! + X!, dy! + Y!), ColoreLinea&, , 65520
  127.     LINE -(dx! + X!, dy!), ColoreLinea&, , 65520
  128.     x% = PMAP(dx! + X!, 0)
  129.     y% = PMAP(dy! + Y!, 1)
  130.     COLOR ColoreTesto&
  131.     _PRINTSTRING (x% + 4, y% - 16), "(" + _TRIM$(STR$(Arrotonda!(X!))) + ";" + _TRIM$(STR$(Arrotonda!(Y!))) + ")"
  132. '--------------------------------------------------------------------------------------------------------------------------------------------------
  133. '--------------------------------------------------------------------------------------------------------------------------------------------------
  134. FUNCTION Arrotonda! (num!)
  135.  
  136.     Arrotonda! = _ROUND(num! * 100) / 100
  137.