If you want something done right... don't wait for bplus to do it because he will post blunderful code many times before he gets this simple thing right
EDIT: oh crap min and max was not calculating correctly, fixed
something is still off!
Fixed: dividing by number of points does not work with ubound of array that starts at 0
Then had to reset everything like max and min to loop through many, many examples with different amounts of points.
Code: (Select All)
Const RED~& = _RGB32(255, 0, 0)
Const GREEN~& = _RGB32(0, 255, 0)
Const GRAY~& = _RGB32(64, 64, 64)
Const Blue~& = _RGB32(0, 0, 255)
Type iPoint
As Integer x, y
End Type
Dim Min As iPoint ' min x,y seen
Dim Max As iPoint ' max x,y seen
Dim Center As iPoint ' center point of random points
Dim Centroid As iPoint
Dim p As Integer
Dim tx, ty ' totals of x, y
_Title "Terry's Point (yellow) versus the Centroid Point in blue."
Screen _NewImage(640, 480, 32)
Randomize Timer
restart:
Min.x = 641 ' start min at max
Min.y = 481
Max.x = -1 'start max at min
Max.y = -1
Dim nRP As Integer: nRP = 3 + Int(Rnd * 9) ' Int(Rnd * 9' NUMBER OF RANDOM POINTS
ReDim rp(1 To nRP) As iPoint ' random point
tx = 0: ty = 0
For p = 1 To nRP ' create random points
rp(p).x = Rnd * 600 + 20 ' 20 pixel border of no points
rp(p).y = Rnd * 440 + 20
tx = rp(p).x + tx
ty = rp(p).y + ty
If rp(p).x > Max.x Then Max.x = rp(p).x
If rp(p).x < Min.x Then Min.x = rp(p).x ' get max/min x
If rp(p).y > Max.y Then Max.y = rp(p).y
If rp(p).y < Min.y Then Min.y = rp(p).y ' get max/min y
Next
Center.x = (Max.x + Min.x) / 2 ' get center x
Center.y = (Max.y + Min.y) / 2 ' get center y
Centroid.x = Int(tx / nRP)
Centroid.y = Int(ty / nRP)
For p = 1 To nRP ' draw points in red, lines to center in gray
Line (rp(p).x, rp(p).y)-(Centroid.x, Centroid.y), GRAY
Circle (rp(p).x, rp(p).y), 2, RED
Paint (rp(p).x, rp(p).y), RED, RED
Next
Circle (Center.x, Center.y), 2, GREEN ' draw green center point
Paint (Center.x, Center.y), GREEN, GREEN
Circle (Centroid.x, Centroid.y), 2, Blue ' draw green center point
Paint (Centroid.x, Centroid.y), Blue, Blue
Print Min.x; Min.y, Max.x; Max.y, Center.x; Center.y, Centroid.x; Centroid.y ' display values
Locate 30, 1: Print "zzz... sleeping press any for another Centroid calc.";
Sleep
Cls
GoTo restartEDIT: oh crap min and max was not calculating correctly, fixed
something is still off!
Fixed: dividing by number of points does not work with ubound of array that starts at 0

Then had to reset everything like max and min to loop through many, many examples with different amounts of points.
724 855 599 923 575 468 400 206 147 564 878 823 652 556 bxor cross forever

