Task: A polygon (not necessarily convex) is defined on the plane by listing the coordinates of the vertices in the order of traversing its boundary. Determine the area of the polygon.
Wrote code to calculate the area of a convex polygon (yes, with memory allocated on the heap). How can it be altered to calculate the area of a nonconvex polygon and, in general, an algorithm for determining whether the polygon is convex or not? (If there is no way to break the polygon into shapes, please write how to break it and calculate each shape separately)
#include "stdafx.h" #include <iostream> using namespace System; using namespace std; int main() { setlocale(0, ""); int y, x, c, i = 0; cout << "Кол-во углов "; cin >> c; int(*coor)[2]; coor = new int[c][2]; while (i != c) { for (int j = 0; j < 2; j++) { cout << "Введите координаты " << endl; cout << "По X "; cin >> x; cout << "По Y "; cin >> y; coor[i][j] = x; j = 1; coor[i][j] = y; j = 2; } i++; }; i = 0; int t = i + 1; double sum, pl = 0; for (i; t!= c; i++) { sum = ((coor[i][0] + coor[t][0])*(coor[i][1] - coor[t][1])) / 2; pl = pl + sum; t++; } cout << "Площадь " << abs(pl) << endl; system("pause"); delete[]coor; }
This is the same program without arrays. Here everything works correctly. Apparently incorrectly integrated here dynamic array ... Help, please
int n, x, y, x1, x2, y1, y2; double sum = 0; cout << "Введите кол-во углов многоугольника: "; cin >> n; cout << "Введите координаты вершины многоугольника: "; cin >> x >> y; x1 = x; y1 = y; for (int i = 0; i < (n - 1); i++) { cout << "Введите координаты следующей вершины: "; cin >> x2 >> y2; sum = sum + (x1 + x2)*(y2 - y1); x1 = x2; y1 = y2; } sum = sum + (x + x2)*(y - y2); sum = abs(sum) / 2; cout << fixed << setprecision(3) << sum << endl;
using namespace System;
??? - AnTnamespace System
is an element of the C ++ / CLI language / platform. What does this issue C + + / CLI? Which language do you want to use: C ++ or C ++ / CLI? - AnT[c++]
tag to your question, and not[c++-cli]
. Therefore, I see no point in thisusing namespace System;
which only litters the code. - AnT