Due: Wednesday 2/5 10:00am

Submission name: w02_area

A Little Geometry:

The signed area of a polygon is a measure of how much space is enclosed by the shape. For a polygon with non intersecting sides, this is the same as the area, but it is also a measure that can be made for shapes with sides that intersect.

  • The signed area can be found by iterating over the vertices of the polygon. For each point from 0 to the number of points - 1, take the sum of the following (this is known as the shoelace formula).
    • sum+= \((x_{i} y_{i+1} - x_{i+1}y_{i})\)
    • When i gets to the last point, perform the calculations above replacing \(i + 1\) with 0. That is to say the correct calculation requires the last point to loop back around to the first.
    • At then end, take the cumulative sum and divide it by 2, this is the signed area.

Add Area to our Shapes

First, get the updated PathShapeDriver code from thesource (this includes updates from class today). Then add the following:

  • To PathShape
  • New instance variables:
    • float area: stores the signed area.
  • New method:
    • void setArea()
      • Sets the area instance variable as defined above.
  • Modified method:
    • void makeRandomShape()
      • Call setArea() after the points are made.
      • Set inside to magenta (255, 0, 255) if area is less than shapeSize and cyan (0, 255, 255) is area is greater than shapeSize.
  • Verify that the above changes have been added to Polygon, if not, add code to Polygon where needed.

a02-patharea