Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions code/computational_geometry/src/area_of_polygon/area_of_polygon.cs
Original file line number Diff line number Diff line change
@@ -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}");
}
}
}