Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct Vertex {
- float X, Y;
- };
- void testFirstPolygon();
- void testSecondPolygon();
- void calculateAndPrintArea(const Vertex *polygon, std::size_t vertexCount);
- int main() {
- testFirstPolygon();
- testSecondPolygon();
- }
- void testFirstPolygon() {
- Vertex vertices[] = {
- {-10, 0}, {10, 0}, {10, 10}, {-10, 10}, {-10, 0}, {10, 0}
- };
- std::size_t vertexCount = 4;
- calculateAndPrintArea(vertices, vertexCount);
- }
- void testSecondPolygon() {
- Vertex vertices[] = {
- {90, 0}, {110, 0}, {110, 10}, {90, 10}, {90, 0}, {110, 0}
- };
- std::size_t vertexCount = 4;
- calculateAndPrintArea(vertices, vertexCount);
- }
- void calculateAndPrintArea(const Vertex *polygon, std::size_t vertexCount) {
- float area = 0;
- for(std::size_t index = 0; index < vertexCount; index += 2) {
- area += polygon[index + 1].X * (polygon[index + 2].Y - polygon[index].Y);
- area += polygon[index + 1].Y * (polygon[index].X - polygon[index + 2].X);
- }
- area /= 2;
- std::cout << "Polygon Area (method1): " << area << std::endl;
- // ------------------------------------------------------------------------
- area = 0;
- for(std::size_t index = 1; index < vertexCount; ++index) {
- area += polygon[index].X * (polygon[index + 1].Y - polygon[index - 1].Y);
- }
- area /= 2;
- std::cout << "Polygon Area (method2): " << area << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement