However, I think it is fascinating to learn about how mathematicians in the pre-computer age were able to approximate π to many decimal places through cleverness and manual paper-and-pencil calculations. Modern computer methods and algorithms enable us to calculate 100 trillion digits of π. It's a distance of 1.įor pixels that are diagonally adjacent, you can use the Pythagorean theorem of a² b²=c² to calculate the distance between two diagonal pixels as the square root of 2, or approximately 1.414.Happy Pi Day! Every year on March 14th (written 3/14 in the US), people in the mathematical sciences celebrate "all things pi-related" because 3.14 is the three-decimal approximation to In a circle, pixels are always directly adjacent to one another: vertically, horizontally, or diagonally.įor pixels that are vertically or horizontally adjacent, the pixel "distance" is simple. The function doesn't attempt to provide antialiasing by drawing nearby pixels in some intermediate color. You don't need a bunch of fancy math or algorithms here, just the knowledge that the OpenWatcom _ellipse function draws only solid pixels in the color you set for the circle. To update the program, you must write a function that calculates the distance between any two pixels: x0,y0 and x,y. To provide a better estimate of the circumference, you must measure the distance between pixels and use that total measurement for the circumference. Pixels are discrete points in a grid, while a circle is a continuous drawing. The problem with counting pixels to estimate the circumference is that the pixels are only a sample of a circular drawing. Latest articles about sustainability You need to measure the distance between pixels to get pi I tried several video resolutions, and I always got a final result of about 2.8: pixel count (circumference?) = 1356 Because pi is the ratio of the circumference of a circle to its diameter, my pi calculation was noticeably lower than 3.14. Printf("pi = c/d = %f\n", (double) count / 480.0) īut counting pixels to determine the circumference underestimates the actual circumference of the circle. Printf("pixel count (circumference?) = %lu\n", count) At the end, you can use the total pixel count as an estimate of the circumference: #include For any pixel that is color 7 (the color of the circle), add one to the pixel count. With these graphics functions, you can write a program that draws a circle to the screen, then iterates over all the x,y coordinates of the circle to count the pixels. Note that in graphics mode, x and y count from zero, so the upper left corner is always 0,0. If the height and width are the same, the ellipse is a circle. In graphics mode, you can use the _ellipse function to draw an ellipse to the screen, from some starting x,y coordinate in the upper left to a final x,y coordinate in the lower right. In the standard 16 color DOS palette, color 0 is black, color 1 is blue, color 7 is a low intensity white, and color 15 is a high intensity white. The _VRES16COLOR video mode puts the display into 640×480 resolution at 16 colors, a common "classic VGA" screen resolution. I wrote it in FreeDOS because DOS programs can easily enter graphics mode by using the OpenWatcom _setvideomode function. I wrote a FreeDOS program using OpenWatcom C that draws a circle to the screen, then counts the pixels that make up that circle. No matter what resolution I tried, the final pi calculation of circumference divided by diameter was always around 2.8. Estimating the circumference of a circle by counting the pixels required to draw it will give you the wrong result. I didn't expect to get 3.14, but I thought the value would be somewhat close to 3.0. I naively assumed that this would give me an approximation of pi. For Pi Day this year, I wanted to write a program to calculate pi by drawing a circle in FreeDOS graphics mode, then counting pixels to estimate the circumference.
0 Comments
Leave a Reply. |