static void Main(string[] args) { Point[] a = new Point[] { new Point() { X = 0, Y = 3 }, new Point() { X = 1, Y = 3 }, new Point() { X = 2, Y = 1 }, new Point() { X = 3, Y = 1 }, new Point() { X = 1, Y = 2 } }; var max = a.Max(p1 => p1.Y); var min = a.Min(p1 => p1.Y); var maxY = a.Where(p => p.Y == max).ToArray(); var minY = a.Where(p => p.Y == min).ToArray(); }
public static IEnumerable <OwnedLocation> AssignOwners(Map map) { var visited = new HashSet <Point>(); var queue = new Queue <OwnedLocation>(); for (int i = 0; i < map.Players.Length; i++) { visited.Add(map.Players[i]); queue.Enqueue(new OwnedLocation(i, map.Players[i], 0)); } while (queue.Count != 0) { var node = queue.Dequeue(); yield return(node); var incidentNodes = new Point[] { new Point(node.Location.X, node.Location.Y + 1), new Point(node.Location.X, node.Location.Y - 1), new Point(node.Location.X + 1, node.Location.Y), new Point(node.Location.X - 1, node.Location.Y) }.Where(point => map.InBounds(point) && map.Maze[point.X, point.Y] != MapCell.Wall); foreach (var nextNode in incidentNodes.Where(incidentNode => !visited.Contains(incidentNode))) { visited.Add(nextNode); queue.Enqueue(new OwnedLocation(node.Owner, nextNode, node.Distance + 1)); } } }
static void Main(string[] args) { Point[] arr = new Point[15].Select(x => x = new Point()).ToArray(); Console.WriteLine("Points:\n" + string.Join("\n", arr.Select(x => x.ToString()))); Console.WriteLine("\nPoints in first octant:\n" + string.Join("\n", arr.Where(x => x.InFirstOctant()))); Console.ReadLine(); }
private static IEnumerable <Point> Expand(Point P, int width, int height) { IEnumerable <Point> Points = new Point[] { new Point(P.X - 1, P.Y), new Point(P.X + 1, P.Y), new Point(P.X, P.Y - 1), new Point(P.X, P.Y + 1), }; return(Points.Where(p => p.X >= 0 && p.Y >= 0 && p.X < width && p.Y < height)); }
public static IEnumerable<Point> GetNeighbors(Point point, Func<Point, bool> isPassable) { var neighbors = new Point[] { new Point(point.x - 1, point.y), new Point(point.x + 1, point.y), new Point(point.x, point.y - 1), new Point(point.x, point.y + 1) }; return neighbors.Where(n => n.IsOnBoard() && isPassable(n)); }
private Line AddDiagonalDecreasingLine(Point pt) { var h = GridsCanvas.ActualHeight; var w = GridsCanvas.ActualWidth; var x0 = pt.X; var y0 = pt.Y; var pts = new Point[] { new Point(0, x0 + y0), new Point(x0 + y0, 0), new Point(w, -w + x0 + y0), new Point(-h + x0 + y0, h), }; var e = 1e-6; var cnt = new Func <Point, bool>(pnt => pnt.X > -e && pnt.X <w + e && pnt.Y> -e && pnt.Y < h + e); var ptss = pts.Where(i => cnt(i)).ToList(); if (ptss.Count < 2) { return(null); } var p1 = ptss[0]; var p2 = ptss[1]; var ln = new Line() { Stroke = Brushes.Black }; ln.X1 = p1.X; ln.X2 = p2.X; ln.Y1 = NormalizeY(p1.Y); ln.Y2 = NormalizeY(p2.Y); GridsCanvas.Children.Add(ln); return(ln); }
public MazeGenerationResults Generate() { var results = new MazeGenerationResults(); var targets = new Dictionary <Point, bool>(); var offsets = new Point(new int[2]).GetAllOffsets(); foreach (var cell in InnerMap.Cells) { var cellNeighbors = offsets.Select(x => x + cell.Key); foreach (var cellNeighbor in cellNeighbors) { var neighborIsAlive = Map.CellExists(cellNeighbor); targets[cellNeighbor] = neighborIsAlive; } targets[cell.Key] = true; } foreach (var target in targets) { var targetNeighbours = offsets.Select(x => x + target.Key); var aliveTargetNeighbours = offsets.Where(x => Map.CellExists(x)); var aliveNeighbourCount = aliveTargetNeighbours.Count(); if (target.Value) // Alive { if (DieIf.Contains(aliveNeighbourCount)) { InnerMap.RemoveCell(target.Key); results.Results.Add(new MazeGenerationResult(target.Key, CellState.Empty, CellDisplayState.Path)); } } else // Dead { if (BornIf.Contains(aliveNeighbourCount)) { InnerMap.SetCell(new Cell(CellState.Filled, CellDisplayState.Wall), target.Key); results.Results.Add(new MazeGenerationResult(target.Key, CellState.Filled, CellDisplayState.Wall)); } } } return(results); }
private IEnumerable<Shape> _addSeriesAsPolyline(Point[] points, Brush color, double storkeThickness, bool animate = true) { if (points.Length < 2) return Enumerable.Empty<Shape>(); var addedFigures = new List<Shape>(); var l = 0d; for (var i = 1; i < points.Length; i++) { var p1 = points[i - 1]; var p2 = points[i]; l += Math.Sqrt( Math.Pow(Math.Abs(p1.X - p2.X), 2) + Math.Pow(Math.Abs(p1.Y - p2.Y), 2) ); } var f = points.First(); var p = points.Where(x => x != f); var g = new PathGeometry { Figures = new PathFigureCollection(new List<PathFigure> { new PathFigure { StartPoint = f, Segments = new PathSegmentCollection( p.Select(x => new LineSegment {Point = new Point(x.X, x.Y)})) } }) }; var path = new Path { Stroke = color, StrokeThickness = storkeThickness, Data = g, StrokeEndLineCap = PenLineCap.Round, StrokeStartLineCap = PenLineCap.Round, StrokeDashOffset = l, StrokeDashArray = new DoubleCollection { l, l }, ClipToBounds = true }; var sp = points.ToList(); sp.Add(new Point(points.Max(x => x.X), ToPlotArea(Chart.Min.Y, AxisTags.Y))); Chart.Canvas.Children.Add(path); addedFigures.Add(path); var draw = new DoubleAnimationUsingKeyFrames { BeginTime = TimeSpan.FromSeconds(0), KeyFrames = new DoubleKeyFrameCollection { new SplineDoubleKeyFrame { KeyTime = TimeSpan.FromMilliseconds(1), Value = l }, new SplineDoubleKeyFrame { KeyTime = TimeSpan.FromMilliseconds(750), Value = 0 } } }; Storyboard.SetTarget(draw, path); Storyboard.SetTargetProperty(draw, new PropertyPath(Shape.StrokeDashOffsetProperty)); var sbDraw = new Storyboard(); sbDraw.Children.Add(draw); var animated = false; if (!Chart.DisableAnimation) { if (animate) { sbDraw.Begin(); animated = true; } } if (!animated) path.StrokeDashOffset = 0; return addedFigures; }
private bool SetBuilding(Vector3 ray, BuildingType type) { //проверяем блоки по спирали от центра int startX, startY; GetPosition(ray, out startX, out startY); var x = startX; var y = startY; // задаем границы движения var minX = x; var maxX = x; // влево вправо var minY = y; var maxY = y; // вверх вниз var direction = SpiralDirection.Up; // сначала пойдем вверх var n = (int)type; var points = new Point[n * n]; for (var i = 0; i < n * n; i++) { var result = _cellsController.CheckFree(x, y); if (result == null || !result.Value) { foreach (var point in points.Where(s => s != null)) { _cellsController.SetFree(point.X, point.Y); } return false; } points[i] = new Point {X = x, Y = y}; _cellsController.SetBusy(x, y); FillSpiralStep(ref x, ref y, ref direction, ref minX, ref minY, ref maxX, ref maxY); } var position = type == BuildingType.Size2X2 ? new Vector3(startX + 0.5f, 0.5f, startY - 0.5f) : new Vector3(startX, 0.5f, startY); var buildingObject = (GameObject)Instantiate(Buildings[(int)type - 1], position, Quaternion.identity); var building = buildingObject.GetComponent<Building>(); building.Points = points; if (CurrentBuildingSize != BuildingType.None) building.SetShadowMode(); _buildings.Add(building); return true; }
public static Point[] ConvexHull(Point[] points) { if (points.Length < 3) { throw new ArgumentException("At least 3 points reqired", "points"); } List<Point> hull = new List<Point>(); // get leftmost point Point vPointOnHull = points.Where(p => p.X == points.Min(min => min.X)).First(); Point vEndpoint; do { hull.Add(vPointOnHull); vEndpoint = points[0]; for (int i = 1; i < points.Length; i++) { if ((vPointOnHull == vEndpoint) || (Orientation(vPointOnHull, vEndpoint, points[i]) == -1)) { vEndpoint = points[i]; } } vPointOnHull = vEndpoint; } while (vEndpoint != hull[0]); return hull.ToArray(); }
Rect getCroppedRectangle(Point [] cachedPoints) { var xMin = cachedPoints.Where (point => point != new Point (-10000, -10000)).Min (point => point.X) - LineWidth / 2; var xMax = cachedPoints.Where (point => point != new Point (-10000, -10000)).Max (point => point.X) + LineWidth / 2; var yMin = cachedPoints.Where (point => point != new Point (-10000, -10000)).Min (point => point.Y) - LineWidth / 2; var yMax = cachedPoints.Where (point => point != new Point (-10000, -10000)).Max (point => point.Y) + LineWidth / 2; xMin = Math.Max (xMin, 0); xMax = Math.Min (xMax, ActualWidth); yMin = Math.Max (yMin, 0); yMax = Math.Min (yMax, ActualHeight); return new Rect (xMin, yMin, xMax - xMin, yMax - yMin); }
private static IEnumerable<Point> ExpandComplete( Point P, int width, int height ) { IEnumerable<Point> Points = new Point[] { new Point( P.X - 1, P.Y + 1), new Point( P.X + 1, P.Y - 1 ), new Point( P.X - 1, P.Y - 1 ), new Point( P.X + 1, P.Y + 1 ), new Point( P.X - 1, P.Y ), new Point( P.X + 1, P.Y ), new Point( P.X, P.Y - 1 ), new Point( P.X, P.Y + 1 ), }; return Points.Where( p => p.X >= 0 && p.Y >= 0 && p.X < width && p.Y < height ); }