02-08-10
Yet another dying project, the sunday post.
Another project I’ll probably not ever finish.
Just a small C program that finds color aggregates.
input:

output:

Another example,
input:

output:

02-03-10
Fun with binary (part two), multiples of 8
Working on my latest school assignment for which I have to program a memory allocator (that’s just a cool way of saying “malloc”) I needed to find the closest multiple of 8 for any unsigned integer n. So the first thing I thought was, check if the remainder of n / 8 is 0, if so it’s a multiple of 8 so just return that value, if not, increment n and check again, until the result is 0, then return that number.
This is the snippet for that algorithm:
1 2 3 4 5 6 7 | unsigned int multiple_of_8(size_t n) { if (!(n % 8)) return (n); while (n++ % 8) ; return n - 1; } |
Yeah… ok, it works, but I thought it would be cool to use some bitwise operations to get the same result avoid using a loop.
So I read this and then took a pen and some paper and tried to figure out a way to get the closest multiple of any value using bitwise operations. I failed. I nailed it for multiples of 8 after a good hour of counting 0s and 1s, but not for any value of n (sadly).
So here it is:
1 2 3 4 | unsigned int multiple_of_8(size_t n) { return (((n + 7) & ~7)); } |
Now that is cool. This really got me on my way.
So, how the f*ck does this work ?
As someone I know would say, “it’s easyyyyyyyy!”
A simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | n = 4 0100 r = 0; // the result r = (n + 7) & ~7 r = (4 + 7) & ~7 r = (0100 + 0111) & ~0111 //adding 4 + 7 is no trouble r = (1011) & ~0111 // now, what the ~ does is swap all the bits, so a 1 becomes a 0, // and a 0 becomes a 1 r = (1011) & 1000 // then, last step, the bitwise & replaces all bits by 0, // except if both bits are 1 r = 1011 & 1000 r = 1000 r = 8 // YAY, the result is 8, which is the closest multiple of 8 from 4. // cool. // another example n = 20 10100 r = 0; r = (10100 + 0111) & ~0111 // add 20 + 7 r = (11011) & ~0111 // twiddle the ~7 r = (11011) & 1000 // AND them out r = 11011 & 1000 // hooray, it works! r = 11000 r = 24 |
Now that is cool as I said, it’s cleaner and doesn’t use a loop, and it makes me happy!
01-12-10
Spherical distribution
Wow holly cow, I finally got some time off, and spent a few hours messing arround with AS3. There’s something I’ve been wanting to do lately, and that was, write something a little nicer than C command line apps and stuff.
So anyway, I was reading some of Paul Bourke’s articles and came across this. I know it’s been done before, countless time, by this dude for example. So I tried out this implementation using DirectX9, and it was slow as fuck. So either I did something wrong, or it’s no the best algorithm to be using if you moving stuff arround. I ended up reading the entire page (for once), and then implemented the last algorithm on the page: (0 <= theta < 360) and phi (0 <= phi <= pi/2).
For an in depth explanation, read this: http://local.wasp.uwa.edu.au/~pbourke/geometry/spherepoints/.
OK, so that was well faster since none of the points are being moved arround to be equidistant from each other.
All I did after that was use the sound spectrum to alter the distance from the points to the middle of the sphere to make it less boring to look at. Then added some glow where there is a little point aggregation or whatever you want to call it.
Long story short, this lets you quickly plot out random points arround a sphere or a circle, or even a square or cube. Even though it’s random, the distribution still looks nice enough.
Anyway, here it is.
And here is the AS3 source.
10-17-09
London lomography
Just a few photos shot in London with my Holga.
10-07-09
First steps with DirectX
A little 3D terrain application with ambient and point lighting using the C++ Direct3D 9 API. Nothing mind blowing, but it allowed me to learn some of the basics in using DirectX.
You can download the .exe file and run it on Windows xp, I’m not sure it will run on vista without it being re-compiled (cross platform much ?).
If you get it to run, use the left and right keys to rotate… left and right, and use the up key to move the “texture”, it’s just perlin noise.
Overall, DirectX is very cool and pretty easy to get running.
First steps with DirectX from __dominic on Vimeo.
Quality is pretty bad, sorry about that.
10-02-09
Fast array item removal
Deleting items in an array in C can be a pain. You allocate a new array with one cell less than the array from which you are deleting an item. Then copy all items from the original array to the other one, then skip the entry to be deleted, and the copy the rest. Last but not least, you need to free the memory used by the array. Thats alot for just removing an item at index n.
I’m sure it’s not the only way to do it, but it gets the job done, even if it’s slow.
Now, if you have an array in which the order of the items is NOT important AT ALL, that’s good news. There’s a fast way to remove an item, and, it’s simple as hell.
The idea is just to move the last item of the array to the index of the item to be deleted and to null out the last item. You need to null out the last item in order to avoid doubles and to “resize” the array.
Simple little trick, but it comes in handy.
Here is a simple little implementation example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include <stdio.h> #include <stdlib.h> #include <string.h> int error() { puts( "args error" ); return 0; } void fremove_item( char *arr, int index ) { arr[ index ] = arr[ strlen( arr ) - 1 ]; arr[ strlen( arr ) - 1 ] = '\0'; } void remove_all_items( char *arr ) { while ( arr[ 0 ] != '\0' ) { fremove_item( arr, 0 ); puts( arr ); } } int main( int argc, char **argv ) { char *ids; int i; if ( argc < 2 ) return error(); ids = malloc( ( strlen( argv[ 1 ] ) ) + 1 * sizeof( *ids ) ); i = 0; while ( argv[ 1 ][ i ] != '\0' ) { ids[ i ] = argv[ 1 ][ i ]; i++; } ids[ i ] = '\0'; printf("initial string: [%s]\n", ids ); remove_all_items( ids ); printf("result string : [%s]\n", ids ); free( ids ); return EXIT_SUCCESS; } |
This is the Makefile used to compile the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | CC = gcc NAME = arf SRC = array_item_remove.c OBJ = $(SRC:.c=.o) CFLAGS = -W -Wall -ansi -pedantic $(NAME): $(OBJ) $(CC) -o $(NAME) $(OBJ) all: $(NAME) clean: rm -f *~ $(OBJ) fclean: clean rm -f *# $(NAME) re: fclean all |
09-22-09
I have an Holga!
It had to happen, but no tech, math, programming or any other likewise nonsense tonight, and for once (now that’s a blessing).
I am the happy owner of a Holga 120CFN, so now of course I have a lomography account.
And no, I’m not a photographer or anything, I just like taking snapshots and having fun with the plastic lens and/or blinding people with the flash.
Time to go twitt about all this.
My pops with our (used to be) cat.

(don’t get all emotional please!!!)
09-13-09
f(x,y) = (x²+y²)² and ackermann, the sunday post.
I got the finest inspiration today: the ackermann function.
Then, one of those moments: “I’m bored, why not port the function to AS3 ?”
Short pause, then:”Because AS3 is not recursion friendly, use processing”.
Result:ackermann function applet.
A few minutes later:”Why not try and port another function ?”
Google search, few minutes, result:wierd f(x,y) function applet.
Conclusion:sunday is boring, xkcd is fun, I’m off to play COD4
09-06-09
ffffound found perlin
Hey kids, trying to pass time on a boring sunday afternoon, just some basic “Perlin motion” (yeah, that sounds cool) using the colour data from a few images I took from www.ffffound.com.
here is a link to the first image in the swf, (sadly) I lost the other ones.
If you don’t know what perlin noise is (you suck at life), you really should google it, and then go watch tron.
09-01-09
twittfeed
title says it all, twittfeed.