home
on exploration, introspection and creation

Archive for the ‘miniproject’ Category

Does anyone ever do [fill in the blank]?

Thursday, September 22nd, 2011

There are a multitude of everyday opportunities to engage with people or institutions, but it’s very likely that nobody really does anymore. Here are some, with a suggestion for a miniproject for each.

  • “How is my driving? Call such and such number with compliments or complaints”. I doubt people actually call to complain, but they certainly don’t call to compliment. If you see a sticker like this, and the driver does something worth complimenting, call the number.
  • The inspection certificate for this elevator is located at the front desk. I bet nobody actually asks to see these. Do it next time you see a note like this in the elevator.
  • An “A” grade given to a New York City restaurant means that the restaurant had at most 13 sanitary violations. Which means it could have 13 sanitary violations! Call the NYC Department of Health and ask what these violations were for a restaurant of your choice.
  • Read an End User License Agreement. In its entirety. Of a single app. I dare you.
  • Movie credits always state that no animals were harmed in the production of the movie. Verify it (how does one even go about doing it?).

An Infinite Pinball Board

Friday, August 19th, 2011

Consider a ball balanced perfectly on one of the pegs in an infinite pinball board:

An infinite pinball board

A microscopic vibration sets the ball in motion. Can you place and size the pegs in such a way that the ball will fall directly on top of the peg below, bounce directly vertically up, and rest on the peg, waiting for yet another microscopic vibration to set it in motion, and so on, thus exemplifying one-dimensional Brownian motion.

Programming Unix/Mac circa 2006

Tuesday, November 2nd, 2010

The computing world changed tremendously between 2000 and 2006… in 2006 I completed a second version of the reference guide, this time focused on Unix and Mac superuser experience (less about programming, more about terminal usage). Most of the stuff here is timeless and still works beautifully, thus yet again proving that good technology never becomes obsolete.

Programming Mac/Unix circa 2003-06

Programming PCs circa 2000

Monday, October 18th, 2010

As a teenager I spent a lot of time programming. I was mostly interested in making games, and for this I had to get involved in the fairly low-level structures, such as interrupts, system calls, and I/O ports. While I mostly programmed in C (DJGPP — a C clone that was available for free for PCs, implementing an extended memory module which allowed me to finally relax the limitation of being able to use up at most 640 kB of memory in my games), I had to implement some assembly routines because using a high-level language for this was simply too slow. The most complex graphics routine I implemented was a routine to copy a rectangular block of pixels from one place (usually an off-the-screen buffer) to another in a way to preserve all pixels marked with a special color (a mask).

It was a wonderful world–there is something magical about dealing with low-level operations. The information I needed to display pixels on screen, or to control the mouse felt in a way like knowing a secret code that unlocks the door with the treasure behind it.

Around 2000 I had enough knowledge (all before I ever got a modem!) to compile it into a kind of cheatsheet. Now, about ten years later, it’s time to share it with the world. Of course, very little of it is relevant anymore — although most if not all of the information should still produce the same effects, thanks for the crippling yet comforting fact that PCs have been painfully backwards compatible for the past decade or more.

The PDF is a little dense, so it deserves a brief walkthrough.

  • Some operations were possibly simply by inspecting a fixed location in the computer’s memory. Most of the information could simply be read, but some could also be fetched by writing a particular set of codes in specific locations. Putting information directly in the computer’s memory has never been recommended (and these days, virtual memory makes it almost impossible), but by inserting bytes directly in memory the computer’s behavior could be changed wildly — most times it would crash the OS, but sometimes it allowed you to get infinite lives in your favorite game or get some creepy screen effects. I used to stay up at night and hunt for locations in memory, mucking with which produced the most spectacular effects.
  • Most low-level operations were provided through issuing a set of interrupt operations to the microprocessor. The OS (in this case DOS) would interpret them in a particular way. Usually you needed to specify additional information — you did that by writing directly to the processor’s registers
  • I spent a lot of time figuring out how to display graphics on the screen. Back in the early 2000, everyone’s resolution of choice was a 320×200 resolution with a color table of 256 colors. Since every pixel was a byte, and the colors could be changed globally, this allowed for a number of games that displayed graphics fast and cycled through the colors. Extended modes (called VESA) were also possible and they offered higher resolutions and full color spectra (15-bit, 16-bit or 24-bit)
  • When you wrote a game, you pretty much had to intercept everything that the OS (MS-DOS) tried to do for you. The default mouse offered by MS-DOS was awful, the keyboard had a frustrating delay that you couldn’t get rid of and the OS wouldn’t even inform you when most of the keys were pressed. Fortunately, it was possible to handle the keyboard and the mouse through similar OS interrupts
  • Finally, the document concludes with some common file formats.

Fortunately, technology today has great abstractions and high level routines that make it unnecessary to know most of what’s in the cheatsheet. I am proud to have had to figure all this out, though — in an extension to many Java jokes, I think in order to be a great programmer, you have to understand the technology stack all the way down to microprocessor commands. The effect is a kind of deep connection with technology, the ability to make optimizations based on what’s going on deeper in the stack (such as my need to write assembly code back in 2000), but also a good deal of humility.

Here it is: programming-cheatsheet-2000.pdf

The Pencil Curve

Sunday, September 26th, 2010

What is the shape of the curve traced by one tip of a pencil as you roll it up a cylinder (the pencil being tangent to the cylinder at all times)? The pencil starts just touching the cylinder. As the pencil moves closer to the cylinder, the tip first moves away, then quickly moves back, eventually to stop as the pencil becomes vertical.

Below is the diagram of the pencil’s start point, the end point, and an arbitrary point.



The pencil rolling on a cylinder. The tip tracing a curve is marked.

It’ll be easier to parameterize the curve: determine the coordinates of each point as a function of the distance of the bottom tip of the pencil to the bottom of the cylinder (where it is tangent with the table). Initially the pencil’s tip is just touching the cylinder and the distance t is equal to l, the length of the pencil. At the end, as the pencil is vertical, t=r, the radius of the cylinder.

We have, from the arbitrary point, \frac{y}{x+t}=tan 2\alpha = \frac{2tan \alpha}{1-tan^2\alpha} = \frac{2r/t}{1-r^2/t^2} = \frac{2rt}{t^2-r^2}.

\frac{x+t}{l}=\cos 2\alpha = 2\cos^2\alpha-1 = \frac{2t^2}{r^2+t^2}-1 = \frac{t^2-r^2}{t^2+r^2} so x = \frac{l(t^2-r^2)}{t^2+r^2}-t.
Similarly, y = \frac{2rt}{t^2-r^2}\cdot (x+t) = \frac{2rt}{t^2-r^2}\cdot\frac{l(t^2-r^2)}{t^2+r^2} = \frac{2rtl}{t^2+r^2}

We can plot the curve as a function of t:



Plotted pencil curve

Interchanges

Wednesday, September 22nd, 2010

Here is my version of a stack interchange — a system of two highways intersecting such that cars coming from any direction can either go straight, turn right onto the intersecting highway, or turn left in the opposite direction of the intersecting highway (I didn’t allow U-turns so as not to complicate things too much).



My highway interchange: the red arrows show the paths that drivers going in a particular direction could take. Note that at most two lanes intersect at a point which makes it conceivable to build the interchange with two levels only; in the diagram the broken lane is below the other

And here is a slightly different version that doesn’t suffer from the problem of a center being too dense — if you look very carefully, you’ll see that in the version above, the centers of each of the arcs would meet unless the drop is not uniform.



Addressing the problem of a dense center

It seems to me that it can be built with two levels only — although something makes me think that it’s not that viable to build (since existing stack interchanges require four or more levels), plus to ensure a practical speed it would either have to have a rather large surface area, or only allow passenger cars driving with reduced speed. Here it is in three dimensions



My stack interchange in three dimensions



My stack interchange, zoomed in. The lane splits into three lanes and each separate lane takes you into one of the three directions.

You can download my Google SketchUp file here.

The structure of elevenseconds.com

Tuesday, September 21st, 2010

I tend to favor building to buying when I want to learn something, so when I first started constructing the main site elevenseconds.com I thought of building myself a small Rails-like framework. The main difference would be that it would apply to a simple principle of just-enough abstraction layers, that is, I would introduce a new abstraction layer when I left that the cost of maintaining the current code was too high. I am currently pretty happy with the structure of the site, although updating the front page takes a bit of time so perhaps it’s time to introduce a new layer of abstraction.

At a high level, all requests go to an index page

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.cgi [QSA,L]

which grabs the desired page to be visited from the URL as well as the subpage option (I thought it would be cool to separate the option with a colon — I never liked the questions marks and ampersands, and overloading slashes for paths and parameter separations never agreed with me. And so http://elevenseconds.com/photography/italy:slides takes you to an index page which knows to display a page called photography/italy with an option of slides.

Most of the pages are very similar so I put in place a kind of Metacontent-Metaview paradigm: every page request will fetch content from the /content/ directory. The ruby files there are organized in the same hierarchy as the URLs, so the above link will fetch content from /content/photography/italy.rb.

The actual content file is very configuration-like; let’s take a look at a part of the main content file (root URL resolves to /content/main.rb):


append = []
append << rendering('text', {
  'x'=>1,
  'y'=>0,
  'text'=>'<img src="/images/ui/whatis.png"/>11seconds: on exploration, introspection and creation'
})
append << rendering('verticalLine', {
  'x'=>1,
  'y'=>2,
  'size'=>3
})
append << rendering('imageWithCaptionOnRight', {
  'x'=>1,
  'y'=>2,
  'link'=>'http://blog.elevenseconds.com/trends/',
  'src'=>'/images/thumbs/main/glogo.png',
  'text'=>'trends'
})
append << rendering('imageOnRight', {
  'x'=>1,
  'y'=>11.5,
  'link'=>'/pictures/glowdoodle',
  'src'=>'/thumb.cgi?im=/images/pictures/glowdoodle/sadterminator.jpg&x1=197&y1=74&ss=344',
  'id'=>'projects',
  'text'=>'glow doodles'
})
render('main', {'append'=>append})

append is really a collection of items to append. Each item is a rendered view with some parameters. Each view is simply some HTML markup with Ruby inline code. For example, imageOnRight.rb contains

<div class="rightOfLine" style="left:#{x*35+2}px;top:#{y*35+69}px">
  <a href="#{link}">
    <img class="image" src="#{src}"
      onMouseOver='caption("#{id}", "#{text}", "#{link}")'
      onMouseOut='caption("#{id}", "", "")'/>
  </a>
</div>

(To make the page more structured, I decided on absolute positioning — maybe that’s because I’m a bit of a control freak.)

Note that I’m not quite allowing hierarchical content — there is no need for it given the current design of the site. All I need to do is a two-level hierarchy: a main view (the last line in the main.rb file above may include a bunch of other views.

I needed to introduce another abstraction because a lot of the sites with photography looked the same and I saw myself writing the same code over and over again. So I wrote a function that displays a generic set of pictures. Take our italy.rb content page:


category = 'photography/italy'
caption = 'italy june 2010'
images = [
  ['italy-0', true, '115, 45, 305'],
  ['italy-1', true, ''],
  ['italy-2', true, '296, 20, 54'],
  ...
  ['italy-45', true, '117, 75, 85'],
]
append = []
append << generateSeries(images, category, caption, [534, 356, 2.42])
render('main', {'append'=>append})

The above specifies the absolute minimum that is needed to render the Italy photographs — a definition of each images (I don’t always just number them in sequence), with a specification of whether the image is in landscape (true) or portrait (false) mode, and the top-left coordinate and the size of a window which will be made into a thumbnail. If not specified, the function chooses a smart default.


$explore = [
  ['http://www.gagneint.com/Final%20site/Animation/Sensology/Sensology.html', '/images/thumbs/main/sensology.png', 'sensology'],
  ['http://js1k.com/demos', '/images/thumbs/main/js1k.png', 'demos'],
  ...
]

I do the same with the front page — since it’s mostly made up of a bunch of links, I define each (in the example above, all I need for a link is a URL and a thumbnail picture) and then just cycle over the array. I only look at the latest 10 images for each category (except for EXPLORE which gets 20) but when you click on the link you get them all (http://elevenseconds.com/detail:explore) — again, code reuse.

As the content gets more streamlined (and as I get more lazy), I’ll probably go a step further towards configuration and just define everything in some standard configuration format like JSON (XML may be human-readable, but it’s certainly not human-writeable!). My goal is not quite to minimize the number of characters that I type (because then the conventions at play will no doubt be too obscure, plus the amount of plumbing code I’ll have to write too much for me to remember what I wrote in a year), but I’ll want to get close to it.

The thumbnails are also pretty painless. The utility page, thumb.cgi, generates a thumbnail out of any picture. You just need to specify the top-left coordinate and the size of the window to made into a thumbnail, and the resulting size. Most thumbnails on my page are 32×32 (it’s a kind of hallmark of the site). Some are 67×67 (not 64×64 because I want the images to line up — two 32×32 in a row actually take up ((1+32+1)+1+(1+32+1)) = 69 pixels because of the border and the one-pixel gap so to replace it with a bigger picture means it needs to be 67: (1+67+1)=69.

thumb.cgi saves the autogenerated thumbnails in a cache directory so that the images don’t have to be scaled each time the page is loaded. That is, when thumb.cgi is called with the same arguments again, it will just bring up the saved thumbnail instead of resizing and cropping the picture:


# by default output png unless the target size is specified
format = cgi.has_key?('sc') ? 'jpeg' : 'png'
hash = Digest::SHA1.hexdigest(ENV['REQUEST_URI'].to_s())[0..7]
cacheFile = "cache/" + hash + "." + format
if(File.exist?(cacheFile))
  puts cgi.header("image/#{format}")
  file = new File(cacheFile, "r")
  puts file.sysread(20)
  exit
end

But (great example of just-enough abstraction layers), I go even further: instead of having thumb.cgi do the logic, I replace the source of the image when rendering the view with its cached image. That way I don’t even have to call thumb.cgi which significantly speeds up the page load:


def gen2cache(html)
  source = html.clone
  links = []
  while(source =~ /"(\/thumb.cgi\?[^"]+)"/)
    links.push($1)
    source[$1] = ''
  end
  links.each { |link|
    format = link.index('sc=') ? 'jpeg' : 'png'
    hash = Digest::SHA1.hexdigest(link)[0..7]
    cacheFile = "cache/" + hash + "." + format
    if(File.exist?(cacheFile))
      html[link] = "/"+cacheFile
    end
  }
  return html
end

I guess the next step would be to actually autogenerate the entire HTML response.

Anyway, while Rails is certainly a much better solution to arrive at the final answer, I’ve learned a great deal about Ruby through this exercise. I doubt I’ll make the site much more complex than that.

The Actual Boston Subway Map

Tuesday, August 3rd, 2010

Being a son of a seafarer, I developed a kind of fascination with being on the sea, and with maps. It is because of the latter (and because I happened to live in Boston, and because I didn’t quite like how MBTA imitated Harry Beck, and because I always wanted to know how far it actually between the different subway stops) that in 2005 I decided to make an actual Boston subway map, that is, a geographically-accurate map of all subway stops.

It was several years ago — I believe MBTA may have added a few subway stops since then, and you can also see all these stops on Google Maps, but there’s something elegant in the simplicity of my diagram. It’s also a good case study of Google Maps, scripting and LaTeX.

The idea was to find all the subway stops on a map downloaded from Google Maps using the locations of the stops as reported by MBTA (as you can imagine, it was a humongous pain to click on every single station map to figure out where to actually plot each station), and put the coordinates of each station in a LaTeX file that would generate the pdf image of the subway map. I used pstricks, which is a great LaTeX package for drawing graphics.

The following tcsh script downloads the relevant quadrants from Google Maps and creates an HTML file that displays all the quadrants on one large page. The URL format for the quadrants has changed since 2005 but you get the idea:

get.tcsh

Then I opened the large map in Photoshop and figured out the coordinates of each subway station and turned them into a LaTeX file:

mbta.txt and mbta.tex

Finally, I ran LaTeX to generate the following pdf file (click on the file to download the pdf):

The actual Boston T Map

A New Punctuation Mark

Monday, January 18th, 2010

One of my miniprojects was to come up with a genuine new punctuation mark (although not necessarily a new glyph). And the other while I was texting my friend, I had an idea. It’s not the most original of all marks but I liked the moment of epiphany. I also did a brief search and didn’t find this mark defined already (even with a different definition than mine), which was actually surprising.

That mark is a twoperiod. It consists of two periods spaced out about one-third em from each other. It’s meant to convey a confirmation of another speaker’s statement but with an undertone of deeper consequence that brings about a feeling in the speaker using it. The feeling can be negative, such as guilt or sadness, or a positive, such as pride. Often it’s also used to convey a shared context that is not appropriate to bring up (or does not need to be brought up). For example, the following dialogue makes the use of a twoperiod appropriate:

A: What are you doing tonight?
B: Going gambling.
A: Wow. Two nights in a row?
B: I have a problem.
A: I know..

Speaker A uses a twoperiod because he or she not only has additional information about speaker B, but also that information is in some way embarrassing or funny. Perhaps speaker B borrowed a lot of money from speaker A and never paid it back. Or speaker A actually has the same problem.

Note that a different punctuation mark would not quite be appropriate here: a period would convey the information but not the subtext. An ellipsis would normally indicate that speaker A is disappointed (one can visualize speaker A sighing when he/she says “I know…”). Instead, speaker A simply refers to some other context and makes a statement “between the lines” at the same time.

A twoperiod may be accompanied with a slightly rising pitch of the syllable immediate preceding it (as opposed to the ellipsis that would normally mean a lower pitch for the last syllable or the last word).

Making ginger beer

Wednesday, December 16th, 2009

I decided to brew ginger beer the other weekend. For one, it’s an easy way to debut in brewing (ginger beer has a negligible amount of alcohol — 0.5% — which makes it easier to brew) and establishing one’s presence in the kitchen in general. Ginger beer also happens to be one of my favorite drinks (if you think you’ve tried ginger beer before, try Fentimans Ginger Beer and prepare to be gobsmacked).

I found very helpful recipes online and adapted them slightly to make the beer taste a little more like Fentiman’s: ensure a distinctive “kick” of ginger with hints of juniper. I did two batches — I wanted to try the first, then improve my process for the second one. The first batch took forever and the beer was too lemonade-y and not ginger-y enough. I improve my recipe and the second batch turned out to be a success.

The Recipe for 5 bottles (16 oz each)

  • 10 cups of water — I used Poland Spring
  • 1/2 cup of ginger juice
  • 4/3 cup of cane sugar
  • 2 tbsp of lemon juice
  • 1 tsp of yeast — I used Fleischmanns Instant baking yeast
  • A pinch of cream of tartar
  • A couple of handfuls of juniper berries

I bought some fresh ginger root, peeled it and juiced it with a garlic press, chunk by chunk (each chunk was big enough to fit in the press).

I brought the water to a boil, added the ginger juice, the sugar, the lemon juice, cream of tartar and the juniper berries. I let the water boil for 10 minutes.

At the same time I reconstructed the yeast — I put it in a cup of lukewarm water with a teaspoon of sugar and let sit for 10 minutes.

I let the compote cool down, added yeast and poured into a large bottle (I used two large orange juice bottles). I covered the bottles with cloth and let sit for 16 hours, undisturbed in a dark, warm place.

Finally I poured the mixture into bottles (I used those flip-top bottles, they are great), sealed the bottles and let them sit in a dark, warm place for 36 hours.

I was very pleased with the result — the beer had the kick I was looking for, and the lemon wasn’t overpowering. Mind you, I was probably biased since I wanted the beer to work out, but overall it was a fun experiment. I think what would make it even better would be to use actual ginger beer plant.

I encourage everyone to try and do something in the kitchen. Brewing is an interesting offshoot of cooking — it teaches patience; but it’s still relatively easy to make. Even a poorly brewed ginger beer will no doubt taste excellent in a Dark and Stormy…