March 10th, 2013, 11:58 AM
Shapes in PPM
Hey guys! I'm currently working on a project where I have to create shapes in a PPM file, obviously using code only. Boxes are easy, but how would I do, say, a circle? A triangle? Another random shape? I really want to put a circle in there.
I know. It's really easy. But I'm still learning. Also, since I'm learning, is there a way in which you could do it uncomplicated? Because if I were to use really crazy functions and stuff, something would look amiss.
March 10th, 2013, 12:16 PM
If you google for Bresenham's Algorithm, you may find what you need.
March 10th, 2013, 04:05 PM
An easy method, but computationally expensive:
associate each point in a 2D array with Cartesian x,y.
You might scale the indexes of H by W array to -1, 1.
Then for each index pair compute the formula value. If the equation is satisfied set the array value to 1.
For a circle assume you've made a linear map from indexes row, col to floating x, y.
This, of course, won't plot any points whatsoever for any but contrived examples. Instead, compute points within an annular region.
array[row][col] = radius == sqrt(pow(x-x0,2)+pow(y-y0,2))
/* a: annular width */
/* d: distance from circle center to this point in the array */
/* r: circle radius */
array[row][col] |= (r-a/2 < d) && (d < r + a/2);
[/code] are essential for python code and Makefiles!