/// <summary> /// Requests new point from the user then parses it into an int-int tuple /// </summary> /// <param name="fieldState">Used to call ShowFieldState</param> /// <returns>New point parsed into an int-int tuple</returns> public Tuple <int, int> GetNewPoint(PolygonState fieldState) { ShowFieldState(fieldState); // TODO: Find a better way to show that result hasn't been initiated. var result = new Tuple <int, int>(-1, -1); do { int coordinateX; int coordinateY; CultureInfo culture = CultureInfo.InvariantCulture; NumberStyles styles = NumberStyles.AllowLeadingWhite; Console.Clear(); Console.WriteLine($"Enter coordinates for the point. Please use \"{separators[0]}\" as a separator."); var response = Console.ReadLine(); var coordinates = new List <string>(response.Split(separators)); // TODO: Consider what to do if the point is not unique. if (int.TryParse(coordinates[0], styles, culture, out coordinateX) && int.TryParse(coordinates[1], styles, culture, out coordinateY)) { result = new Tuple <int, int>(coordinateX, coordinateY); } } while (result.Item1 < 0 || result.Item2 < 0); return(result); }
/// <summary> /// Gets the current direction of the points on a polygon /// </summary> /// <param name="polygonState">Used to find the current direction based on the points list</param> /// <returns>Current direction of the points on polygon</returns> public DirectionEnum GetCurrentDirection(PolygonState polygonState) { // (x2-x1)(y2+y1) from https://stackoverflow.com/a/1165943 // Sum polygon edges. Positive sum = clockwise, negative sum = counterclockwise int edgesum = 0; for (var point = 0; point < polygonState.Points.Count - 1; point++) { edgesum += (polygonState.Points[point + 1].Item1 - polygonState.Points[point].Item1) * (polygonState.Points[point + 1].Item2 + polygonState.Points[point].Item2); } // here we close the polygon for formula to work edgesum += (polygonState.Points[0].Item1 - polygonState.Points[polygonState.Points.Count - 1].Item1) * (polygonState.Points[0].Item2 + polygonState.Points[polygonState.Points.Count - 1].Item2); if (edgesum > 0) { return(DirectionEnum.Clockwise); } else if (edgesum < 0) { return(DirectionEnum.CounterClockwise); } return(DirectionEnum.None); }
/// <summary> /// Shows algorithm metadata /// </summary> /// <param name="fieldState">Used to get field state metadata</param> private void ShowFieldState(PolygonState fieldState) { Console.Clear(); Console.WriteLine($"Field size is {fieldState.SizeX} by {fieldState.SizeY}."); Console.WriteLine($"Currently entered {fieldState.Points.Count} points."); Console.WriteLine($"Current direction: {fieldState.Direction}.\n"); }
/// <summary> /// Gets next action from user and then returns it as an ActionEnum /// </summary> /// <param name="fieldState">Used to call ShowFieldState</param> /// <returns>Selected user action parsed to an ActionEnum result</returns> public ActionEnum GetNextAction(PolygonState fieldState) { // TODO: Remove magic strings from command letters ShowFieldState(fieldState); Console.WriteLine("To enter new point, press N"); Console.WriteLine("To exit, press Esc"); Console.WriteLine("To restart press R"); ConsoleKeyInfo keyInfo = Console.ReadKey(); switch (keyInfo.Key) { case ConsoleKey.Escape: return(ActionEnum.Exit); case ConsoleKey.N: return(ActionEnum.AddNextPoint); case ConsoleKey.R: return(ActionEnum.Restart); default: return(ActionEnum.Continue); } }
private bool HasNeighbourWithState(IBasicPolygon polygon, PolygonState state) { var neighbours = GetNeighbours(polygon); return neighbours.Where(cn => cn.State == state).Count() > 0; }
public ClockworkEngine() { iOService = new InputOutputService(); algorithmService = new AlgorithmService(); fieldState = new PolygonState(); }