Thread: Shapes in PPM

  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Rep Power

    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.

    Thanks guys!
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Mobile, Alabama
    Rep Power
    If you google for Bresenham's Algorithm, you may find what you need.
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    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.
    array[row][col] = radius == sqrt(pow(x-x0,2)+pow(y-y0,2))
    This, of course, won't plot any points whatsoever for any but contrived examples. Instead, compute points within an annular region.

        /* 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]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo