From 0cc73033416ec094c1c6d9f0ba811700c5305df3 Mon Sep 17 00:00:00 2001 From: Sharvari Tatachar Date: Sun, 22 Dec 2024 23:11:53 -0500 Subject: [PATCH] area of polygon in C# --- .../src/area_of_polygon/area_of_polygon.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 code/computational_geometry/src/area_of_polygon/area_of_polygon.cs diff --git a/code/computational_geometry/src/area_of_polygon/area_of_polygon.cs b/code/computational_geometry/src/area_of_polygon/area_of_polygon.cs new file mode 100644 index 0000000000..53ffc8ed4e --- /dev/null +++ b/code/computational_geometry/src/area_of_polygon/area_of_polygon.cs @@ -0,0 +1,38 @@ +using System; +namespace Cosmos +{ + public static class AreaOfPolygon + { + public static double createShape(){ + Console.WriteLine("Enter number of sides of the polygon: "); + int n = int.Parse(Console.ReadLine()); + int [] x = new int[n]; + int [] y = new int[n]; + for(int i =0; i < n; i++){ + Console.WriteLine("Enter x coordinate: "); + x[i] = int.Parse(Console.ReadLine()); + Console.WriteLine("Enter y coordinate: "); + y[i] = int.Parse(Console.ReadLine()); + } + return calculateArea(x, y); + } + + public static double calculateArea(int[] xCoords, int[] yCoords){ + int length = xCoords.Length; + int b; + double area = 0; + for (int i = 0; i < length; i ++){ + b = (i + 1) % length; + area += xCoords[i] * yCoords[b] - xCoords[b] * yCoords[i]; + } + area = Math.Abs(area) / 2.0; + return area; + } + + public static void Main(string[] args) + { + double area = createShape(); + Console.WriteLine($"The area of the polygon is: {area}"); + } + } +} \ No newline at end of file