Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Centroid Question
#13
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
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 restart

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 Tongue
Then had to reset everything like max and min to loop through many, many examples with different amounts of points. Tongue


Attached Files Image(s)
   
b = b + ...
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: 1 Guest(s)