用c#剪切矩形
发布时间:2020-12-15 05:36:42 所属栏目:百科 来源:网络整理
导读:我有一些代码可以生成随机角度的矩形: 但我需要通过父边界切割子矩形,例如 我的代码:http://pastebin.com/b6ry8j68 任何人都可以用算法来帮助我吗? 解决方法 使用 SetClip Property非常容易. 基本上你需要添加这个代码: if (!pre_defined) { g.SetClip(n
我有一些代码可以生成随机角度的矩形:
但我需要通过父边界切割子矩形,例如 我的代码:http://pastebin.com/b6ry8j68 任何人都可以用算法来帮助我吗? 解决方法
使用
SetClip Property非常容易.
基本上你需要添加这个代码: if (!pre_defined) { g.SetClip(new Rectangle(x,y,600,300)); } 在drawline命令之前.其中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))); 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,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]); } 编辑:这不是一个完整的例子,因为这个只会将Clip设置为父宽度和高度.您需要修改函数以提供每个元素的宽度和高度.但现在我正在看你提供的图片,它看起来比我想象的要复杂.您可能最终会存储所有随机值的数组,并按大小排序,然后绘制所有元素. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |