108 lines
3.3 KiB
C#
108 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
namespace TMI_practicum
|
|
{
|
|
internal static class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
|
|
//SweepLineEffAlgorithm.Solve(null);
|
|
if (args.Length < 1)
|
|
{
|
|
Console.WriteLine("Please enter a file.");
|
|
Environment.Exit(1);
|
|
}
|
|
|
|
if (!File.Exists(args[0]))
|
|
{
|
|
Console.WriteLine("File {0} not found.", args[0]);
|
|
Environment.Exit(1);
|
|
}
|
|
|
|
var (algorithm, _, circles) = ParseFile(args[0]);
|
|
|
|
IEnumerable<Intersection> solved;
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
stopwatch.Start();
|
|
|
|
switch (algorithm)
|
|
{
|
|
case 1:
|
|
solved = SimpleAlgorithm.Solve(circles);
|
|
break;
|
|
case 2:
|
|
solved = SweepLineAlgorithm.Solve(circles);
|
|
break;
|
|
case 3:
|
|
solved = SweepLineEffAlgorithm.Solve(circles);
|
|
break;
|
|
default:
|
|
throw new ArgumentException("Algorithm with id: " + algorithm + " is not defined!");
|
|
}
|
|
|
|
stopwatch.Stop();
|
|
WriteOutput(solved, stopwatch.ElapsedMilliseconds);
|
|
}
|
|
|
|
|
|
private static (byte Algorithm, int NbCircles, IList<Circle> Circles) ParseFile(string path)
|
|
{
|
|
|
|
byte algorithm = 0;
|
|
int nbCircles = 0;
|
|
Circle[] circles = null;
|
|
|
|
using (StreamReader file = new StreamReader(path))
|
|
{
|
|
try
|
|
{
|
|
algorithm = byte.Parse(file.ReadLine());
|
|
nbCircles = int.Parse(file.ReadLine());
|
|
circles = new Circle[nbCircles];
|
|
string line;
|
|
int i = 0;
|
|
while ((line = file.ReadLine()) != null)
|
|
{
|
|
double[] parts = line.Split(' ').Select(double.Parse).ToArray();
|
|
circles[i++] = new Circle(parts[0], parts[1], parts[2]);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
Console.WriteLine("An error occured parsing the input file.\r\n" +
|
|
"Make sure the file has all required values and consists only of numbers.");
|
|
Environment.Exit(1);
|
|
}
|
|
}
|
|
|
|
return (algorithm, nbCircles, circles);
|
|
}
|
|
|
|
private const string OutputFile = "output.txt";
|
|
|
|
private static void WriteOutput(IEnumerable<Intersection> intersections, double time)
|
|
{
|
|
if (File.Exists(OutputFile))
|
|
{
|
|
File.Delete(OutputFile);
|
|
}
|
|
|
|
using (StreamWriter sw = File.CreateText(OutputFile))
|
|
{
|
|
foreach (var intersection in intersections)
|
|
{
|
|
sw.WriteLine("{0:0.000000000000000}\t{1:0.000000000000000}", intersection.X, intersection.Y);
|
|
}
|
|
|
|
sw.WriteLine("\r\n{0}", time);
|
|
}
|
|
}
|
|
}
|
|
}
|