using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
public Point(int x, int y)
public List<object> CircleList;
public List<object> SquareList;
public List<object> PointList;
public FileHandler(string filepath)
CircleList = readCircles();
SquareList = readSquares();
PointList = readPoints();
private List<object> readCircles()
string[] text = File.ReadAllLines(Filepath);
List<object> circles = new List<object>();
foreach (string line in text)
string[] parts = line.Split(' ');
if (parts[0].StartsWith("Circle"))
circles.Add(new Circle(int.Parse(parts[1])));
private List<object> readSquares()
string[] text = File.ReadAllLines(Filepath);
List<object> squares = new List<object>();
foreach (string line in text)
string[] parts = line.Split(' ');
if (parts[0].StartsWith("Square"))
squares.Add(new Square(int.Parse(parts[1])));
private List<object> readPoints()
string[] text = File.ReadAllLines(Filepath);
List<object> points = new List<object>();
foreach (string line in text)
string[] parts = line.Split(' ');
if (parts[0].StartsWith("Point"))
points.Add(new Point(int.Parse(parts[1]), int.Parse(parts[2])));
public double CalculateDistance(Point p1, Point p2)
double distance = Math.Sqrt(Math.Pow(p1.x - p2.x, 2) + Math.Pow(p1.y - p2.y, 2));
public double CalculateCircleArea(Circle c)
double area = Math.PI * Math.Pow(c.x, 2);
public double CalculateSquareArea(Square s)
double area = Math.Pow(s.x, 2);
public double[,] CalculateDistanceMatrix(List<object> points)
double[,] DistMatrix = new double[points.Count(), points.Count()];
foreach (Point i in points)
foreach (Point j in points)
SimpleMath d = new SimpleMath();
DistMatrix[m, n] = this.CalculateDistance(i, j);
public double NearestNeighbor(double[,] DistMatrix, int index)
double distBuffer = DistMatrix[index, 0];
int finaliterator = DistMatrix.GetLength(0) - 1;
if (iterator > finaliterator)
double currentDistance = DistMatrix[index, iterator];
if (currentDistance < distBuffer)
distBuffer = currentDistance;
indexNeighbour = iterator;
static void getIndex(ref double dist, double[,] distMatrix, int index)
double[] seq = new double[distMatrix.GetLength(1)];
for (int i = 0; i < distMatrix.GetLength(1); i++)
seq[i] = distMatrix[index - 1, i];
dist = Array.IndexOf(seq, dist);
public static void Main(string[] args)
FileHandler file = new FileHandler("C:/Users/tamar/OneDrive/Dokumente/C#/Shapes (1).txt");
SimpleMath m = new SimpleMath();
foreach (Circle c in file.CircleList)
sum += m.CalculateCircleArea(c);
Console.WriteLine("Sum of all circle areas: " + sum);
foreach (Square s in file.SquareList)
sum_squares += m.CalculateSquareArea(s);
Console.WriteLine("Sum of all square areas: " + sum_squares);
Console.WriteLine("List of points: ");
foreach (Point p in file.PointList)
Console.WriteLine("{0},{1}", p.x, p.y);
Console.WriteLine("Choose a starting index from 1 to {0}", file.PointList.Count());
string i = Console.ReadLine();
int index = Convert.ToInt32(i);
double[,] distMatrix = m.CalculateDistanceMatrix(file.PointList);
double dist = m.NearestNeighbor(distMatrix, index);
Console.WriteLine("The distance to the nearest neighbor is: ");
getIndex(ref dist, distMatrix, index);
Console.WriteLine("The index of the nearest neighbor is: ");