2012-11-12 4 views
4

나는 사각형 임의의 각도 wшth 생성 whiсh 일부 코드가 있습니다 : 예를 들어,클리핑 사각형

enter image description here

를하지만 컷 아이들이 부모 테두리로 직사각형 필요

enter image description here

내 코드 : http://pastebin.com/b6ry8j68

누구든지 나를 도와 줄 수 있습니까?

+0

최대 및 최소, x 및 y. –

+0

무슨 소리 야? – Wolfgang

+0

사각형의 부모/자식 관계를 어떻게 결정합니까? –

답변

3

SetClip 속성으로 꽤 쉽게 할 수 있습니다. 바로 drawline 명령 전에

  if (!pre_defined) 
      { 
       g.SetClip(new Rectangle(x, y, 600, 300)); 
      } 

:

은 기본적으로이 코드를 추가해야합니다. 여기서 x와 y는 부모 사각형의 좌표입니다. 그것은 당신의 기능에서 쉽게 얻을 수 있습니다.

이 작품 완성 기능은 다음과 같습니다

public void drawRectangle(double Width, double Height, int A, bool pre_defined) 
    { 
     Graphics g = pictureBox1.CreateGraphics(); 
     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
     g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 

     System.Drawing.Brush brush = new System.Drawing.SolidBrush(Color.FromArgb(r.Next(0, 251), r.Next(0, 251), r.Next(0, 251))); 
     Pen myPen = new Pen(brush, 2); 
     myPen.Width = 2; 
     int x = center.X; 
     int y = center.Y; 
     //top left 
     P[0] = new PointF((float)Math.Round(x + (Width/2) * Math.Cos(A) + (Height/2) * Math.Sin(A)), (float)Math.Round(y - (Height/2) * Math.Cos(A) + (Width/2) * Math.Sin(A))); 
     //top right 
     P[1] = new PointF((float)Math.Round(x - (Width/2) * Math.Cos(A) + (Height/2) * Math.Sin(A)), (float)Math.Round(y - (Height/2) * Math.Cos(A) - (Width/2) * Math.Sin(A))); 
     //bottom left 
     P[2] = new PointF((float)Math.Round(x + (Width/2) * Math.Cos(A) - (Height/2) * Math.Sin(A)), (float)Math.Round(y + (Height/2) * Math.Cos(A) + (Width/2) * Math.Sin(A))); 
     //bottom right 
     P[3] = new PointF((float)Math.Round(x - (Width/2) * Math.Cos(A) - (Height/2) * Math.Sin(A)), (float)Math.Round(y + (Height/2) * Math.Cos(A) - (Width/2) * Math.Sin(A))); 
     if (!pre_defined) 
     { 
      g.SetClip(new Rectangle(50, 50, 600, 300)); 
     } 
     g.DrawLine(myPen, P[0], P[1]); 
     g.DrawLine(myPen, P[1], P[3]); 
     g.DrawLine(myPen, P[3], P[2]); 
     g.DrawLine(myPen, P[2], P[0]); 
    } 

편집 :이 하나는 부모의 폭과 높이에 클립을 설정하기 때문에
이 완벽한 예가 아니다. 함수를 수정하여 각 요소의 폭과 높이를 제공해야합니다. 하지만 지금은 당신이 제공 한 그림을보고 있고 생각보다 복잡해 보입니다.
아마 모든 임의 값의 배열을 저장하게 될 것이고, 크기에 따라 순서를 정렬 한 다음 모든 요소를 ​​그립니다.

+0

그래, 정말 작동하지만, 대학 과제이고, 나는 setClip과 같은 도우미없이 실현해야한다고 생각한다. – Wolfgang

+0

그런 훌륭한 대답. 이 문제를 자주 접하게되었습니다. – Neophile