Added simple algorithm
This commit is contained in:
23
TMI-practicum/Circle.cs
Normal file
23
TMI-practicum/Circle.cs
Normal file
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
TMI-practicum/Intersection.cs
Normal file
14
TMI-practicum/Intersection.cs
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
@@ -20,10 +21,33 @@ namespace TMI_practicum
|
|||||||
Console.WriteLine("File {0} not found.", args[0]);
|
Console.WriteLine("File {0} not found.", args[0]);
|
||||||
Environment.Exit(1);
|
Environment.Exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var parsed = ParseFile(args[0]);
|
||||||
|
|
||||||
|
IEnumerable<Intersection> 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<byte, int, ICollection<Circle>> ParseFile(string path)
|
private static Tuple<byte, int, IList<Circle>> ParseFile(string path)
|
||||||
{
|
{
|
||||||
|
|
||||||
byte algoritm = 0;
|
byte algoritm = 0;
|
||||||
@@ -52,10 +76,10 @@ namespace TMI_practicum
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Tuple<byte, int, ICollection<Circle>>(algoritm, nbCircles, circles);
|
return new Tuple<byte, int, IList<Circle>>(algoritm, nbCircles, circles);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly string OutputFile = "output.txt";
|
private const string OutputFile = "output.txt";
|
||||||
|
|
||||||
private static void WriteOutput(IEnumerable<Intersection> intersections, double time)
|
private static void WriteOutput(IEnumerable<Intersection> intersections, double time)
|
||||||
{
|
{
|
||||||
@@ -70,34 +94,9 @@ namespace TMI_practicum
|
|||||||
{
|
{
|
||||||
sw.WriteLine("{0}\t{1}", intersection.X, intersection.Y);
|
sw.WriteLine("{0}\t{1}", intersection.X, intersection.Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
sw.WriteLine("\r\n{0}", time);
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
49
TMI-practicum/SimpleAlgorithm.cs
Normal file
49
TMI-practicum/SimpleAlgorithm.cs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace TMI_practicum
|
||||||
|
{
|
||||||
|
public static class SimpleAlgorithm
|
||||||
|
{
|
||||||
|
public static IEnumerable<Intersection> Solve(IList<Circle> circles)
|
||||||
|
{
|
||||||
|
var intersections = new Stack<Intersection>();
|
||||||
|
|
||||||
|
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<Intersection> 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -40,8 +40,11 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="Circle.cs" />
|
||||||
|
<Compile Include="Intersection.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="SimpleAlgorithm.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
|
Reference in New Issue
Block a user