LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Blogs > MrCode
User Name
Password

Notices



This is the first "blog" of any kind I've ever had, so it's probably not the greatest...

Just a little snippet about me:

I'd like to consider myself relatively tech-savvy; obviously I use Linux (Arch to be precise; K.I.S.S. FTW), and I enjoy learning new things about computers in general (both software and hardware-related).

This blog is mostly just for whenever I feel like telling the world about my experiences with computers/Linux (or just life in general), or just posting for the hell of it.
Rate this Entry

A little "fractal" I discovered…

Posted 07-20-2011 at 03:45 PM by MrCode
Updated 07-25-2011 at 04:56 AM by MrCode (tinypic dumped one of my images; replaced :D)

…while I was still learning the Windows API.

I was experimenting with (very slow) procedural image generation with SetPixel(), when I did something like this for each pixel in the image:

Code:
SetPixel(x,y,RGB((x ^ y) * (cr.right - cr.left) * 0.025,
                 (x ^ y) * (cr.right - cr.left) * 0.025,
                 (x & y) * (cr.right - cr.left) * 0.025));
…or something like that (I don't remember all the arguments to Windows' SetPixel() ).

This is how it turned out ():



I'm not sure if this "pseudo-fractal" has a distinct name, but I know it's related to the "Munching Square" (in fact that WP article has an image of exactly what I did here).

I recently started messing with direct surface pixel manipulation in SDL, and I thought "hey, why not try re-creating my old Windows GDI experiment? So I went ahead and copy-pasted the RGB macro from my old Windows code (I wrote my own RGB #define for the SDL program) and tweaked it a little so that there wouldn't be any weird color distortions.

…and voilá ():



…the relevant drawing code (you'll need to create/lock a 32-bit RGB surface for direct pixel data access before calling DrawMunchingSquares()):

Code:
#define RGB(r,g,b) \
        ((unsigned char)r << 0x10) + \
        ((unsigned char)g << 0x8)  + \
        ((unsigned char)b)

[...]

void SetPixelColor(SDL_Surface* surf,int x,int y,unsigned color)
{
    unsigned* pixels = surf->pixels;

    pixels[(y * surf->w) + x] = color;

}

void DrawMunchingSquares(SDL_Surface* surf,float munch)
{
    int l,p;
    for(l = 0; l < surf->h; l++)
    {
        for(p = 0; p < surf->w; p++)
            SetPixelColor(surf,p,l,RGB(((p ^ l) * munch),
                                       ((p ^ l) * munch),
                                       ((p ^ l) * munch)));
    }
}
I apologize if this post seems pointless and/or boring, but I just wanted to do something a bit "happier", seeing as how my previous blog entry was basically just an outburst of existential angst.
Views 3099 Comments 2
« Prev     Main     Next »
Total Comments 2

Comments

  1. Old Comment
    Very interesting... Add an alpha channel calculation in there and you got one hell of a texture layer addition... maybe some kind of metal reflection calculation?
    Posted 07-20-2011 at 06:34 PM by lumak lumak is offline
  2. Old Comment
    It's pretty sweet when you can translate some technical concept into an aesthetic concept like this. Very interesting indeed.
    Posted 07-21-2011 at 04:49 PM by rocket357 rocket357 is offline
 

  



All times are GMT -5. The time now is 12:17 AM.

Main Menu
Advertisement

Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration