【问题标题】:How to calculate the center of a drawing (multiple rectangles)如何计算图形的中心(多个矩形)
【发布时间】:2016-10-19 14:59:59
【问题描述】:

我有一个 C# windows 窗体,我在其中绘制多个矩形:

我在我的pictureboxOnPaint 事件中这样做。

是否有任何“简单”的方法来获得该图的中心?

这是我的代码:

private void pbBox_Paint(object sender, PaintEventArgs e)
        {

            Rectangle leftInsertionBox = new Rectangle(_leftPadding, _topPadding + _boxBase, _insertion, _boxFront);

            Rectangle leftCoverBox = new Rectangle(_leftPadding + leftInsertionBox.Width, _topPadding + _boxBase, _boxBase, _boxFront);

            Rectangle leftTopDustBox = new Rectangle(_leftPadding + leftInsertionBox.Width + leftCoverBox.Width - (_insertion + _boxBase) / 2, _topPadding, (_insertion + _boxBase) / 2, _boxBase);
            Rectangle rightTopDustBox = new Rectangle(_leftPadding + leftInsertionBox.Width + leftCoverBox.Width + _boxHeight, _topPadding, (_insertion + _boxBase) / 2, _boxBase);

            Rectangle topRect = new Rectangle(_leftPadding + leftInsertionBox.Width + leftCoverBox.Width, _topPadding, _boxHeight, _boxBase);
            Rectangle frontRect = new Rectangle(_leftPadding + leftInsertionBox.Width + leftCoverBox.Width, _topPadding + _boxBase, _boxHeight, _boxFront);

            Rectangle leftBottomDustBox = new Rectangle(_leftPadding + leftInsertionBox.Width + leftCoverBox.Width - (_insertion + _boxBase) / 2, _topPadding + topRect.Height + frontRect.Height, (_insertion + _boxBase) / 2, _boxBase);
            Rectangle rightBottomDustBox = new Rectangle(_leftPadding + leftInsertionBox.Width + leftCoverBox.Width + _boxHeight, _topPadding + topRect.Height + frontRect.Height, (_insertion + _boxBase) / 2, _boxBase);

            Rectangle bottomRect = new Rectangle(_leftPadding + leftInsertionBox.Width + leftCoverBox.Width, _topPadding + _boxBase + _boxFront, _boxHeight, _boxBase);
            Rectangle backRect = new Rectangle(_leftPadding + leftInsertionBox.Width + leftCoverBox.Width, _topPadding + _boxBase + _boxFront + _boxBase, _boxHeight, _boxFront);

            Rectangle rightCoverBox = new Rectangle(_leftPadding + leftInsertionBox.Width + leftCoverBox.Width + _boxHeight, _topPadding + _boxBase + _boxFront + _boxBase, _boxBase, _boxFront);

            Rectangle rightInsertionBox = new Rectangle(_leftPadding + leftInsertionBox.Width + leftCoverBox.Width + _boxHeight + rightCoverBox.Width, _topPadding + topRect.Height + frontRect.Height + bottomRect.Height , _insertion, _boxFront);

            Rectangle bottomGlueBox = new Rectangle(_leftPadding + leftInsertionBox.Width + leftCoverBox.Width, _topPadding + topRect.Height + frontRect.Height + bottomRect.Height + backRect.Height, _boxHeight, _glue);

            // solid brush
            SolidBrush myBrush = new SolidBrush(System.Drawing.Color.LightGray);

            LinearGradientBrush linearGradientBrush = new LinearGradientBrush(topRect, Color.Red, Color.Yellow, 90);

            ColorBlend cblend = new ColorBlend(3);
            cblend.Colors = new Color[3]  { Color.Red, Color.Yellow, Color.Green };
            cblend.Positions = new float[3] { 0f, 0.5f, 1f };

            linearGradientBrush.InterpolationColors = cblend;


            using (Pen pen = new Pen(Color.DarkGray, 1))
            {
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

                DoRotation(e);

                e.Graphics.DrawRectangle(pen, leftInsertionBox);
                e.Graphics.DrawRectangle(pen, leftCoverBox);

                e.Graphics.DrawRectangle(pen, leftTopDustBox);
                e.Graphics.DrawRectangle(pen, rightTopDustBox);
                e.Graphics.DrawRectangle(pen, leftBottomDustBox);
                e.Graphics.DrawRectangle(pen, rightBottomDustBox);

                e.Graphics.DrawRectangle(pen, topRect);
                e.Graphics.FillRectangle(myBrush, topRect.X+1,topRect.Y+1,topRect.Width-1,topRect.Height-1);

                e.Graphics.DrawRectangle(pen, frontRect);
                e.Graphics.FillRectangle(myBrush, frontRect.X + 1, frontRect.Y + 1, frontRect.Width - 1, frontRect.Height - 1);

                e.Graphics.DrawRectangle(pen, bottomRect);
                e.Graphics.FillRectangle(myBrush, bottomRect.X + 1, bottomRect.Y + 1, bottomRect.Width - 1, bottomRect.Height - 1);

                e.Graphics.DrawRectangle(pen, backRect);
                e.Graphics.FillRectangle(myBrush, backRect.X + 1, backRect.Y + 1, backRect.Width - 1, backRect.Height - 1);


                e.Graphics.DrawRectangle(pen, rightCoverBox);
                e.Graphics.DrawRectangle(pen, rightInsertionBox);

                e.Graphics.DrawRectangle(pen, bottomGlueBox);
            }


        }

【问题讨论】:

  • 图纸是大盒子吗?还是它的形式?你总是画相同的形状吗?
  • 将它们添加到 GraphicsPath 并使用其边界矩形。

标签: c# paint picturebox


【解决方案1】:

我找到了办法:

  // 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;

【讨论】:

    【解决方案2】:

    获得边界的中心应该是微不足道的 (width/2, height/2) + (offsetX, offsetY) 所以我认为这不是你所指的。要获取您正在绘制的所有矩形的“质心”,只需松散地跟踪最外边:

    for each rectangle
        minLeft = min(currentLeft, minLeft)
        maxRight = max(currentRight, maxRight)
        minTop = ...
        maxBottom = ...
    
    centerX = (maxRight - minLeft) / 2 + minLeft
    centerY = ...
    

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

    【讨论】:

      猜你喜欢
      • 2011-06-22
      • 2017-06-18
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 2021-08-17
      • 2013-11-14
      相关资源
      最近更新 更多