public bool this[Position p] { get { return this[p.X, p.Y]; } set { this[p.X, p.Y] = value; } }
private void DrawMember(Position pos, double nodeW, double nodeH, double padding, Color color, string title) { var poly = new Polygon(); poly.Points.Add(new Point(padding, padding)); poly.Points.Add(new Point(nodeW - padding, padding)); poly.Points.Add(new Point(nodeW - padding, nodeH - padding)); poly.Points.Add(new Point(padding, nodeH - padding)); poly.Points.Add(new Point(padding, padding)); poly.Fill = new SolidColorBrush(color); var xs = pos.X * nodeW + (pos.Y % 2 == 0 ? 0.0 : nodeW / 2); var ys = pos.Y * nodeH; Canvas.SetLeft(poly, xs); Canvas.SetTop(poly, ys); if (title != null) { var titleBlock = new TextBlock(); titleBlock.Text = title; Canvas.SetLeft(titleBlock, xs); Canvas.SetTop(titleBlock, ys); host.Children.Add(titleBlock); } host.Children.Add(poly); }
private static double GetHiddenHoles(Field field) { int result = 0; for (int y = 0; y < field.Height - 1; y++) { for (int x = 0; x < field.Width - 1; x++) { if (field[x, y] && field[x + 1, y]) { var underCell = new Position(x, y).Translate(MoveDirection.SouthEast); if (!field[underCell]) result++; } } } return result; }
public double DistanceTo(Position other) { int dx = (other.X - X); int dy = (other.Y - Y); return Math.Sqrt(dx*dx + dy*dy); }
/*private static Position GetBottomOpenPosition(Field field) { Position min = new Position(-1, field.Height); for (int x = 0; x < field.Width; x++) { int y = 0; // drop down until cells is empty while (y < field.Height && !field[x, y]) { y++; } if (y <= min.Y) { min = new Position(x, y); } } return min; }*/ private static unsafe Position GetBottomOpenPosition(Field field) { var w = field.Width; var h = field.Height; fixed (byte* ptr = field.Cells) { Position min = new Position(-1, h); for (int x = 0; x < w; x++) { int y = 0; // drop down until cells is empty var offset = x + y * w; var v = ptr[offset >> 3] & (1 << (offset & 7)); while (y < h && v != 0) { y++; } if (y <= min.Y) { min = new Position(x, y); } } return min; } }