5

我必须绘制一个蜂窝图案并识别 mousemove 上的每个单元格(行、列)。 蜂窝图说明

这就是我生成图表的方式。

protected override void GenerateGridBitmap()
    {
        if (_circleGrid != null)
        {
            _circleGrid.Dispose();
            _circleGrid = null;
        }
        Bitmap _texture = new Bitmap(circleSize, circleSize);
        using (Graphics g = Graphics.FromImage(_texture))
        {
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            Rectangle r = new Rectangle(0, 0, circleSize, circleSize);
            g.DrawEllipse(Pens.Black, r);
        }

        Bitmap rowBlock = new Bitmap(CanvasSize.Width - (circleSize/ 2), circleSize);

        using (Brush b = new TextureBrush(_texture))
        {
            using (Graphics g = Graphics.FromImage(rowBlock))
            {
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.FillRectangle(b, new Rectangle(new Point(0, 0), rowBlock.Size));
            }
        }
        //rowBlock.Save("rowblock.bmp");
        _circleGrid = new Bitmap(CanvasSize.Width, CanvasSize.Height);
        using (Graphics g = Graphics.FromImage(_circleGrid))
        {
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;
            int x, y;
            for (int i = 0; i < rows; i++)
            {
                x = 0;
                if (i % 2 != 0)
                    x = (circleSize/ 2);

                y = (i * circleSize);
                if (i != 0)
                {
                    y -= (VERTICAL_PIXEL_OFFSET * i);
                }
                g.DrawImage(rowBlock, x, y);
                //g.DrawImage(DrawCodedCrystal(i,rowBlock), x, y);
                Console.WriteLine(i);
            }
        }
        _circleGrid.Save("grid.bmp");
        Console.WriteLine(_circleGrid.Size);
        _texture.Dispose();
        _texture = null;
        rowBlock.Dispose();
        rowBlock = null;
    }

这就是我为获取图形坐标所做的工作。但问题是我能够完美地获得专栏。但对于这一行,我认为计算上存在细微差别。例如。在第 99 行,对于 (1/4) 圈,它说 {row 98},对于剩余的圈,它说 {row 99}。偏差随着行数的增加而增加。

protected override CanvasCell GetCanvasCellAt(int x, int y)
    {

        Rectangle rect = GetImageViewPort();
        Point pt = new Point(x, y);

        CanvasCell c = new CanvasCell() { Row = -1, Column = -1 };
        if (rect.Contains(pt))
        {
            double zoomedCircleSize = CircleSize * ZoomFactor;
            Point p = pt;// PointToClient(new Point(x, y));
            p.X -= (int)(rect.X + (AutoScrollPosition.X) );
            p.Y -= (int)(rect.Y + (AutoScrollPosition.Y));

            int row = (int)((p.Y) / (zoomedCircleSize));
            //row = (int)((p.Y + (row * ZoomFactor)) / zoomedCircleSize);
            int col;
            if (row % 2 != 0)
            {
                if (p.X >= 0 && p.X < (zoomedCircleSize / 2))
                {
                    col = -1;
                }
                else
                    col = (int)((p.X - (zoomedCircleSize / 2)) / zoomedCircleSize);
            }
            else
            {
                if (p.X > (zoomedCircleSize * cols))
                {
                    col = -1;
                }
                else
                {
                    col = (int)((p.X) / zoomedCircleSize);

                }
            }
            //if (!GetRectangle(row, col).ContainsWithInBoundingCircle(p))
            //{
            //    c.Column = -1;
            //    c.Row = -1;
            //}
            //else
            {
                c.Column = col;
                c.Row = row;
            }
        }
        //

        return c;
    }

希望我能清楚地解释我的问题。

编辑: VERTICAL_PIXEL_OFFSET 为 1,圆圈大小为 16

4

2 回答 2

2

我还在 msdn 上发布了这篇文章,Stefan Hoffmann 很友好地发布了一个关于如何做到这一点的 SSCCE。正如@usr 在评论部分所建议的那样,基本上这个想法是预先计算蜂窝。这是MSDN帖子的链接。

于 2012-08-10T00:14:31.037 回答
0

快速观察:你必须使用圆圈吗?六边形会简化问题吗?

于 2012-08-10T00:31:32.463 回答