07-14-2024, 12:00 AM
(07-13-2024, 06:00 PM)TerryRitchie Wrote: Ok you math wizards, I need your input/advice. I've been reading up on centroids and barycenters because I want to find the center point of a given set of x,y points. The suggested math for this as found on the Internet is truly impressive (i.e. wow).
However, why couldn't you just use the center point of the min/max x,y point seen in the set? The code illustrates what I have in mind. Any thoughts on this approach? Good, bad? If bad, why? Thanks for taking the time to read this.
Code: (Select All)CONST RED~& = _RGB32(255, 0, 0)
CONST GREEN~& = _RGB32(0, 255, 0)
CONST GRAY~& = _RGB32(64, 64, 64)
TYPE iPoint ' x,y point construct
x AS INTEGER
y AS INTEGER
END TYPE
DIM rp(19) AS iPoint ' random point
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 p AS INTEGER ' counter
RANDOMIZE TIMER
Min.x = 319 ' start min/max at center of screen
Min.y = 239
Max = Min
SCREEN _NEWIMAGE(640, 480, 32)
p = 0
DO ' create random points
rp(p).x = RND * 640
rp(p).y = RND * 480
IF rp(p).x > Max.x THEN Max.x = rp(p).x ELSE 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 ELSE IF rp(p).y < Min.y THEN Min.y = rp(p).y ' get max/min y
p = p + 1
LOOP UNTIL p > UBOUND(rp)
Center.x = Min.x + ((Max.x - Min.x) / 2) ' get center x
Center.y = Min.y + ((Max.y - Min.y) / 2) ' get center y
p = 0
DO ' draw points in red, lines to center in gray
LINE (rp(p).x, rp(p).y)-(Center.x, Center.y), GRAY
CIRCLE (rp(p).x, rp(p).y), 2, RED
PAINT (rp(p).x, rp(p).y), RED, RED
p = p + 1
LOOP UNTIL p > UBOUND(rp)
CIRCLE (Center.x, Center.y), 2, GREEN ' draw green center point
PAINT (Center.x, Center.y), GREEN, GREEN
PRINT Min.x; Min.y, Max.x; Max.y, Center.x; Center.y ' display values
OK I would compare the proper calc of centroid = ave x and ave y as Pete says and mid x and mid y.
BTW what are centroids used for, I asked Google and got Bull Science.
b = b + ...