_TITLE "Celtic Square Knot" 'b+ 2020-09-15 I am obsessed ;-))
'check for overlap , dang ok fix OK good
drawFrame
_WIDTH / 2 - 100, _HEIGHT / 2 - 100, 300, 200, 10, &H22FFFFFFside = 390
CelticSqrKnot
_WIDTH / 2 - side
/ 2, _HEIGHT / 2 - side
/ 2, side
, &HFF00AA55
'now for the knot
SUB CelticSqrKnot
(x
, y
, side
, backK
AS _UNSIGNED LONG) 'if side and thick are multiples of 7 this could work side > 6*th ' calc 7*xSpace + 6*th = side so xspace = (side - 6*th)/7
'if we make it simple x = th then 7*th + 6*th = side so side has to be multiple of 13
' 3 frames 4 boxes each
cF1 = &HFF0000AA: cF2 = &HFFFFFF00: cF3 = &HFFAA0000: white = &HFFFFFFFF
LINE (x
, y
)-STEP(13 * th
, 13 * th
), backK
, BF
'back ground can be transparent
drawFrame x + 5 * th, y + th, 3 * th, 11 * th, th, cF1 'vertical
frameParts x + 5 * th, y + th, 3 * th, 11 * th, th, f1()
drawFrame x + 3 * th, y + 3 * th, 7 * th, 7 * th, th, cF2 'square
frameParts x + 3 * th, y + 3 * th, 7 * th, 7 * th, th, f2()
drawFrame x + th, y + 5 * th, 11 * th, 3 * th, th, cF3 ' horizontal
frameParts x + th, y + 5 * th, 11 * th, 3 * th, th, f3()
count = 0
'frame 1, 1234 intersect frame 2 square 5678
IF collision
(f1
(fb1
).x
, f1
(fb1
).y
, f1
(fb1
).w
, f1
(fb1
).h
, f2
(fb2
).x
, f2
(fb2
).y
, f2
(fb2
).w
, f2
(fb2
).h
) THEN
' get the intersecting box
collisionIntersect2Boxes f1(fb1).x, f1(fb1).y, f1(fb1).w, f1(fb1).h, f2(fb2).x, f2(fb2).y, f2(fb2).w, f2(fb2).h, bix, biy, biw, bih
count = count + 1
'IF count MOD 4 = 3 THEN LINE (bix, biy)-STEP(biw, bih), cF2, BF
'IF count MOD 4 = 0 THEN LINE (bix, biy)-STEP(biw, bih), cF1, BF
count = 0
'frame 1, 1234 intersect frame 3 intersect 9, 10, 11, 12
IF collision
(f1
(fb1
).x
, f1
(fb1
).y
, f1
(fb1
).w
, f1
(fb1
).h
, f3
(fb2
).x
, f3
(fb2
).y
, f3
(fb2
).w
, f3
(fb2
).h
) THEN
' get the intersecting box
collisionIntersect2Boxes f1(fb1).x, f1(fb1).y, f1(fb1).w, f1(fb1).h, f3(fb2).x, f3(fb2).y, f3(fb2).w, f3(fb2).h, bix, biy, biw, bih
count = count + 1
'IF count MOD 4 = 3 THEN LINE (bix, biy)-STEP(biw, bih), cF1, BF
'IF count MOD 4 = 0 THEN LINE (bix, biy)-STEP(biw, bih), cF3, BF
count = 0
'frame 2, 5678 intersect frame 3 intersect 9, 10, 11, 12
IF collision
(f2
(fb1
).x
, f2
(fb1
).y
, f2
(fb1
).w
, f2
(fb1
).h
, f3
(fb2
).x
, f3
(fb2
).y
, f3
(fb2
).w
, f3
(fb2
).h
) THEN
' get the intersecting box
collisionIntersect2Boxes f2(fb1).x, f2(fb1).y, f2(fb1).w, f2(fb1).h, f3(fb2).x, f3(fb2).y, f3(fb2).w, f3(fb2).h, bix, biy, biw, bih
count = count + 1
'IF count MOD 4 = 3 THEN LINE (bix, biy)-STEP(biw, bih), cF3, BF
'IF count MOD 4 = 0 THEN LINE (bix, biy)-STEP(biw, bih), cF2, BF
LINE (x
, y
)-STEP(w
, th
), k
, BF
'two horizontal LINE (x
, y
+ th
+ 1)-STEP(th
, h
- 2 * th
- 2), k
, BF
'try not to overlap boxes in case k is transparent LINE (x
+ w
- th
, y
+ th
+ 1)-STEP(th
, h
- 2 * th
- 2), k
, BF
SUB frameParts
(x
, y
, w
, h
, th
, bxs
() AS Box
)
'for each line in drawFrame
'LINE (x, y)-STEP(w, th), k, BF 'two horizontal
bxs(1).x = x
bxs(1).y = y
bxs(1).w = w
bxs(1).h = th
'LINE (x, y + h - th)-STEP(w, th), k, BF
bxs(2).x = x
bxs(2).y = y + h - th
bxs(2).w = w
bxs(2).h = th
'LINE (x, y + th + 1)-STEP(th, h - 2 * th - 2), k, BF 'try not to overlap boxes in case k is transparent
bxs(3).x = x
bxs(3).y = y + th + 1
bxs(3).w = th
bxs(3).h = h - 2 * th - 2
'LINE (x + w - th, y + th + 1)-STEP(th, h - 2 * th - 2), k, BF
bxs(4).x = x + w - th
bxs(4).y = y + th + 1
bxs(4).w = th
bxs(4).h = h - 2 * th - 2
FUNCTION collision%
(b1x
, b1y
, b1w
, b1h
, b2x
, b2y
, b2w
, b2h
) ' x, y represent the box left most x and top most y
' w, h represent the box width and height which is the usual way sprites / tiles / images are described
' such that boxbottom = by + bh
' and boxright = bx + bw
IF (b1y
+ b1h
< b2y
) OR (b1y
> b2y
+ b2h
) OR (b1x
> b2x
+ b2w
) OR (b1x
+ b1w
< b2x
) THEN collision% = 0
collision% = 1
SUB collisionIntersect2Boxes
(b1x
, b1y
, b1w
, b1h
, b2x
, b2y
, b2w
, b2h
, bix
, biy
, biw
, bih
) IF b2x
>= b1x
AND b2x
<= b1x
+ b1w
AND b2y
>= b1y
AND b2y
<= b1y
+ b1h
THEN 'top left corner in 2nd box bix = b2x: biy = b2y
IF b2x
+ b2w
<= b1x
+ b1w
THEN biw = b2w
biw = b1x + b1w - b2x
IF b2y
+ b2h
<= b1y
+ b1h
THEN bih = b2h
bih = b1y + b1h - b2y
ELSEIF b2x
>= b1x
AND b2x
<= b1x
+ b1w
AND b2y
+ b2h
>= b1y
AND b2y
+ b2h
<= b1y
+ b1h
THEN 'bottom left corner of 2nd box in first bix = b2x
IF b2x
+ b2w
<= b1x
+ b1w
THEN biw = b2w
biw = b1x + b1w - b2x
biy = b1y
bih = b2y + b2h - b1y
biy = b2y
bih = b2h
ELSEIF b2x
+ b2w
>= b1x
AND b2x
+ b2w
<= b1x
+ b1w
AND b2y
>= b1y
AND b2y
<= b1y
+ b1h
THEN 'right top corner 2nd box in first bix = b2x
biw = b2w
bix = b1x
biw = b2x + b2w - b1x
biy = b2y
IF b2y
+ b2h
<= b1y
+ b1h
THEN bih = b2h
bih = b1y + b1h - b2y
ELSEIF b2x
+ b2w
>= b1x
AND b2x
+ b2w
<= b1x
+ b1w
AND b2y
+ b2h
>= b1y
AND b2y
+ b2h
<= b1y
+ b1h
THEN 'left bottom corners in first box bix = b2x
biw = b2w
bix = b1x
biw = b2x + b2w - b1x
biy = b2y
bih = b2h
biy = b1y
bih = b2y + b2h - b1y
ELSEIF collision%
(b1x
, b1y
, b1w
, b1h
, b2x
, b2y
, b2w
, b2h
) THEN bix = max(b1x, b2x): biy = max(b1y, b2y)
biw = min(b1x + b1w, b2x + b2w) - bix: bih = min(b1y + b1h, b2y + b2h) - biy
bix = -1: biy = -1: biw = 0: bih = 0