Tuesday, March 18, 2008

Pixel Experiment in Microsoft Excel

I took a break today to play with Microsoft Excel. I have been learning graphics programming (very rudimentary stuff here because I have never done anything in this area). I wanted learn the mathematics behind it all instead of focusing on a  graphics technology or platform....you know, like what does a circle-drawing algorithm look like in the raw?

I decided to try something kind of silly but fun.  A spreadsheet can be thought of as a Cartesian plane so why not configure an Excel worksheet to act as a very rudimentary pixel display?

The image below is of a worksheet with 256 columns and rows, with most of the other UI stripped away.

image

Now with a simple LOGO-like (or should I say turtle graphics) syntax, I can specify some operations and data:

image

The Action maps to an Enum with Draw, Erase, Pause, Resume, Goto, and End as its members.  Type is for drawing shapes and is mapped to another Enum called Shape with Pixel, Line, Rectangle, Circle, and Brush as its members.  Depending on the Actions and the Type, the Data column contains directions to the graphics engine about the shape to render.

The data above says something like this

  1. Draw a black circle at 65 and 120 with radius 10
  2. Pause for 50 ms
  3. Erase #1
  4. Draw a black circle at 70 and 125 with radius 10
  5. Pause for 50 ms
  6. Erase #4
  7. Draw and erase at various spots to animate the circle until Row # 35
  8. Goto #1 and repeat three times
  9. Draw a red rectangle at 6 and 64 with height and width = 128
  10. End

Okay, so at 256x256 resolution, it is a grainy and jumpy but I wanted to look at the math behind all of this.  Remember, this is in Excel so I am using VBA for all of this.  Here is the code to draw a single pixel in our display:

image

Here is the code to draw our circle (two functions):

image

Unfortunately, my first implementation didn't work at all because Excel wouldn't update the display while this was happening, even with using Good Old DoEvents and setting ScreenUpdating = true.  So, I found this:

image

Fun!  Back to work...

2 comments:

zack said...

http://www.gamasutra.com/view/feature/3563/microsoft_excel_revolutionary_3d_.php

Sorry bud, someone's beat you to it. I hear Microsoft is licensing this for the next version of DirectX. =P

Team Africa said...

Hehe. It was fun for a minute!

Disclaimer

Content on this site is provided "AS IS" with no warranties and confers no rights. Additionally, all content on this site is my own personal opinion and does not represent my employer's view in any way.