我必须绘制一个蜂窝状图案并识别鼠标移动时的每个单元格(行,列)。
这就是我生成图表的方式。
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
答案 0 :(得分:2)
我也在msdn上发布了这个帖子,Stefan Hoffmann非常友好地发布了如何做到这一点的SSCCE。基本上这个想法是预先计算蜂窝,正如@usr在评论部分所建议的那样。 以下是MSDN帖子的链接。
答案 1 :(得分:0)
快速观察:你是否必须使用圆圈 - 六边形会简化问题吗?