Skip to content

Instantly share code, notes, and snippets.

@unilecs
Last active January 26, 2018 05:32
Show Gist options
  • Save unilecs/e2844c1028917838760f5a7a0ae06085 to your computer and use it in GitHub Desktop.
Save unilecs/e2844c1028917838760f5a7a0ae06085 to your computer and use it in GitHub Desktop.
Задача 65: Площадь многоугольника
using System;
public class Program
{
public static double GetSquareOfPolygon(int[] X, int[] Y)
{
if (X.Length < 1 && Y.Length < 1 && X.Length != Y.Length)
{
throw new Exception("Input data is not correct");
}
int length = X.Length;
int[] newX = new int[length + 1];
int[] newY = new int[length + 1];
Array.Copy(X, newX, length);
Array.Copy(Y, newY, length);
newX[X.Length] = newX[0];
newY[Y.Length] = newY[0];
// Площадь многоугольника по формуле трапеций
double S = 0;
for (int i = 0; i < length; i++)
{
S += Math.Abs((newY[i + 1] + newY[i]) * (newX[i + 1] - newX[i])) / 2.0;
}
return S;
}
public static void Main()
{
Console.WriteLine("UniLecs");
int[] X = new int[] { 0, 0, 2 };
int[] Y = new int[] { 0, 2, 0 };
Console.WriteLine(string.Format("Answer = {0}", GetSquareOfPolygon(X, Y)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment