Thread: Area of irregular shape

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212

    Area of irregular shape

    I'm writing a space game with customisable spaceship shapes, but I'm a bit overwhelmed by a problem, I want to figure out the mass of an irregular 2d shape (the spaceship) based on its area.

    The shape itself is stored as a sort of linked list

    Point x1, y1 -----> Point x2, y2 ----> Point x3, y3 ----> point x4, y4

    and the last point joins back onto the first point. Every 1 unit (unit being 1 integer distance between points) cubed has a mass of 1.

    so a triangle of, say:
    0, 0
    0, 17
    12, 0

    would have a mass of 17/2 * 12 = 102.
    I know I will have to split the main polygon down into triangles, but using every 3 points will not work because sometimes a line back between points could overlap.

    I hope I make sense, could I please have any advice on how to go about splitting my shape into triangles.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Is your polygon limited to the convex case? If so...

    Do you have them in either counter-clockwise or clockwise order? If so just pick the first point as your starting point. So you have 6 points represented like so:

    Code:
       *
      *  *
      *  *
       *
    Let point 1 be the bottom and start moving in a clockwise motion.

    Triangles:

    123
    134
    145
    156

    It will be a bit more work for concave polygons. Let me know if that's your case.

    Edit:

    Yeah...or Salem's way for the general case.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Banned
    Join Date
    May 2004
    Posts
    129
    also keep in mind that you don't have to have exact area. You can approximate the shape by enclosing it with a box, or a circle, or some other basic geometry shape for which you know how to calculate the area.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    cool, I have it now, thanks for the help

    btw this site gives polygon area source code for any of you searchers:

    http://astronomy.swin.edu.au/~pbourk...etry/polyarea/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Helping out a friend...
    By rhymewithnothin in forum C Programming
    Replies: 21
    Last Post: 09-15-2005, 08:59 PM
  2. Displaying more shapes in the same client Area
    By sajeev_js in forum Windows Programming
    Replies: 1
    Last Post: 06-11-2005, 01:52 PM
  3. Newbie- having trouble with functions
    By bnmwad in forum C Programming
    Replies: 7
    Last Post: 02-22-2005, 04:41 PM
  4. Need help with switch
    By 3kgt in forum C Programming
    Replies: 2
    Last Post: 02-26-2003, 12:43 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM