Maths!

Those of you who like maths should be happy - this is an effect that requires more maths than code. Those who don't like maths shouldn't worry too much though - it really is very simple.

Calculating the distance between two points is done using a thereom by Pythagoras. It runs:

distance = square root((x1-x2)2+(y1-y2)2)

...where the two points are (x1,y1) and (x2,y2). We want the distance squared - which is most fortunate as this cancels out the expensive square root operation from the equations. (By "expensive" I mean a function that takes a long time to perform). So now, for each pixel we can compare it to the centre point of the blob and calculate a distance squared. We can then turn this into a brightness - hang on, how are we going to do that?

If you think about it, our distances are going to be smallest on top of the centre of the blob - it'll be zero. As we get further and further away from the centre of the blob the distance2 will get larger and larger. If you start with the brightest value for the pixel then subtract the distance2 value, we'll get the value for the brightness of the pixel that we need to plot to the screen. Of course, you need to check that it's within our range of allowable pixel values (between 0 and 255, for example, in 24- and 32-bit video modes).

I know I'm not a great fan of source, so have some pseudo-source that illustrates what I mean a bit better:

for y is 0 to screen_height - 1
    for x is 0 to screen_width - 1
    
        // Calculate the distance2:
        distance_squared = (blob_x-x)2 + (blob_y-y)2
        
        // Turn it into a pixel brightness:
        pixel_brightness = 255 - distance_squared
        
        // Clip it into the range 0→255:
        if pixel_brightness < 0 then
            pixel_brightness = 0
        end if

        if pixel_brightness > 255 then
            pixel_brightness = 255
        end if
        
        // Set the pixel on the screen:
        set_screen_pixel(x,y) = pixel_brightness
    next x
next y

PreviousNextShow All IndexBlob effect demo