_TITLE "Graph"
SCREEN _NEWIMAGE(1080, 720, 12)
$RESIZE:ON
$CHECKING:OFF
' Origin in Cartesian coordinates. (Changes when mouse is clicked.)
OriginX = -100
OriginY = -100
' Point of interest in Cartesian coordinates. (Changes while mouse moves.)
x = _MOUSEX
y = _MOUSEY
IF x > 0 AND x < screenWidth AND y > 0 AND y < screenHeight THEN
GOSUB unconvert
OriginX = x
OriginY = y
ELSE
ThePointX = 100
ThePointY = 100
END IF
' Main loop.
DO: _LIMIT 60
DO WHILE _MOUSEINPUT
x = _MOUSEX
y = _MOUSEY
IF x > 0 AND x < screenWidth AND y > 0 AND y < screenHeight THEN
GOSUB unconvert
ThePointX = x
ThePointY = y
IF _MOUSEBUTTON(1) THEN
IF _MOUSEY < -190 AND _MOUSEX < -110 AND _MOUSEX > -120 AND _MOUSEY > -220 THEN
SYSTEM
END IF
x = _MOUSEX
y = _MOUSEY
GOSUB unconvert
OriginX = x
OriginY = y
END IF
END IF
IF _RESIZE THEN
SCREEN _NEWIMAGE(_RESIZEWIDTH, _RESIZEHEIGHT, 256)
screenWidth = _RESIZEWIDTH
screenHeight = _RESIZEHEIGHT
END IF
LOOP
GOSUB DrawEverything
LOOP
SYSTEM 0
$CHECKING:ON
DrawEverything:
CLS
' Make Cartesian grid.
FOR x = OriginX TO screenWidth STEP 10
LINE (x, 0)-(x, screenHeight), 8
NEXT
FOR x = OriginX TO 0 STEP -10
LINE (x, 0)-(x, screenHeight), 8
NEXT
FOR y = OriginY TO screenHeight STEP 10
LINE (0, -y + screenHeight / 2)-(screenWidth, -y + screenHeight / 2), 8
NEXT
FOR y = OriginY TO -(screenHeight / 2) STEP -10
LINE (0, -y + screenHeight / 2)-(screenWidth, -y + screenHeight / 2), 8
NEXT
x = OriginX
y = OriginY
GOSUB convert
LINE (0, y)-(screenWidth, y), 7
LINE (x, 0)-(x, screenHeight), 7
_PRINTSTRING (screenWidth - 8 * 6, y), "X-axis"
_PRINTSTRING (x, 0), "Y-axis"
_PRINTSTRING (x, y), "Origin"
_PRINTSTRING (0, 0), "Exit"
' Draw the circle on which the position vector lives.
Radius = SQR((ThePointX - OriginX) ^ 2 + (ThePointY - OriginY) ^ 2)
x = OriginX
y = OriginY
GOSUB convert
CIRCLE (x, y), Radius, 7
' Draw the vertical component.
x = OriginX
y = OriginY
GOSUB convert
x1 = x
y1 = y
x = ThePointX
y = OriginY
GOSUB convert
x2 = x
y2 = y
LINE (x1, y1)-(x2, y2), 9
LINE (x1, y1 + 1)-(x2, y2 + 1), 9
LINE (x1, y1 - 1)-(x2, y2 - 1), 9
' Draw the horizontal component.
x = ThePointX
y = OriginY
GOSUB convert
x1 = x
y1 = y
x = ThePointX
y = ThePointY
GOSUB convert
x2 = x
y2 = y
LINE (x1, y1)-(x2, y2), 4
LINE (x1 - 1, y1)-(x2 - 1, y2), 4
LINE (x1 + 1, y1)-(x2 + 1, y2), 4
' Draw position vector (aka the Hypotenuse).
x = OriginX
y = OriginY
GOSUB convert
x1 = x
y1 = y
x = ThePointX
y = ThePointY
GOSUB convert
x2 = x
y2 = y
LINE (x1, y1)-(x2, y2), 10
LINE (x1 + 1, y1)-(x2 + 1, y2), 10
LINE (x1, y1 + 1)-(x2, y2 + 1), 10
' Write text.
COLOR 7
_PRINTSTRING (screenWidth - 250, 0), "-------Origin-------"
_PRINTSTRING (screenWidth - 250, 15), "Cartesian/Polar/Qb64:"
_PRINTSTRING (screenWidth - 250, 30), "X=0 , Y=0"
_PRINTSTRING (screenWidth - 250, 45), "R=0 , Ang=undef"
_PRINTSTRING (screenWidth - 250, 60), "x="
_PRINTSTRING (screenWidth - 235, 60), STR$(OriginX + screenWidth / 2) + ", " + "y=" + STR$(-OriginY + screenHeight / 2)
_PRINTSTRING (screenWidth - 250, 80), "-------Cursor-------"
_PRINTSTRING (screenWidth - 250, 95), "Cartesian/Polar/Qb64:"
_PRINTSTRING (screenWidth - 250, 110), "X=" + STR$(ThePointX - OriginX) + ", " + "Y=" + STR$(ThePointY - OriginY)
' Deal with radius calculation.
Radius = SQR((ThePointX - OriginX) ^ 2 + (ThePointY - OriginY) ^ 2)
IF Radius < .0001 THEN Radius = .0001
_PRINTSTRING (screenWidth - 250, 130), "R=" + STR$(INT(Radius)) + ", " + "Ang=" + STR$(TheAngle)
' Deal with the anlge calculation.
xdiff = ThePointX - OriginX
ydiff = ThePointY - OriginY
IF xdiff > 0 AND ydiff > 0 THEN ' First quadrant
TheAngle = INT((180 / 3.14159) * ATN(ydiff / xdiff))
END IF
IF xdiff < 0 AND ydiff > 0 THEN ' Second quadrant
TheAngle = 180 + INT((180 / 3.14159) * ATN(ydiff / xdiff))
END IF
IF xdiff < 0 AND ydiff < 0 THEN ' Third quadrant
TheAngle = 180 + INT((180 / 3.14159) * ATN(ydiff / xdiff))
END IF
IF xdiff > 0 AND ydiff < 0 THEN ' Fourth quadrant
TheAngle = 360 + INT((180 / 3.14159) * ATN(ydiff / xdiff))
END IF
IF SQR(ydiff ^ 2) < .0001 THEN ydiff = .0001
IF SQR(xdiff ^ 2) < .0001 THEN xdiff = .0001
_PRINTSTRING (screenWidth - 250, 145), "x=" + STR$(ThePointX + 320) + ", " + "y=" + STR$(-ThePointY + 240)
_PRINTSTRING (screenWidth - 250, 200), "--------Trig--------"
_PRINTSTRING (screenWidth - 250, 215), "sin(Ang)=" + "Opp" + "/" + "Hyp"
_PRINTSTRING (screenWidth - 250, 230), " =" + STR$(ydiff / Radius)
_PRINTSTRING (screenWidth - 250, 245), "cos(Ang)=" + "Adj" + "/" + "Hyp"
_PRINTSTRING (screenWidth - 250, 260), " =" + STR$(xdiff / Radius)
_PRINTSTRING (screenWidth - 250, 275), "tan(Ang)=" + "Opp" + "/" + "Adj"
_PRINTSTRING (screenWidth - 250, 290), " =" + STR$(ydiff / xdiff)
_DISPLAY
RETURN
convert:
' Converts Cartesian coordinates to QB64 coordinates.
x0 = x: y0 = y
x = x0 + screenWidth / 2
y = -y0 + screenHeight / 2
RETURN
unconvert:
' Converts QB64 coordinates to Cartesian coordinates.
x0 = x: y0 = y
x = x0 - screenWidth / 2
y = -y0 + screenHeight / 2
RETURN