Dear friends,

I get a assigment and i very confused with this cases.

How to write a function that computes the area of an arbitrary polygon
– so not necessarily a triangle or rectangle, etc. The input of the function should be a list of points, where each point is given as a tuple (x, y). The output of the function is one number. Use the following algorithm, which comes down to computing a contour integral (see the picture below):
For each pair of consecutive points (p, q) and (s, t), compute the area of the trapezoid with additional corners (p, 0) and (s, 0). This area is the width s–p times the average height (q+t)/2. For the actual area of the trapezoids, we would have to take the absolute value of the result. However, for computing the area of a polygon, it is convenient to retain the sign of the result.

Add all these outcomes together to get the area of the polygon (or its negative). This works because the trapezoids for increasing x-coordinates give a positive contribution to the total (the diagonally lined part of the picture), while the trapezoids for decreasing x-coordinates give a negative contribution to the total (the horizontally dashed area) – assuming all ycoordinates are positive; for negative and mixed y-coordinates the reasoning is a bit more complicated, but it works as well.
If the points of the polygon where specified in clockwise order, the result is positive. For anticlockwise order, the result is the negative of the area. So at the end – and no earlier – you have to take the absolute value. Try your function on simple squares, triangles and rectangles first.

Somebody help me please... :(

Kind regards


What code you got now? What kind of error are you getting and what you did to try to solve it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.