#include <iostream>
#include <stdlib.h>
#include <cmath>
using namespace std;

struct Point {
	float x;
	float y;
};

const int LIMIT = 10;

struct Polygon {
	int size;
	Point points[LIMIT];
};

Polygon createPolygon(int);
void inputPolygon(Polygon&);
float getPolygonArea(const Polygon&);
float getPolygonPerimeter(const Polygon&);

int main() {

	Polygon p = createPolygon(5);
	inputPolygon(p);
	
	float area = getPolygonArea(p);
	float perimeter = getPolygonPerimeter(p);

	cout << "P = " << perimeter << endl
		 << "S = " << area << endl;

	return 0;
}

Polygon createPolygon(int size) {
	
	if(size > LIMIT) {
		cerr << "Size is too big!";
		exit(1);
	}

	Polygon p = { size };
	return p;
}

void inputPoint(Point &p) {
	cout << "(x, y): ";
	cin >> p.x >> p.y;
}

void inputPolygon(Polygon &p) {
	cout << "Input all the " << p.size << " points of the polygon:" << endl;
	for(int i = 0; i < p.size; ++i) {
		inputPoint(p.points[i]);
	}
}

Point midPoint(const Point *points, int count) {

	Point result = { 0.f, 0.f };
	for(int i = 0; i < count; ++i) {
		result.x += points[i].x;
		result.y += points[i].y;
	}
	
	// Помнете, умножението е по-бързо!
	float coef = 1.f / count;
	result.x *= coef;
	result.y *= coef;

	return result;
}

float pointsDistance(const Point &a, const Point &b) {
	float distx = b.x - a.x;
	float disty = b.y - a.y;
	return sqrtf(distx * distx + disty * disty);
}

float triangleArea(const Point &A, const Point &B, const Point &C) {
	float c = pointsDistance(A, B);
	float b = pointsDistance(A, C);
	float a = pointsDistance(B, C);
	float p = (a + b + c) * .5f;
	return sqrtf(p * (p - a) * (p - b) * (p - c));
}

float getPolygonArea(const Polygon &p) {

	Point center = midPoint(p.points, p.size);
	float area = 0;
	
	for(int i = 0; i < p.size; ++i) {
		area += triangleArea(center, p.points[i], p.points[(i + 1) % p.size]);
	}

	return area;
}

float getPolygonPerimeter(const Polygon &p) {
	
	float perimeter = 0.f;

	for(int i = 0; i < p.size; ++i) {
		perimeter += pointsDistance(p.points[i], p.points[(i + 1) % p.size]);
	}
	
	return perimeter;
}
