08-30-2025, 11:52 AM
(This post was last modified: 08-30-2025, 11:53 AM by Albert Richheimer.)
You mean this one, from Stuart Mclachlan, Sept. 28, 2022?
You will find it in Gary Beene's gbthreads.
Code:
#COMPILE EXE
#DIM ALL
MACRO RadToDeg(r) = (r * 57.2957795130823209##)
MACRO Pi = 3.14159265358979324##
MACRO TwoPi = 6.283185307179586##
TYPE CartPt
s AS EXT 'sequence number
x AS EXT ' x coord
y AS EXT ' y coord
a AS EXT 'angle in degrees about centre
d AS EXT 'distance from centre
END TYPE
GLOBAL TotPoints AS LONG
GLOBAL Minx AS EXT
GLOBAL Maxx AS EXT
GLOBAL Miny AS EXT
GLOBAL MaxY AS EXT
FUNCTION PBMAIN() AS LONG
LOCAL hGrf AS DWORD
LOCAL x AS LONG
LOCAL CentreX, CentreY,OffsetX,OffsetY,SumX,SumY AS EXT
'Set parameters
totpoints = 20
minx = 20
miny = 50
maxx = 260
maxy = 160
'Initialise a random set of points
DIM pts(1 TO TotPoints) AS CartPt
RANDOMIZE TIMER
FOR x = 1 TO TotPoints
pts(x).s = x
pts(x).x = RND() * (Maxx - minx) + minx
pts(x).Y = RND() * (Maxy - miny) + miny
SumX += pts(x).x
SumY += pts(x).y
NEXT
CentreX = SumX/TotPoints
CentreY = SumY/TotPoints
'Calculate polar coordinate from Centre point
FOR x = 1 TO TotPoints
OffsetX = pts(x).x - CentreX
Offsety = pts(x).y - Centrey
pts(x).a = ATN(Offsety / Offsetx) ' if you want to use degrees: RadToDeg(ATN(Offsety / Offsetx))
IF Offsetx < 0 THEN 'Quadrant 2 or 3
pts(x).a += Pi '180 if RadToDeg
ELSEIF Offsety < 0 THEN ' Quadrant 4
pts(x).a += TwoPi '360 if RadToDeg
END IF
pts(x).d = SQR(Offsety ^ 2 + offsetx ^ 2)
NEXT
ARRAY SORT pts(), CALL SortClockwise
GRAPHIC WINDOW NEW "Points - Hit any key to cycle and exit", 50,50,600,600 TO hGrf
GRAPHIC WINDOW STABILIZE
GRAPHIC SCALE (minx,miny) - (maxx,maxy)
PlotPoints Pts()
GRAPHIC WAITKEY$
DrawPoly(Pts(),CentreX,CentreY)
GRAPHIC WAITKEY$
GRAPHIC PAINT BORDER(CentreX,CentreY) , %RGB_GREEN, %RGB_BLACK
GRAPHIC WAITKEY$
Radiate(Pts(),CentreX,CentreY)
GRAPHIC WAITKEY$
GRAPHIC WINDOW END
END FUNCTION
FUNCTION SortClockwise(p1 AS Cartpt,p2 AS Cartpt) AS LONG
IF p1.a < p2.a THEN FUNCTION = -1 : EXIT FUNCTION
IF p1.a > p2.a THEN FUNCTION = 1 : EXIT FUNCTION
'same angle - sort "inside" one first
IF p1.d < p2.d THEN FUNCTION = 1 : EXIT FUNCTION
IF p1.d > p2.d THEN FUNCTION = -1 : EXIT FUNCTION
END FUNCTION
FUNCTION Radiate(pts() AS cartPt,cx AS EXT, cy AS EXT )AS LONG
LOCAL x AS LONG
FOR x = LBOUND(pts()) TO UBOUND(pts())
GRAPHIC LINE (cx,cy) - (pts(x).x,pts(x).y),%RGB_WHITE
NEXT
END FUNCTION
FUNCTION PlotPoints(pts() AS CartPt) AS LONG
LOCAL x AS LONG
LOCAL xsize,ysize AS EXT
xsize = (maxx-minx)/200
ysize = (maxy-miny)/200
FOR x = LBOUND(pts()) TO UBOUND(pts())
GRAPHIC ELLIPSE (pts(x).x -xsize,pts(x).y-ysize) - (pts(x).x+xsize,pts(x).y+ysize),%RGB_RED,%RGB_RED
NEXT
END FUNCTION
FUNCTION DrawPoly(pts() AS CartPt,cx AS EXT,cy AS EXT) AS LONG
LOCAL x AS LONG
'If wanted, mark the centre of the polygon
GRAPHIC SET POS (cX,cY)
GRAPHIC PRINT "X"
'Now draw it
GRAPHIC SET POS (pts(LBOUND(pts())).x,pts(LBOUND(pts())).y)
FOR x = LBOUND(pts()) + 1 TO UBOUND(pts())
GRAPHIC LINE (pts(x-1).x,pts(x-1).y)- (pts(x).x,pts(x).y)
NEXT
GRAPHIC LINE (pts(UBOUND(pts())).x,pts(UBOUND(pts())).y) - (pts(LBOUND(pts())).x,pts(LBOUND(pts())).y)
END FUNCTION
'
You will find it in Gary Beene's gbthreads.
„Let the machine do the dirty work.“
The Elements of Programming Style, Brian W. Kernighan, P. J. Plauger 1978
The Elements of Programming Style, Brian W. Kernighan, P. J. Plauger 1978