C++ Program - Area of a Quadrilateral

In summary, this conversation is about a homework assignment for a first C++ programming class. The task is to create a program that takes in four ordered pairs of coordinates that go around a quadrilateral in either clockwise or counterclockwise direction. The program should output the area using the standard formula and then use Heron's formula to find the area by breaking the quadrilateral into two triangles and summing up their areas. The student is having trouble with the bonus question, which asks to determine whether the quadrilateral is concave or convex. They are unsure how to determine whether to add or subtract the areas of the two triangles depending on the shape of the quadrilateral. The student is required to use Heron's formula for the triangles, and
  • #1
exitwound
292
1
(Reminder: This is a 2nd week problem for school. First C++ programming class. very limited input/output knowledge at this point.)

Homework Statement



(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.

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.
 
Physics news on Phys.org
  • #2
exitwound said:
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.

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
 
  • #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.
 
  • #4
(0,0), (2,0), (0,2), (1,1) isn't a simple quadrilateral. That could confuse a program.
 
Last edited:
  • #5
Any four-sided shape is a quadrilateral.
 
  • #6
exitwound said:
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.

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.
 
  • #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.
The grader will test your for these three quadrilaterals, plus some others that are not listed. You are encouraged to test it with others of your own.
(0,2), (0,0), (1.73205, 1), (1.73205, 2)
(-1.8,-1), (0.7,-1), (1.4, 1.4), (-1.1,1.4)
(1,1), (15,6), (17,8), (3,11)
Do not expect or demand parentheses or commas in your input.

All vertices will be supplied in sequence, either in clockwise or anti-clockwise order (no jumping across the middle).

[...]

BONUS Program Feature (5 points)
A convex polygon is one where any line connecting two points in the polygon lies completely within the polygon. All three polygons above are convex.

A concave polygon is one where a line connecting two points within the figure may lie partly outside the figure.

The calculations above can actually be used to determine whether a quadrilateral is concave or convex. Include code and an extra output reliably indicating whether the four input vertices describe a concave or convex polygon.
 
  • #8
exitwound said:
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.

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.
 
  • #9
Psu - cse 121?
 
  • #10
Indeed!
 

FAQ: C++ Program - Area of a Quadrilateral

What is a C++ program to find the area of a quadrilateral?

A C++ program to find the area of a quadrilateral is a computer program written in the C++ programming language that calculates the area of a four-sided shape using the length and width of each side.

What are the steps to write a C++ program to find the area of a quadrilateral?

The steps to write a C++ program to find the area of a quadrilateral are as follows:1. Declare variables for the length and width of each side of the quadrilateral.2. Prompt the user to input the values for each side.3. Calculate the area using the formula A = length * width.4. Print the result to the user.

Can a C++ program find the area of any type of quadrilateral?

Yes, a C++ program can find the area of any type of quadrilateral as long as the length and width of each side are known.

What is the formula used to calculate the area of a quadrilateral in a C++ program?

The formula used to calculate the area of a quadrilateral in a C++ program is A = length * width, where A is the area and length and width are the measurements of each side of the quadrilateral.

What happens if the user inputs negative values for the length and width in a C++ program to find the area of a quadrilateral?

If the user inputs negative values for the length and width in a C++ program to find the area of a quadrilateral, the program will still calculate the area using the absolute values of the inputs. This means that the resulting area will always be a positive value.

Similar threads

Replies
1
Views
1K
Replies
15
Views
7K
Replies
5
Views
2K
Replies
9
Views
3K
Replies
6
Views
2K
Back
Top