GLBasic User Manual

Main sections

DIMDEL

DIMDEL array#$[], index%



Deletes the entry at 'index#$' from the 'array'. All following entries will be moved upwards. It does not matter how many dimensions the array has.

If index% is negative, it will be counted from the end of the array. Thus,
DIMDEL a[], -1
will remove the last element.

For better understanding. This array:
DIM a[3][2]
a[0][0] = 0
a[0][1] = 1
a[1][0] = 10
a[1][1] = 11
a[2][0] = 20
a[2][1] = 21
DIMDEL a[], 1

will look afterwards like:
a[0][0] =  0
a[0][1] = 1
a[1][0] = 20
a[1][1] = 21


An example for daily use. Here you can create stars with the left mouse button and remove them with the right mouse button.

// StarMaker
// -----------------------
LOCAL mx, my, b1, b2, best
DIM star[0][2]
mousedown = FALSE


// Main Loop
WHILE TRUE
    MOUSESTATE mx, my, b1, b2
    // Mouse pointer
    PRINT "<=", mx, my
    // Click+Release
    IF b1=FALSE AND b2=FALSE THEN mousedown=FALSE
    IF mousedown
        b1=FALSE
        b2=FALSE
    ENDIF
    IF b1 OR b2 THEN mousedown=TRUE
    
    // Left = add, Right = delete
    IF b1 THEN AddStar(mx,my)
    IF b2 THEN DelStar(mx,my)
    
    // find nearest star
    best = Nearest(mx, my)
    // Show all stars
    FOR i=0 TO BOUNDS(star[],0)-1
        // highlight nearest
        IF best = i
            PRINT "*", star[i][0], star[i][1]
        ELSE
            PRINT "+", star[i][0], star[i][1]
        ENDIF
    NEXT
    SHOWSCREEN
WEND

// -----------------------------------
// DELSTAR
// Delete a star
// -----------------------------------
FUNCTION DelStar: x,y
LOCAL i
    i = Nearest(x,y)
    // Magic!
    IF i>=0 THEN DIMDEL star[], i
ENDFUNCTION

// -----------------------------------
// ADDSTAR
// Add a star
// -----------------------------------
FUNCTION AddStar: x,y
LOCAL m
    m = BOUNDS(star[], 0)
    REDIM star[m+1][2]
    star[m][0] = x
    star[m][1] = y
ENDFUNCTION

// -----------------------------------
// NEAREST
// Find the nearest star to x,y
// -----------------------------------
FUNCTION Nearest: x,y
LOCAL best, bestdist, dist, i, dx, dy
    best = -1
    FOR i=0 TO BOUNDS(star[],0)-1
        dx = x-star[i][0]
        dy = y-star[i][1]
        dist = SQR(dx*dx + dy*dy)
        IF i=0 OR dist<bestdist
            bestdist = dist
            best = i
        ENDIF
    NEXT
    RETURN best
ENDFUNCTION

See also...