## 2D random terrain: iterative diamond-square algorithm

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.)*

## Python: Word filtering and regex

## 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:

- 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- instead of , where is the number of permutations, and is the length of the longest permutation.
- 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.

## Takkuso’s Cycles

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

- Pick a positive integer, i
- Find the magnitude, m, of the difference between i and i’s reverse, j
- 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.

## Color Profiles

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 »

## Digit counting in Python

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:

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

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

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.

## Experimenting with the Collatz Conjecture and C#’s GC

# 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 »