Wednesday, January 6, 2021

Genuary 2021 Day 6: "Triangle Subdivision"

This prompt sent me back to copying BASIC programs out of the old "Creative Computing" magazine on our family's Apple II. I dimly recall keying in a program that divided a triangle recursively, giving a heightfield. Our Apple II had 64 kilobytes of RAM, which a good chunk was given over to DOS and the BASIC overhead, so there wasn't a lot of room for fancy data structures or very complicated geometry. Then again, the hi-resolution graphics screen was 280x192 pixels, or less if you were picky about color.

This is a throwback to the output of that program, even though I only recall the basic algorithm. I pick random elevations for the corners of the triangle, and then subdivide the triangle up into four sub-triangles of half the width, and perturb the new vertices based on the parent triangle's vertices. This is similar to the "Diamond/Square" terrain generation algorithm.

After creating a triangular heightfield, I generate lines by projecting each x,y,z world point into a sx, sy point on the page. To honor the hacky BASIC program, I did this by multiplying each x, y, z component by a vector I thought would look nice. No matrix multiplies, per se, no look-vectors. I could rewrite this to use that, but meh.

Another thing this doesn't have is colors (the flat part to the right is maybe water, which could be blue), which would lead me to break my paths into zero or more land parts and zero or more water parts, which could totally be done, but again meh - especially since I was planning to draw this on my AxiDraw, which I wasn't going to load different pens into, so monochrome it is.

And still one more thing that this could have, but doesn't, is hidden surface elimination. I think the original Apple II program implemented this by retaining an array in screen-x that remembered the highest value that had been drawn at that value of X, thus not going "beneath" the horizon. I've used this many times before, I refer to it as "The Joy Division Algorithm". I could do something like that here, but since I'm dealing in a bigger space than the Apple II was, I'd want to do my clipping against the vector path information, rather than some quantized pixel positions. Again, this particular seed value doesn't benefit from this, so I skipped it.


Tools Used: AxiDraw, DrawSVG

Languages Used: Python3

Development Time: ~90 minutes

Drawing Time: ~3 minutes

What's Generative Here: The heightfield is generated using RNG. This might have been the first generative algorithm I ever played with, as a kid.











No comments:

Post a Comment