Feb 01 2007

Mandelbrot Set in VB.NET

I finally downloaded the Express edition of Microsoft Visual Basic .NET, and started a long-overdue peek into the much-hyped .NET technology. Up till now, most of my code has been plain C++ with straight Windows API calls. I generally stay away from languages that remove the programmer from the underlying architecture. But I must say that programming .NET has been nothing but a pleasure so far, despite the cons of .NET programming, of which there’s also no shortage.

I was actually pretty surprised that the Express editions of all .NET languages (VB, C++, C#, and J#) are freely downloadable and, for all intents and purposes, fully functional.

My first stab at programming VB.NET is a simple Mandelbrot Set viewer:

Download the program! | Browse source code repository

Mandelbrot Set

The total time to get this program written was about 25 minutes, which says a lot about the possibilities for tremendous productivity using .NET, although this application hardly scratches the surface of .NET functionality…


Jan 24 2007

Hyperbolic Tessellations

A tessellation refers to a uniform tiling of a plane with polygons, such that an equal number of identical polygons meet at each vertex. For example, the tiles in a bathroom, the squares of linoleum on an office floor, or the honeycomb pattern in a bees’ nest are all tessellations of the Euclidean plane.

Hyperbolic TessellationHowever, tessellations are also possible on non-Euclidean spaces, such as the elliptic plane (like the stitching pattern on a soccer ball), and the hyperbolic plane (like… nothing you’d find around the house). In fact, the Euclidean plane has only three regular tessellations (with squares, hexagons, and triangles), while the hyperbolic plane can be tessellated in infinitely many ways.

Since we do not exist in hyperbolic space, we cannot truly “see” hyperbolic tessellations. We can only “represent” them in Euclidean form. A common way of doing this is on the Poincaré disk, which is a finite circle that represents the boundary of the (infinite) hyperbolic plane that is contained inside. The image on the right is a hyperbolic tessellation drawn on the Poincaré disk.

Since tessellations of the hyperbolic plane are especially interesting and mesmerizing to look at, I wrote a small program that generates them, with a great deal of configurable options.

Download the program! | Browse source code repository

Tessellation Application

Using the Program

The program allows you to create an unlimited number of tessellations by selecting “File -> New” from the menu.

When viewing a tessellation, click-and-drag inside of it to shift its position within hyperbolic space. You can also click-and drag with the right mouse button to manipulate the truncation of the tessellation.

Tessellation ControlsThe “Tessellation Controls” window allows you to change the settings for the tessellation that is currently active.

  • p and q — The numbers specified by p and q refer to the Schläfli symbol {p,q} of the tessellation. The Schläfli symbol is a simple way of classifying tessellations where p is the number of sides in each polygon, and q is the number of polygons that meet at each vertex.
  • Max. Vertices — This specifies the number of vertices that will be drawn (how far the tessellation will extend towards the disk boundary). More vertices will take exponentially longer to draw. Also, with more vertices, clicking-and-dragging the tessellation will become slower. With a good screen resolution, 10000 vertices fills up the Poincaré disk almost completely.
  • Model — Select “Poincaré” to draw the tessellation on a Poincaré-style disk, and “Klein” to draw on a Klein-style disk. The Klein disk is similar to the Poincaré disk, except the Klein disk transforms hyperbolic space so that a line between two points appears as a straight line, instead of a circle arc, which is what the Poincaré disk gives.
  • Quality — Select “Low” to display simple straight lines between vertices (and let the tessellation be drawn much faster). Select “High” to draw actual curved lines between vertices. This will slow down drawing considerably.
  • Truncation — This is a list of predefined levels of truncation for the tessellation. Select from this list to apply a certain truncation. You can also do free-form truncation by clicking-and-dragging on the tessellation with the right mouse button.
  • Colors — This allows you to select different colors for each of the components of the tessellation, and to enable or disable drawing of each component.
  • Driver — This selects what functions the program will use to draw the tessellation. Select “OpenGL” to use OpenGL technology, or “Windows GDI” to use plain Windows functions. In most cases, selecting OpenGL will enable the images to be drawn considerably faster, especially with Antialiasing enabled. However, OpenGL is not supported on some (very) old graphics cards. Also, if you create multiple tessellations, only one can be drawn with OpenGL at any given time.
  • Antialias lines — Check this box to draw “smooth” lines.
  • Line Thickness — This specifies the thickness (in pixels) of the lines that make up the tessellation.
  • Advanced — These options are mostly experimental and will not be discussed here.

Gallery

Here’s a brief collection of images created using this program. Click on an image to view a larger version.

{7,3} tessellations, with various truncation:
none, (0,1,0), (0,.5,.5), runcinated, omnitruncated, and snub.

No Truncation(0,1,0) Truncation(0,.5,.5) Truncationruncinatedomnitruncatedsnub

Links

  • This program borrows a substantial amount of code from Don Hatch’s page, which has an exhaustive gallery of {p,q} permutations and truncations, as well as a tessellation Java applet.
  • David E. Joyce’s tessellation page at Clark University.

Jan 05 2007

Ulam’s Prime Number Spiral

There is an infinite number of prime numbers, and yet the prime numbers themselves do not display any apparent pattern, nor does any formula exist that generates prime numbers. In fact, Legendre proved that there cannot be an algebraic function which always gives primes.

Prime SpiralHowever, prime numbers do exhibit a curious phenomenon when arranged in a spiral along with other consecutive integers, as in the figure to the right (in the figure, prime numbers are highlighted in white, twin primes are green, and Mersenne primes are red).

The Phenomenon

It was first noticed by the physicist Stanisław Ulam in 1963, when he got bored in a meeting and started doodling spirals of numbers. He noticed that, if he makes a spiral of consecutive integers, and circles only the prime numbers, strange diagonal “lines” of prime numbers emerge.

This is quite surprising, since we would intuitively expect a random distribution of prime numbers. However, these diagonal segments occur on an impressively large scale, and arbitrarily far from the center of the spiral. The following image is a spiral containing about 4000 primes, and next to it is the same image with some of the diagonal paths highlighted.
Prime Spiral

Application

Prime Spiral Application
To explore this phenomenon on a large scale, I wrote a small program that generates arbitrarily large spirals, with configurable coloring and other options.

Download the program! | Browse the source code repository

The program generates a spiral based on the total number of integers that you specify. It also allows you to specify the colors to use for the background, prime numbers, twin primes, and Mersenne primes.

In addition, the program allows you to use a custom polynomial (up to degree-2) for generating the spiral. By default, the polynomial is set to

f(n) = n

so the spiral will have the normal sequence 0, 1, 2, 3, etc. However, as an example, suppose you want the spiral to consist only of odd integers (1, 3, 5, 7, etc). You can simply set the polynomial to be

f(n) = 2n + 1

Odd Prime Spiral
by writing “2″ in the text box next to “n“, and “1″ in the last of the three text boxes. The image to the right shows a spiral constructed from odd integers only. Notice the prominent “patterns,” this time extending vertically and horizontally.

Conclusions

Ultimately, all that these patterns show is that certain polynomials are more “likely” to generate prime numbers than others. In fact, we can speculate that these kinds of patterns would emerge if we arrange the integers in any ordered design, not just a spiral. Even if we arrange the integers in a simple table, we would still see occasional “streaks” of prime numbers similar to the ones seen in the spiral.

The existence of “prime-generating” polynomials was known since the time of Euler, who discovered a polynomial that gives 40 consecutive prime numbers, namely

f(n) = n2 - n + 41

The reason why some polynomials generate more primes than others is still not known.

Extreme Spirals

Using my program, you can generate extremely large spirals, limited only by the amount of memory on your computer. Here are some fairly large ones that I generated:

Links


Oct 06 2005

Upgrade Your Mouse Cursor, Free!

Filed under: Windows Programming

For a while now I’ve been unhappy with my mouse cursor; specifically, the cursor that appears when you’re hovering over a link on a web page, like this. So I went ahead and created my own. It looks almost the same as the original, but with one subtle difference. Want the cursor for yourself? Download it, copy it to your Windows\Cursors directory, and select it from the Mouse Properties in your Control Panel.


Jul 29 2000

Clean Up Your Damn System Tray!

Filed under: Windows Programming

In the Windows operating system, the “System Tray” is the collection of small icons that appears on the far right side of the taskbar. These icons generally represent programs that are currently running in the background. An icon in the system tray could be used to inform the user that a certain program is running, or offer the user a quick way to change certain settings. I have found that the number of icons visible in a certain computer’s system tray gives a surprisingly accurate measurement of the well-being of the computer, as well as the degree of negligence and/or inexperience of the computer’s owner.

Theorem 1: The well-being of the computer is inversely proportional to the number of visible system tray icons.

Let’s now have a look at some of the programs that actually put their icon in the system tray. Some of these programs may very well prove to be useful. However, the vast majority of them are utterly useless, and sometimes even malicious.
The following is a screen shot of an actual computer (running Windows 98) from a user who has generously contributed to this study. Let’s examine some of the icons seen here, and comment on the worst offenders in this category:

  • McAfee Virus Scan — Okay, this one may be useful to a certain extent. However, my feelings toward antivirus programs deserve an article of their own, so I’ll leave this alone for now.
  • ATI Display Settings — honestly, how many times a day do you find yourself changing your display resolution or color depth? I suppose this may have been useful in the old days when certain programs (especially games) required a certain color depth, but we’re well beyond those constraints now.
  • SETI-at-home — While a noble and conceptually fascinating project, the SETI-at-home effort has produced absolutely no results and, so far, has been a total waste of resources. SETI-at-home was one of the first large-scale distributed computing projects, where CPU time of any available computer in the world could be contributed toward solving a single problem. However, now there are many other distributed computing projects that serve much more important purposes, like modeling protein folding in order to find possible cures for diseases.
  • MSN Messenger — An instant messaging application that is quite unpopular (compared to AIM and Yahoo Messenger). And yet Windows launches it by default even if you don’t have an MSN account, and it takes considerable effort to disable it completely. Notice that this user also has both Yahoo Messenger and AIM running alongside. It should not be necessary to run more than one instant messaging client. If it is absolutely necessary to use both (if you have friends on the different networks who simply refuse to switch), then download a program that supports both protocols simultaneously, like Gaim or Trillian.
  • Adaptec DirectCD — This allows you to drag and drop files onto a rewritable disc and burn a disc with the click of a button. However, Windows XP now allows you to do this natively.
  • WeatherBug — This program runs in the background and shows you the local temperature, absolutely free! Oh, and, every few minutes or so, it will pop up an advertisement that you didn’t ask for, also absolutely free! And good luck uninstalling it!
  • Bonzi Buddy — The mildly amusing, but increasingly annoying virtual monkey-companion who can read your e-mail aloud, among a multitude of other activities. Of course, every once in a while, he’ll kindly present you with an unsolicited online advertisement or a very special offer. He also tracks your internet activity, and then customizes the advertisements he displays based on your browsing habits! Sign me up!
  • WinAmp Agent — WinAmp is an extremely useful and simple tool for playing music files. But is it really necessary to have a portion of it running in the background at all times? When you want to play a song, launch Winamp. When you’re done, close it. Who cares if Winamp takes a few milliseconds longer to load if the Agent isn’t running? It’s better than having the Agent continually consume system resources.
  • Mouse Properties — for God’s sake, how often do you need to alter your mouse properties? Sure, it’s cute to change around your mouse cursor so that it looks like a little dinosaur or a piece of cheese being eaten by a mouse. But it gets old rather quickly.

Jul 13 2000

Serial Mouse “Driver” / Tester

Filed under: Windows Programming

If you have a Microsoft-compatible serial mouse lying around and want to play around with it or test whether it works, this program I wrote in Borland C++Builder opens the serial port and actually reads information from the mouse. You can track the mouse’s position, and even patch the serial mouse into the Windows mouse driver (sort of). Give it a try. If you want, here’s the source code. There’s also a version for Visual Basic.