2016-06-17 3 views
0

У меня есть C# Windows вид где Im картина несколько прямоугольников:Как рассчитать центр чертежа (несколько прямоугольников)

enter image description here

Im делает что внутри OnPaint случае моей picturebox.

Есть ли какой-либо «простой» способ получить центр этого чертежа?

Это мой код:

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); 
      } 


     } 
+0

Является ли чертеж большой коробкой? Или это форма? Вы всегда рисуете одинаковые формы? –

+0

Добавление их в GraphicsPath и использование его прямоугольника Bounds. – TaW

ответ

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

получая центр рамки должен быть тривиальным (ширина/2, высота/2) + (offsetX, offsetY), поэтому я предполагаю, что это не то, о чем вы говорите. Для того, чтобы принять «центр масс» всех прямоугольников вы рисуете, просто отслеживать самые дальние края, свободно:

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

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

это поможет ответить на ваш вопрос?

Смежные вопросы