diff --git a/TMI-practicum/Circle.cs b/TMI-practicum/Circle.cs new file mode 100644 index 0000000..5c4091b --- /dev/null +++ b/TMI-practicum/Circle.cs @@ -0,0 +1,23 @@ +using System; + +namespace TMI_practicum +{ + public class Circle + { + public readonly double X; + public readonly double Y; + public readonly double R; + + public Circle(double x, double y, double r) + { + X = x; + Y = y; + R = r; + } + + public double Distance(Circle otherCircle) + { + return Math.Sqrt(Math.Pow(X - otherCircle.X, 2) + Math.Pow(Y - otherCircle.Y, 2)); + } + } +} \ No newline at end of file diff --git a/TMI-practicum/Intersection.cs b/TMI-practicum/Intersection.cs new file mode 100644 index 0000000..43382a6 --- /dev/null +++ b/TMI-practicum/Intersection.cs @@ -0,0 +1,14 @@ +namespace TMI_practicum +{ + public struct Intersection + { + public readonly double X; + public readonly double Y; + + public Intersection(double x, double y) + { + X = x; + Y = y; + } + } +} \ No newline at end of file diff --git a/TMI-practicum/Program.cs b/TMI-practicum/Program.cs index 29da9eb..79df9c6 100644 --- a/TMI-practicum/Program.cs +++ b/TMI-practicum/Program.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; @@ -20,10 +21,33 @@ namespace TMI_practicum Console.WriteLine("File {0} not found.", args[0]); Environment.Exit(1); } + + var parsed = ParseFile(args[0]); + + IEnumerable solved; + + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); + + switch (parsed.Item1) + { + case 1: + solved = SimpleAlgorithm.Solve(parsed.Item3); + break; + case 2: + throw new NotImplementedException(); + case 3: + throw new NotImplementedException(); + default: + throw new ArgumentException("Algorithm with id: " + parsed.Item1 + " is not defined!"); + } + + stopwatch.Stop(); + WriteOutput(solved, stopwatch.ElapsedMilliseconds / 1000.0); } - private static Tuple> ParseFile(string path) + private static Tuple> ParseFile(string path) { byte algoritm = 0; @@ -52,10 +76,10 @@ namespace TMI_practicum } } - return new Tuple>(algoritm, nbCircles, circles); + return new Tuple>(algoritm, nbCircles, circles); } - private static readonly string OutputFile = "output.txt"; + private const string OutputFile = "output.txt"; private static void WriteOutput(IEnumerable intersections, double time) { @@ -70,34 +94,9 @@ namespace TMI_practicum { sw.WriteLine("{0}\t{1}", intersection.X, intersection.Y); } + sw.WriteLine("\r\n{0}", time); } } - - private struct Circle - { - public readonly double X; - public readonly double Y; - public readonly double R; - - public Circle(double x, double y, double r) - { - X = x; - Y = y; - R = r; - } - } - - private struct Intersection - { - public readonly double X; - public readonly double Y; - - public Intersection(double x, double y) - { - X = x; - Y = y; - } - } } } \ No newline at end of file diff --git a/TMI-practicum/SimpleAlgorithm.cs b/TMI-practicum/SimpleAlgorithm.cs new file mode 100644 index 0000000..65ccc4d --- /dev/null +++ b/TMI-practicum/SimpleAlgorithm.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; + +namespace TMI_practicum +{ + public static class SimpleAlgorithm + { + public static IEnumerable Solve(IList circles) + { + var intersections = new Stack(); + + for (int i = 0; i < circles.Count - 1; i++) + { + for (int j = i + 1; j < circles.Count; j++) + { + var c1 = circles[i]; + var c2 = circles[j]; + CalculateIntersection(intersections, c1, c2); + } + } + + return intersections; + } + + private static void CalculateIntersection(Stack intersections, Circle c1, Circle c2) + { + double d = c1.Distance(c2); + if (d > c1.R + c2.R || d < Math.Abs(c1.R - c2.R)) return; + + if (d == 0.0 && c1.R - c2.R == 0.0) throw new ArgumentException("Circles may not be coincident!"); + + double a = (c1.R * c1.R - c2.R * c2.R + d * d) / (2.0*d); + double px = c1.X + a * (c2.X - c1.X) / d; + double py = c1.Y + a * (c2.Y - c1.Y) / d; + + double htemp = c1.R * c1.R - a * a; + if (htemp == 0) + { + intersections.Push(new Intersection(px, py)); + } + else + { + double h = Math.Sqrt(htemp); + intersections.Push(new Intersection(px + h * (c2.Y - c1.Y) / d, py - h * (c2.X - c1.X) / d)); + intersections.Push(new Intersection(px - h * (c2.Y - c1.Y) / d, py + h * (c2.X - c1.X) / d)); + } + } + } +} \ No newline at end of file diff --git a/TMI-practicum/TMI-practicum.csproj b/TMI-practicum/TMI-practicum.csproj index 41368d9..844d10a 100644 --- a/TMI-practicum/TMI-practicum.csproj +++ b/TMI-practicum/TMI-practicum.csproj @@ -40,8 +40,11 @@ + + +