_TITLE "Circle Packing 4-B nonCircles" 'B+ started 2019-04-09 update with distLessCheck function LINE (0, 0)-(xmax
- 1, ymax
- 1), _RGB32(0, 0, 0), BF
cText xmax
/ 2, ymax
/ 2, 500, _RGB32(255, 255, 255), "QB64"
count = count + 1
newCircle 20
drawCircles
IF circles
(i
).growing
THEN 'check new r testr = circles(i).r + 1
IF circles
(i
).x
- testr
< 0 OR circles
(i
).x
+ testr
> xmax
- 1 OR circles
(i
).y
- testr
< 0 OR circles
(i
).y
+ testr
> ymax
- 2 THEN circles(i).growing = 0
'check if run into another circle
IF distLessCheck
(circles
(j
).x
, circles
(i
).x
, circles
(j
).y
, circles
(i
).y
, testr
+ circles
(j
).r
) THEN 'IF SQR((circles(j).x - circles(i).x) ^ 2 + (circles(j).y - circles(i).y) ^ 2) <= testr + circles(j).r THEN
circles(i).growing = 0: circles(j).growing = 0
IF circles
(i
).growing
THEN circles
(i
).r
= testr
star circles(i).x, circles(i).y, .3 * circles(i).r, circles(i).r, circles(i).n, circles(i).a, circles(i).c
attempts = 0
retry:
OK = -1
IF distLessCheck
(testX
, circles
(j
).x
, testY
, circles
(j
).y
, circles
(j
).r
+ 3) THEN OK
= 0:
EXIT FOR 'IF SQR((testX - circles(j).x) ^ 2 + (testY - circles(j).y) ^ 2) < circles(j).r + 3 THEN OK = 0: EXIT FOR
circles(new).x = testX
circles(new).y = testY
circles(new).r = 3
circles
(new
).n
= 5 + INT(RND * 5) circles(new).growing = -1
attempts = attempts + 1
'screen snapshot
mult = textHeight / 16
xlen
= LEN(txt$
) * 8 * mult
_PUTIMAGE (x
- .5 * xlen
, y
- .5 * textHeight
)-STEP(xlen
, textHeight
), I&
, cur&
'this just draws a bunch of triangles according to x, y points in arr()
ox = arr(0): oy = arr(1) 'the first 2 items in arr() need to be center
ftri ox, oy, arr(i), arr(i + 1), arr(i + 2), arr(i + 3), K
' x, y are same as for circle,
' rInner is center circle radius
' rOuter is the outer most point of star
' nPoints is the number of points,
' angleOffset = angle offset in radians
' this is to allow us to spin the star
DIM ar
(INT(nPoints
) * 4 + 3) 'add two for origin pAngle
= _PI(2) / nPoints: radAngleOffset
= angleOffset
- _PI(1 / 2) ar(0) = x: ar(1) = y
ar
(2) = x
+ rOuter
* COS(radAngleOffset
) ar
(3) = y
+ rOuter
* SIN(radAngleOffset
) idx = 4
ar
(idx
) = x
+ rInner
* COS(i
* pAngle
+ radAngleOffset
+ .5 * pAngle
) idx = idx + 1
ar
(idx
) = y
+ rInner
* SIN(i
* pAngle
+ radAngleOffset
+ .5 * pAngle
) idx = idx + 1
ar
(idx
) = x
+ rOuter
* COS((i
+ 1) * pAngle
+ radAngleOffset
) idx = idx + 1
ar
(idx
) = y
+ rOuter
* SIN((i
+ 1) * pAngle
+ radAngleOffset
) idx = idx + 1
fIrrPoly ar(), K
FUNCTION distLessCheck
(x1
, x2
, y1
, y2
, checkThis
) dx = x1 - x2: dy = y1 - y2
IF dx
* dx
+ dy
* dy
< checkThis
* checkThis
THEN distLessCheck
= -1 ELSE distLessCheck
= 0