using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Windows.Forms;
namespace VstupniTest
{
static class Program
{
private static Control control;
///
/// Polomer kruznice
///
private static readonly int radius = 200;
private static readonly int startX = radius; //Startovni pozice na ose X
private static readonly int startY = 0; //Startovni pozice na ose Y
private static Queue numberOfPoints = new Queue();
private static int[] pointsX, pointsY;
///
/// The main entry point for the application.
///
[STAThread]
static void Main(String[] args)
{
if (args.Length == 0)
{
MessageBox.Show("No input arguments.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
LoadInput(args[0]);
control = new Control(startX, startY); //Vytvoreni objektu pro kresleni
Next(); //spusteni algoritmu pro defaultni hodnotu, nebo pro prvni nactenou hodnotu parametru N.
control.Show(); //Zobrazeni GUI
}
}
///
/// Nacte vstupni soubor a hodnoty vlozi do fronty.
///
public static void LoadInput(String inputFile)
{
String line;
try
{
//Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader(inputFile);
//Read the first line of text
line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//save value to stack
numberOfPoints.Enqueue(int.Parse(line));
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
//MessageBox.Show("Were loading " + numberOfPoints.Count + " numbers.", "Number of points", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception e)
{
MessageBox.Show("Exception: " + e.Message, "Can not read input file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
///
/// Spusteni algoritmu pro hodnotu N.
///
/// Pocet vrcholu N.
public static void Start(int N = 10)
{
//MessageBox.Show("Currently points are " + N, "Number of points", MessageBoxButtons.OK, MessageBoxIcon.Information);
control.Reset(startX, startY);
PointPlace(N);
String text = "";
for (int i = 0; i < N; i++)
{
text += (i + 1) + "["+ pointsX[i] +","+ pointsY[i] +"]\n";
}
//MessageBox.Show(text, "Whera are the points", MessageBoxButtons.OK, MessageBoxIcon.Information);
int celkemHran = (N * (N - 1)) / 2;
if ((N % 2) == 1) //pro licha cisla
{
int maxDelka = (N - 1) / 2;
int aktualniDelka = 0;
int index = 0;
while (celkemHran != 0)
{
aktualniDelka = (aktualniDelka % maxDelka) + 1;
index = (index + aktualniDelka) % N;
//MessageBox.Show("maxDelka: "+maxDelka + "\naktualniDelka: " + aktualniDelka + "\n celkemHran: " + celkemHran + "\n index: " + index, "Whera are the points", MessageBoxButtons.OK);
control.Draw(pointsX[index], pointsY[index]);
celkemHran--;
}
}
else //pro suda cisla
{
int[,] matrix = createMatrix(N);
int maxDelka = N / 2;
int aktualniDelka = maxDelka;
int aktualniIndex = 0, budouciIndex;
while (celkemHran != 0)
{
budouciIndex = (aktualniIndex + aktualniDelka) % N;
if (aktualniDelka == 0) //uz jsem z daneho bodu udelal vsechny presuny
{
int indexPresunu = (aktualniIndex + 1) % N;
if (jeVolny(matrix, N, indexPresunu))
{
control.Move(pointsX[indexPresunu], pointsY[indexPresunu]);
aktualniDelka = maxDelka;
//MessageBox.Show("Move()\nfrom " + aktualniIndex + "to " + indexPresunu, "TEST", MessageBoxButtons.OK, MessageBoxIcon.Information);
aktualniIndex = indexPresunu;
}
else
{
aktualniIndex++;
//MessageBox.Show("Jump", "TEST", MessageBoxButtons.OK, MessageBoxIcon.Information);
continue;
}
}
if (matrix[aktualniIndex, budouciIndex] == 0)
{
control.Draw(pointsX[budouciIndex], pointsY[budouciIndex]);
matrix[aktualniIndex, budouciIndex] = 1;
matrix[budouciIndex, aktualniIndex] = 1;
celkemHran--;
aktualniDelka = maxDelka;
//MessageBox.Show("Draw()\naktualniDelka: " + aktualniDelka + "\nfrom " + aktualniIndex + "to " + budouciIndex, "TEST", MessageBoxButtons.OK, MessageBoxIcon.Information);
aktualniIndex = budouciIndex;
}
else
{
//MessageBox.Show("aktualniDelka--", "TEST", MessageBoxButtons.OK, MessageBoxIcon.Information);
aktualniDelka--;
continue;
}
}
}
control.Refresh(); //Prekresleni okna.
//MessageBox.Show("Draw(): " + control.DrawCount + "\nMove(): " + control.MoveCount, "Count of calling methods", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
///
/// Zjisti, jesli jsme z vrocholu vykreslili již všechny hrany
///
public static bool jeVolny(int[,] matrix, int dimension, int index)
{
for (int i = 0; i < dimension; i++)
{
if(matrix[index,i] == 0)
return true;
}
return false;
}
///
/// Vytvori matici NxN a inicializuje ji na nuly, vyjma hlavni diagonaly.
///
public static int[,] createMatrix(int dimension)
{
int[,] matrix = new int[dimension, dimension];
for (int i = 0; i < dimension; i++)
{
for (int j = 0; j < dimension; j++)
{
if(i == j)
matrix[i, j] = 1;
else
matrix[i, j] = 0;
}
}
return matrix;
}
///
/// Vypocita rovnomerne rozlozeni bodu.
///
public static void PointPlace(int N)
{
pointsX = new int[N];
pointsY = new int[N];
double fi = 2 * Math.PI / N;
double currentFI, x, y;
for (int i = 0; i < N; i++)
{
currentFI = (i+1) * fi;
x = radius * Math.Cos(currentFI);
y = radius * Math.Sin(currentFI);
pointsX[i] = (int)x;
pointsY[i] = (int)y;
//String text = "" + RadianToDegree(actuallyFI) + "° = " + actuallyFI + "\n X = " + x + "(" + pointsX[i] + ")\n Y = " + y + "(" + pointsY[i] + ")";
//MessageBox.Show(text, "Variable Dump", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
///
/// Prevede radiany na stupne.
///
private static double RadianToDegree(double angle)
{
return angle * (180.0 / Math.PI);
}
///
/// Spusti algoritmus pro dalsi hodnotu N.
///
public static void Next()
{
if (numberOfPoints.Count != 0)
{
Start(numberOfPoints.Dequeue());
}
else
{
MessageBox.Show("All points were used.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}