Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Centroid Question
#15
nice mod, bplus, check out my old centroid mod

Code: (Select All)
deflng a-z
const sw = 800
const sh = 600
dim shared pi as double
pi = 4*atn(1)
declare sub circlef(x, y, r, c)

n = 14
dim x(n), y(n)

for i=0 to n-1
    x(i) = 200*cos(2*pi*i/n)
    y(i) = 200*sin(2*pi*i/n)
next

'x(0) = 0: y(0) = -200
'x(1) = -30: y(1) = -30
'x(2) = -200: y(2) = 0
'x(3) = -30: y(3) = 30
'x(4) = 0: y(4) = 200
'x(5) = 30: y(5) = 30
'x(6) = 200: y(6) = 0
'x(7) = 30: y(7) = -30
x(n) = x(0)
y(n) = y(0)

for i=0 to n-1
    x(i) = 200*cos(2*pi*i/n)
    y(i) = 200*sin(2*pi*i/n)
next

'screenres sw, sh, 32
screen _newimage(sw, sh, 32)
redraw = -1
drag = 0
do
    'm = getmouse(mx, my, mw, mb)
    do
        mx = _mousex
        my = _mousey
        mb = -_mousebutton(1)
    loop while _mouseinput
   
    if drag then
        if mb = 1 then
            x(ii) = mx - sw/2
            y(ii) = sh/2 - my
        else
            drag = 0
        end if
    else
        dmin = 1e10
        for i=0 to n - 1
            dx = (mx - sw/2) - x(i)
            dy = (sh/2 - my) - y(i)
            d = (dx*dx + dy*dy)
            if d < dmin then
                dmin = d
                ii = i
            end if
        next

        if mb = 1 then
            omx = mx
            omy = my
            drag = -1
        end if
    end if

    a = 0
    cx = 0
    cy = 0
    for i=0 to n-1
        d = x(i)*y(i + 1) - x(i + 1)*y(i)
        cx = cx + (x(i) + y(i + 1))*d
        cy = cy + (y(i) + y(i + 1))*d
        a = a + d
    next
    cx = cx/(3*a)
    cy = cy/(3*a)

    redraw = -1

    if redraw then
        'screenlock
       
        line (0,0)-(sw,sh),_rgb(0,0,0),bf
        locate 1,1: ? m, mx, my, mw, mb

        x(n) = x(0)
        y(n) = y(0)
        x0 = sw/2 + x(0)
        y0 = sh/2 - y(0)
        for i=1 to n
            x1 = sw/2 + x(i)
            y1 = sh/2 - y(i)
            line (x0, y0)-(x1, y1)
            'line -(sw/2 + cx, sh/2 - cy)
            circlef x1, y1, 8, _rgb(255,255,255)
            x0 = x1
            y0 = y1
        next
        circlef sw/2 + x(ii), sh/2 - y(ii), 8, _rgb(255,0,0)

        circle (sw/2 + cx, sh/2 - cy), 10, _rgb(255,255,0)
        circlef sw/2 + cx, sh/2 - cy, 7, _rgb(255,255,0)

        'screenunlock
        'screensync

        redraw = 0

        _display
    end if
loop until _keyhit = 27
system


sub circlef(x, y, r, c)
    x0 = r
    y0 = 0
    e = -r

    do while y0 < x0
        if e <=0 then
            y0 = y0 + 1
            line (x - x0, y + y0)-(x + x0, y + y0), c, bf
            line (x - x0, y - y0)-(x + x0, y - y0), c, bf
            e = e + 2*y0
        else
            line (x - y0, y - x0)-(x + y0, y - x0), c, bf
            line (x - y0, y + x0)-(x + y0, y + x0), c, bf
            x0 = x0 - 1
            e = e - 2*x0
        end if
    loop
    line (x - r, y)-(x + r, y), c, bf
end sub
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: 6 Guest(s)