Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Centroid Question
#1
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
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply


Messages In This Thread
Centroid Question - by TerryRitchie - 07-13-2024, 06:00 PM
RE: Centroid Question - by Pete - 07-13-2024, 06:26 PM
RE: Centroid Question - by bplus - 07-13-2024, 07:58 PM
RE: Centroid Question - by TerryRitchie - 07-13-2024, 09:20 PM
RE: Centroid Question - by Pete - 07-13-2024, 09:44 PM
RE: Centroid Question - by justsomeguy - 07-13-2024, 11:56 PM
RE: Centroid Question - by bplus - 07-14-2024, 12:00 AM
RE: Centroid Question - by aadityap0901 - 07-14-2024, 06:14 AM
RE: Centroid Question - by TerryRitchie - 07-14-2024, 01:14 PM
RE: Centroid Question - by Kernelpanic - 07-14-2024, 01:11 PM
RE: Centroid Question - by bplus - 07-14-2024, 03:42 PM
RE: Centroid Question - by Kernelpanic - 07-14-2024, 06:24 PM
RE: Centroid Question - by bplus - 07-14-2024, 06:55 PM
RE: Centroid Question - by Kernelpanic - 07-15-2024, 03:07 PM
RE: Centroid Question - by Pete - 07-14-2024, 07:15 PM
RE: Centroid Question - by vince - 07-15-2024, 03:46 AM
RE: Centroid Question - by bplus - 07-15-2024, 01:02 PM
RE: Centroid Question - by Pete - 07-16-2024, 12:33 AM



Users browsing this thread: 3 Guest(s)