1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ Program - Area of a Quadrilateral

  1. Sep 8, 2009 #1
    (Reminder: This is a 2nd week problem for school. First C++ programming class. very limited input/output knowledge at this point.)

    1. The problem statement, all variables and given/known data

    (paraphrased) Have user input 4 ordered pairs of coordinates that go around the quadrilateral in either the CW or CCW direction. Output the area according to the standard formula:

    .5 * (abs( (x1y2 - x2y1) + (x2y3 - x3y2) + (x3y4 - x4y3) + (x4y1-x1y4) ) )

    Then, use Heron's formula to find the area. Break the quadrilateral up into two triangles by connecting vertices 1 & 3. Find area of Triangle 1, then Triangle 2, then sum them up.

    Area = sqrt(s(s-a)(s-b)(s-c))
    s= .5*perimeter

    Then, use Heron's formula again to find the area but splitting the triangle through vertices 2 & 4.

    3. The attempt at a solution

    Okay, this is all fine and dandy and I have the program working completely, although I get an unsual error that I can't quite figure out how to fix, and it's only when the bonus question is involved.

    The bonus question asks "what if the quadrilateral is concave? Write reliable code that can determine whether or not the quad is concave or convex."

    And when I input a concave quadrilateral, such as with coordinates: (0,0)(2,0)(0,2)(1,1) I end up with the correct area being computed by the standard formula, but not by Heron's formula. This is because the 2nd triangle that is computed is outside the polygon.

    BASICALLY: If I have a convex quad, all of the area found (by splitting up the quad into two triangles) is inside the polygon. If i have a concave quad, one of the triangles lies outside the polygon.

    I can't figure out how to remedy this. I have a week to get this done so I'm in no rush.
     
  2. jcsd
  3. Sep 8, 2009 #2

    sylas

    User Avatar
    Science Advisor

    This is a maths question rather than a computing question. If you can sort out what is happening mathematically, then you can put it in the program.

    Your formula for the triangle method involves square roots... and when you have square roots, then you might have an issue with plus and minus.

    Sometimes you want to add the two triangles. Sometimes you want to subtract. Can you think of a way to tell when you add and when you subtract?

    Cheers -- sylas
     
  4. Sep 8, 2009 #3
    That's the issue I'm having. I can't visually figure out how to determine whether I should add or subtract the triangles, other than by knowing i already have a convex or concave poly. I also can't figure out mathematically how to determine that either at a glance.
     
  5. Sep 8, 2009 #4

    nvn

    User Avatar
    Science Advisor
    Homework Helper

    (0,0), (2,0), (0,2), (1,1) isn't a simple quadrilateral. That could confuse a program.
     
    Last edited: Sep 8, 2009
  6. Sep 8, 2009 #5
    Any four-sided shape is a quadrilateral.
     
  7. Sep 8, 2009 #6

    sylas

    User Avatar
    Science Advisor

    OK. Let's assume for a start that you don't have the sides of a quadrilateral actually intersecting like this!

    [tex]\setlength{\unitlength}{1mm}\begin{picture}(40,40)
    \put(0,10){\line(0,1){20}}
    \put(0,10){\line(2,1){40}}
    \put(0,30){\line(1,-1){30}}
    \put(30,0){\line(1,3){10}}
    \end{picture}[/tex]​

    It's not enough to know concave/convex to decide whether you add or subtract the triangles. For example, in the first case you subtract, and in the second you add:
    [tex]\setlength{\unitlength}{1mm}\begin{picture}(100,40)
    \put(0,0){\line(0,1){40}}
    \put(0,0){\line(1,1){40}}
    \put(0,40){\line(1,-2){10}}
    \put(10,20){\line(3,2){30}}
    \multiput(0,40)(2,0){20}{\line(1,0){1}}
    \put(60,0){\line(0,1){40}}
    \put(60,0){\line(1,1){40}}
    \put(60,40){\line(1,-2){10}}
    \put(70,20){\line(3,2){30}}
    \multiput(60.5,1)(1,2){10}{\circle{0.4}}
    \end{picture}[/tex]​

    It should be enough to decide whether two points are on the same side, or different sides, of a line through two other points. But there may be better methods still.

    Are you required to use Heron's formula for the triangles? Is that part of the problem statement? Your input seems to be co-ordinates, not lengths.

    Cheers -- sylas

    PS. I have crossed posts with nvn. He's raised the same point I did. Sometimes four co-ordinates don't actually give you a four sided figure, because you might have intersecting sides; that's why I note that I'm assuming we don't have intersecting sides, which is technically not a quadrilateral.
     
  8. Sep 8, 2009 #7
    Heron's formula is a MUST. We use the lengths of the sides (part of our assignment is learning the cmath operators) in the equation Area = sqrt(s(s-a)(s-b)(s-c)) after we split the quad.

    Nice work on the latex code. The homework was stated as this. I believe that, for simplicity, the quads will not cross.
     
  9. Sep 8, 2009 #8

    sylas

    User Avatar
    Science Advisor

    The problem occurs, as the assignment states, when line bisecting the quadrilateral is actually outside the quadrilateral. That's what you should be looking for, I suspect.
     
  10. Sep 18, 2009 #9
    Psu - cse 121?
     
  11. Sep 19, 2009 #10
    Indeed!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++ Program - Area of a Quadrilateral
  1. Program in C (Replies: 1)

  2. C++ Program (Replies: 17)

Loading...