Coding: Bring the Noise

%26lt;0011 Em

2D random terrain: iterative diamond-square algorithm

leave a comment »

A year and a half ago, I created a simple application that allowed one to specify a folder of column-wise data, which axes to plot, a color scheme, and then with those parameters ran over the files and created an avi file from the data.  This was for my university’s CSUMS program, and while I was excited to release it to my peers, no one really understood what it did, or why they should care.

I needed some examples.  Since I wanted to start working on terrain generation and I was on a minecraft kick at the time, I read up on the Diamond-Square Algorithm and went with an iterative version of that.  The following is a mostly-cleaned-up version of that code.  I’m posting it today because this post reminded me of it, and I want to adapt it to C#/XNA and then implement my alternating directions implicit diffusion algorithm, so that I can see just how fast I can run that in a shader.

Implementation note: this wraps edges, so that you can tile the output seamlessly.

Here’s the code.   (You’ll need numpy and matplotlib.)

Read the rest of this entry »

Written by delwinna

April 30, 2012 at 10:35 pm

Posted in Python

Python: Word filtering and regex

leave a comment »

The Setup

Someone posed the question to me yesterday “given a list of all the permutations of vulgarities, how would you filter a relatively short message (say, 200 characters long) for one of those permutations?  This is time-sensitive, imagine on the order of potentially hundreds of checks a second.”

Asking for a bit more detail, I was told this list (array for you dirty non-python folk) was “over a million lines long”.  I had two immediate thoughts:

  1. That is a terrible representation of those permutations.  At least move to a basic trie and cut out (most) of your repetition.  Huge space savings if you’ve got similar words (likely, since they’re small variations of the same letters) and great savings in inclusion checks- O(k) instead of O(n), where n is the number of permutations, and k is the length of the longest permutation.
  2. Math senses tingling, primarily from combinatorics class.  There is NO WAY that this list is on the order of “millions”.  Since we can have multiple delimiters between characters, even conservative estimates for permutations of single words with restrictive assumptions and a small delimiter character set put us into the billions.  For one word.  Not buying it.

Read the rest of this entry »

Written by delwinna

April 26, 2012 at 3:54 pm

Posted in Uncategorized

Takkuso’s Cycles

leave a comment »

I saw an interesting post on /r/math today, and decided to write a small script to play around with it.

  1. Pick a positive integer, i
  2. Find the magnitude, m, of the difference between i and i’s reverse, j
  3. Repeat step 2 using m; you will eventually hit either 0 or a special number.

The special number is 21, followed by min(0, log10(i)-4) 9s, then 78.

A quick table:

# of digits Special number
4 2178
5 21978
6 219978
7 2199978

Below is the Python script in full, followed by a quick example usage.  I’m hoping to do some graphs when I get home.

Read the rest of this entry »

Written by delwinna

April 20, 2012 at 6:30 pm

Posted in Puzzles

Color Profiles

leave a comment »

Today’s post isn’t groundbreaking (are they ever) just a wrapper around a nested dictionary.  Turns out this is a really convenient pattern in general – I’m using a more general two-key dictionary in the engine that was built off of the below code.

I got sick of reading lots of this:

Color baseColor = defaultBaseColor;
if(Hostility == Hostility.Friendly)
    baseColor = Color.DarkGreen;
else if (Hostility == Hostility.Enemy)
    baseColor = Color.DarkRed;
BasicShapes.DrawSolidSquare(batch, Position, baseColor, SideLength, Rotation);

Now, the rendering code reads:

public class GameObject
    ColorScheme ColorScheme;
    Hostility Hostility;

    // ...

    protected Color LayerColor(LayerType layer)
        return ColorScheme[Hostility, layer];

    public virtual void Draw(SpriteBatch batch, RenderPass pass)
        // ...
        BasicShapes.DrawSolidSquare(batch, Position,
                LayerColor(LayerType.Base), SideLength, Rotation);

        var InnerPos = Position + new Vector2(SideLength / 2);
        var SmallSide = SideLength / 2;
        BasicShapes.DrawSolidSquare(batch, InnerPos,
                LayerColor(LayerType.Highlight), SmallSide, Rotation);

Source after the jump.
Read the rest of this entry »

Written by delwinna

March 23, 2012 at 4:17 pm

Posted in Projects

Digit counting in Python

leave a comment »

Question: what’s the fastest way to count the number of digits (base 10) in an integer (integral type, could be long) in python?

This came up when I was working on a ProjectEuler problem.  There are three options I came up with, and we can rule the first out immediately:

  1. Divide (as an integer) the number by ten and keep a counter of how many times we divided it, until the number is 0.
  2. Use log(n, 10) and properly account for floating point black magic
  3. Convert to a string, get the length

Read the rest of this entry »

Written by delwinna

January 27, 2012 at 2:40 am

Posted in Puzzles, Python

Old code: “playing with boids” OR “this would be a cool music visualizer”

leave a comment »

As I was sorting through my old code, I found this “gem” from my visual python experiments last year in college.  The code and instructions are at the end of this post.  I’d recommend reading the rest of the post before watching the video, otherwise it might not make sense.


Playing with boids (you can jump to 0:45)

Attractor jump at 2:05 (which is pretty freakin cool)

I particularly like how they respond to the attractor (blue ball) once they’ve settled into an orbit- it’s like an amoeba reaching out and surrounding a smaller organism and then devouring it all at once.

There’s some music in here from this playlist.

Read the rest of this entry »

Written by delwinna

January 22, 2012 at 4:03 am

Posted in Projects, Python

Experimenting with the Collatz Conjecture and C#’s GC

leave a comment »

For those new to the Collatz Conjecture

EDIT: Commented the latex tabular code. Get it here.

More after the break. Read the rest of this entry »

Written by delwinna

September 1, 2011 at 7:23 am

Posted in Puzzles