From 88ed2b32a46e3cf3491ff4bc9c865ceb5273451b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Tue, 7 Dec 2021 20:21:25 +0100 Subject: [PATCH] implementing parsing and plotting --- src/Program.cs | 54 +++++++++++++++++++++++++++++++++++++++++++++-- src/kMeans.csproj | 10 +++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/Program.cs b/src/Program.cs index 3751555..d83a303 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,2 +1,52 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using System.Diagnostics; +using System.Drawing; +using System.Globalization; + +namespace kMeans; + +public static class Program +{ + private const string Coordinates = "coordinates.csv"; + private const string PlotOut = "plot.png"; + private const int K = 3; + + private static void Main() + { + Console.WriteLine("k-Means-Algorithmus"); + var points = ParseCsv(Coordinates); + PlotDiagram(points); + } + + private static IReadOnlyList ParseCsv(string path) + { + var sw = Stopwatch.StartNew(); + var result = new List(); + var lines = File.ReadLines(path); + foreach (var line in lines) + { + var current = line.Split(','); + if (!double.TryParse(current[0], NumberStyles.Any, CultureInfo.InvariantCulture, out var x)) continue; + if (!double.TryParse(current[1], NumberStyles.Any, CultureInfo.InvariantCulture, out var y)) continue; + var point = new Point(x, y); + result.Add(point); + } + + sw.Stop(); + Console.WriteLine($"[{nameof(ParseCsv)}] Elapsed: {sw.ElapsedMilliseconds}ms"); + return result; + } + + private static void PlotDiagram(IReadOnlyList points) + { + var sw = Stopwatch.StartNew(); + var plot = new ScottPlot.Plot(); + plot.AddScatterPoints(points.Select(p => p.X).ToArray(), points.Select(p => p.Y).ToArray()); + plot.SaveFig(PlotOut); + sw.Stop(); + Process.Start(new ProcessStartInfo(PlotOut) { UseShellExecute = true }); + Console.WriteLine($"[{nameof(PlotDiagram)}] Elapsed: {sw.ElapsedMilliseconds}ms"); + } + + + private record Point(double X, double Y, Color? Color = default, bool Mean = false); +} \ No newline at end of file diff --git a/src/kMeans.csproj b/src/kMeans.csproj index 74abf5c..0428267 100644 --- a/src/kMeans.csproj +++ b/src/kMeans.csproj @@ -7,4 +7,14 @@ enable + + + + + + + PreserveNewest + + +