Author Topic: Cartesian Coordinate System Setup for QB64  (Read 4245 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Cartesian Coordinate System Setup for QB64
« on: October 20, 2019, 03:28:29 pm »
All the tools are already there, just need a decent demo, try something along lines of STxAxTIC's recent work of art:
Code: QB64: [Select]
  1. _TITLE "Test Using Window for Cartesian Coordinate System x = -100 to 100, y = -100 to 100"
  2. 'It seems all graphics commands convert x, y to window system BUT...
  3. 'Mouse and _PrintString do not convert! BUT...
  4. 'Somebody setup a PMAP system that will do these conversions both to graphics coordinate system and back
  5. ' convert mouse qbx, qby to graphics x, y use
  6. ' PMAP(qbx, 2), PMAP(qby, 3)
  7. ' And to print something at the graphics location use
  8. ' PMAP(graphicsX, 0), PMAP(graphicsY, 1) to _PRINTSTRING to a graphics location
  9.  
  10. CONST xmax = 700, ymax = 700, red = &HFFFF0000, grn = &HFF009900, blu = &HFF0000AA
  11. CONST tlx = -100, tly = 100, brx = 100, bry = -100 ' Cartesian Coordinate System corners for WINDOW command
  12. SCREEN _NEWIMAGE(xmax, ymax, 32)
  13. _SCREENMOVE 300, 40
  14.  
  15. DIM mx, my, a, r, s$
  16.  
  17. WINDOW (tlx, tly)-(brx, bry) ' converts QB screen to Cartesian Coodianate System
  18. WHILE _KEYDOWN(27) = 0
  19.     CLS
  20.     mx = INT(PMAP(_MOUSEX, 2)): my = INT(PMAP(_MOUSEY, 3)) 'covert mouse to Cart Cood using PMAP
  21.     a = atan360(my, mx) 'mouse angle to origin
  22.     r = INT((mx * mx + my * my) ^ .5)
  23.     LINE (0, 0)-(mx, my), grn 'hypotenuse = radius
  24.     LINE (0, 0)-(mx, 0), blu ' r*cos
  25.     LINE (mx, 0)-(mx, my), red ' r*sin
  26.     arc 0, 0, 10, 0, _R2D(a), &HFFFFFF00 'yellow arc
  27.     s$ = "Mouse (" + _TRIM$(STR$(mx)) + ", " + _TRIM$(STR$(my)) + "), Radius: "
  28.     s$ = s$ + _TRIM$(STR$(r)) + ", Degrees: " + _TRIM$(STR$(_R2D(a) \ 1))
  29.     _PRINTSTRING (PMAP(0, 0), PMAP(-1, 1)), s$
  30.     _PRINTSTRING (PMAP(-95, 0), PMAP(95, 1)), "COS = x/r = " + _TRIM$(STR$(mx / r))
  31.     _PRINTSTRING (PMAP(-95, 0), PMAP(89, 1)), "SIN = y/r = " + _TRIM$(STR$(my / r))
  32.      IF mx <> 0 THEN _PRINTSTRING (PMAP(-95, 0), PMAP(83, 1)), "TAN = y/x = " + _TRIM$(STR$(my / mx))  'EDIT
  33.     _DISPLAY
  34.     _LIMIT 60
  35.  
  36. ' A regular graphic draw still works correctly in a Cartesian Window System! even the angle goes Counter-Clockwise.
  37. 'note: degrees start due East = 0 and go clockwise: South = 90, West = 180, North = 270
  38. SUB arc (x, y, r, degStart, degStop, c~&)
  39.  
  40.     DIM rs, re, a, ax, ay, lastx, lasty
  41.     'x, y origin, r = radius, c = color
  42.  
  43.     'degStart is first angle clockwise from due East = 0 degrees
  44.     ' arc will start drawing there and clockwise until degStop angle reached, unless in Cartesain Window
  45.  
  46.     IF degStop < degStart THEN
  47.         arc x, y, r, degStart, 360, c~&
  48.         arc x, y, r, 0, degStop, c~&
  49.     ELSE
  50.         rs = _D2R(degStart): re = _D2R(degStop)
  51.         FOR a = rs TO re STEP 1 / (7 * r)
  52.             ax = x + r * COS(a)
  53.             ay = y + r * SIN(a)
  54.             IF a <> rs THEN LINE (lastx, lasty)-(ax, ay), c~&
  55.             lastx = ax: lasty = ay
  56.         NEXT
  57.     END IF
  58.  
  59. 'Do need to match degrees going around clockwise up to full circle to avoid negative angles
  60. FUNCTION atan360 (y, x)
  61.     DIM test
  62.     test = _ATAN2(y, x)
  63.     IF test < 0 THEN atan360 = _PI(2) + test ELSE atan360 = test
  64.  
  65.  

EDIT: I had the ratio for tangent wrong, it is rise over run just like slope of a line, sin/cos or y/x here.
« Last Edit: October 21, 2019, 02:03:23 am by bplus »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Cartesian Coordinate System Setup for QB64
« Reply #1 on: October 20, 2019, 05:13:46 pm »
Excellent!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Cartesian Coordinate System Setup for QB64
« Reply #2 on: October 20, 2019, 06:49:32 pm »
My only beef is this comment:

Code: QB64: [Select]
  1. 'note: degrees start due East = 0 and go clockwise: South = 90, West = 180, North = 270
  2.  

I believe north and south are flipped good sir. Angles should go counterclockwise, which I think is correct in the actual code.
« Last Edit: October 20, 2019, 07:37:51 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cartesian Coordinate System Setup for QB64
« Reply #3 on: October 20, 2019, 08:14:43 pm »
My only beef is this comment:

Code: QB64: [Select]
  1. 'note: degrees start due East = 0 and go clockwise: South = 90, West = 180, North = 270
  2.  

I believe north and south are flipped good sir. Angles should go counterclockwise, which I think is correct in the actual code.

Hi STxAxTIC,

That comment was leftover from the original arc code, explaining how normal QB64 graphics go. The amazing thing is that it works with the Cartesian system such that the angles do increase in Counter-Clockwise direction without any change of the drawing code! I thought the comment above it, that applies to the demo would over-ride it.

Quote
' A regular graphic draw still works correctly in a Cartesian Window System! even the angle goes Counter-Clockwise.
« Last Edit: October 20, 2019, 08:16:47 pm by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Cartesian Coordinate System Setup for QB64
« Reply #4 on: October 20, 2019, 08:21:22 pm »
Ah, that clears it up. You da man.
You're not done when it works, you're done when it's right.

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Cartesian Coordinate System Setup for QB64
« Reply #5 on: October 21, 2019, 11:19:34 am »
Very cool. Excellent work!
QB64 is the best!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Cartesian Coordinate System Setup for QB64
« Reply #6 on: October 22, 2019, 03:45:45 pm »
Cool!
It is clear trigonometry in this way :-) You can see directly it!
Programming isn't difficult, only it's  consuming time and coffee