如何计算图纸的中心(多个矩形)

时间:2016-06-17 17:33:44

标签: c# paint picturebox

我有一个C#窗体,我在这里绘制多个矩形:

enter image description here

我在OnPaint的{​​{1}}事件中执行此操作。

  

有没有"容易"如何获得该绘图的中心?

这是我的代码:

picturebox

2 个答案:

答案 0 :(得分:0)

我找到了办法:

  // init left point
            List<Point> boundariesCoords = new List<Point>();
            boundariesCoords.Add(new Point(_leftPadding, _topPadding));


 // final right point
            boundariesCoords.Add(new Point(_leftPadding + leftInsertionBox.Width + leftCoverBox.Width + bottomGlueBox.Width, _topPadding + topRect.Height + frontRect.Height + bottomRect.Height + backRect.Height + bottomGlueBox.Height));


     int totalX = 0, totalY = 0;
                foreach (Point p in boundariesCoords)
                {
                    totalX += p.X;
                    totalY += p.Y;
                }
                int centerX = totalX / boundariesCoords.Count;
                int centerY = totalY / boundariesCoords.Count;

答案 1 :(得分:0)

获得边界的中心应该是微不足道的(宽度/ 2,高度/ 2)+(偏移X,偏移Y),所以我认为这不是你所指的。采取&#34;质量中心&#34;在您绘制的所有矩形中,只需松散地跟踪最外边缘:

for each rectangle
    minLeft = min(currentLeft, minLeft)
    maxRight = max(currentRight, maxRight)
    minTop = ...
    maxBottom = ...

centerX = (maxRight - minLeft) / 2 + minLeft
centerY = ...

这有助于回答您的问题吗?