'$DYNAMIC
'$INCLUDE: 'USELESS.BI'
DEFINT A-Z

REM $STATIC
SUB USdrawBox (X1 AS INTEGER, Y1 AS INTEGER, X2 AS INTEGER, Y2 AS INTEGER, element AS INTEGER, colour AS INTEGER, destSegment AS INTEGER, destOffset AS INTEGER)
'Draws a normal empty box
IF X1 > X2 THEN SWAP X1, X2
IF Y1 > Y2 THEN SWAP Y1, Y2
FOR a = X1 TO X2
IF a < 1 OR a > USinternalScreenX THEN GOTO skipdraw12
IF Y1 > USinternalScreenY OR Y1 < 1 THEN GOTO skipdraw11
USplaceElement a, Y1, element, colour, destSegment, destOffset
skipdraw11:
IF Y2 > USinternalScreenY OR Y2 < 1 THEN GOTO skipdraw12
USplaceElement a, Y2, element, colour, destSegment, destOffset
skipdraw12:
NEXT a
FOR a = Y1 TO Y2
IF a < 1 OR a > USinternalScreenY THEN GOTO skipdraw14
IF X1 < 1 OR X1 > USinternalScreenX THEN GOTO skipdraw13
USplaceElement X1, a, element, colour, destSegment, destOffset
skipdraw13:
IF X2 < 1 OR X2 > USinternalScreenX THEN GOTO skipdraw14
USplaceElement X2, a, element, colour, destSegment, destOffset
skipdraw14:
NEXT a
END SUB

SUB USdrawBoxF (X1 AS INTEGER, Y1 AS INTEGER, X2 AS INTEGER, Y2 AS INTEGER, borderElement AS INTEGER, fillElement AS INTEGER, borderColour AS INTEGER, fillColour AS INTEGER, destSegment AS INTEGER, destOffset AS INTEGER)
'Draws a kewlish filled box to your specifications
IF X1 > X2 THEN SWAP X1, X2
IF Y1 > Y2 THEN SWAP Y1, Y2
FOR a = X1 TO X2
IF a < 1 OR a > USinternalScreenX THEN GOTO skipdraw22
IF Y1 > USinternalScreenY OR Y1 < 1 THEN GOTO skipdraw21
USplaceElement a, Y1, borderElement, borderColour, destSegment, destOffset
skipdraw21:
IF Y2 > USinternalScreenY OR Y2 < 1 THEN GOTO skipdraw22
USplaceElement a, Y2, borderElement, borderColour, destSegment, destOffset
skipdraw22:
NEXT a
FOR a = Y1 TO Y2
IF a < 1 OR a > USinternalScreenY THEN GOTO skipdraw24
IF X1 < 1 OR X1 > USinternalScreenX THEN GOTO skipdraw23
USplaceElement X1, a, borderElement, borderColour, destSegment, destOffset
skipdraw23:
IF X2 < 1 OR X2 > USinternalScreenX THEN GOTO skipdraw24
USplaceElement X2, a, borderElement, borderColour, destSegment, destOffset
skipdraw24:
NEXT a
IF (X2 - X1) < 2 OR (Y2 - Y1) < 2 THEN EXIT SUB
FOR a = (X1 + 1) TO (X2 - 1)
FOR b = (Y1 + 1) TO (Y2 - 1)
IF a < 1 OR a > USinternalScreenX THEN GOTO skipdraw25
IF b < 1 OR b > USinternalScreenY THEN GOTO skipdraw25
USplaceElement a, b, fillElement, fillColour, destSegment, destOffset
skipdraw25:
NEXT b, a
END SUB

SUB USdrawEllipse (x AS INTEGER, y AS INTEGER, xr AS INTEGER, yr AS INTEGER, element AS INTEGER, colour AS INTEGER, destSegment AS INTEGER, destOffset AS INTEGER)
'Draws an ellipse
FOR xl = xr TO 0 STEP -1
  yl = SQR(xr * xr - xl * xl) * yr / xr%: Y2 = yl%
  DO
    USplaceElement (x + xl), (y + yl), element, colour, destSegment, destOffset
    USplaceElement (x - xl), (y + yl), element, colour, destSegment, destOffset
    USplaceElement (x + xl), (y - yl), element, colour, destSegment, destOffset
    USplaceElement (x - xl), (y - yl), element, colour, destSegment, destOffset
    yl = yl - 1
  LOOP WHILE yl > Y1%
  Y1 = Y2%
NEXT
END SUB

SUB USdrawLine (X1 AS INTEGER, Y1 AS INTEGER, X2 AS INTEGER, Y2 AS INTEGER, element AS INTEGER, colour AS INTEGER, destSegment AS INTEGER, destOffset AS INTEGER)
'Draws a straight line using the Bresenham's line theory
USplaceElement X1, Y1, element, colour, destSegment, destOffset
USplaceElement X2, Y2, element, colour, destSegment, destOffset
wid = (X2 - X1)
hei = (Y2 - Y1)
IF wid < 0 THEN
  wid = ABS(wid)
  deltax1 = -1
ELSE
  deltax1 = 1
END IF
IF hei < 0 THEN
  hei = ABS(hei)
  deltay1 = -1
ELSE
  deltay1 = 1
END IF
IF wid < hei THEN
  SWAP hei, wid
  deltax2 = 0
  deltay2 = deltay1
ELSE
  deltax2 = deltax1
  deltay2 = 0
END IF
nondiag = hei * 2
d = nondiag - wid
diaginc = d - wid
FOR bung = 0 TO wid
IF d < 0 THEN
  x = x + deltax2
  y = y + deltay2
  d = d + nondiag
ELSE
  x = x + deltax1
  y = y + deltay1
  d = d + diaginc
END IF
USplaceElement INT(X1 + x), INT(Y1 + y), element, colour, destSegment, destOffset
NEXT bung
END SUB

