画锚[未使用矩阵坐标转换]

来源:互联网 发布:免费翻墙软件 编辑:程序博客网 时间:2024/05/16 01:50
   #region 画锚枚举    public enum LineCaps:int    {        ///         /// 无锚头        ///         NoAnchor = 0,        ///         /// 尾部凹入的等腰三角形        ///              ArrowAnchor = 1,        ///         /// 菱形        ///         DiamondAnchor = 2,        ///         /// 半圆        ///         Round = 4,        ///         /// 圆        ///         RoundAnchor = 8,        ///         /// 半长方形        ///         Square = 16,        ///         /// 长方形        ///         SquareAnchor = 32,        ///         /// 等腰三角形        ///         Triangle = 64,        ///         /// 加入此项可使锚头长减为50%        ///         Flat = 128,        ///         /// 60f圆弧        ///         Round60F = 256,        ///         /// 30f圆弧        ///         Round30F = 512    };    #endregion            #region 画锚公共方法        ///         /// 画直线        ///         /// 
Graphics /// startPoint /// endPoint /// pen /// capMultiple锚相对于Pen的倍数 public void DrawLine( Graphics g, Point startPoint, Point endPoint, Pen pen,LineCaps startCap,LineCaps endCap, int capMultiple) { pen.StartCap = LineCap.NoAnchor; pen.EndCap = LineCap.NoAnchor; //画线 g.DrawLine(pen,startPoint,endPoint); //画Cap DrawCap(g, endPoint, startPoint, pen, startCap, capMultiple); DrawCap(g, startPoint, endPoint, pen, endCap, capMultiple); } /// /// 画锚 /// /// Graphics /// startPoint用来表示锚的方向 /// endPoint为基点 /// pen /// 锚的形状 /// 锚相对于Pen的倍数 private void DrawCap( Graphics g, Point startPoint, Point endPoint, Pen pen, LineCaps cap, int capMultiple) { if (cap == LineCaps.NoAnchor) return; double sng = 0.0f; double tan = 0.0; if (startPoint.X - endPoint.X != 0) { tan = (double)(endPoint.Y - startPoint.Y) / (double)(endPoint.X - startPoint.X); sng = (double)Math.Atan(tan) * 180 / Math.PI; //区分1.4,2.3象限;如果是2.3象则翻转 if ((endPoint.X - startPoint.X < 0 && endPoint.Y - startPoint.Y > 0) ||(endPoint.X - startPoint.X < 0 && endPoint.Y - startPoint.Y < 0)) sng += 180.0; } else { if (endPoint.Y < startPoint.Y) sng = 90.0f; else sng = -90.0f; } DrawCap(g,sng,endPoint ,pen,cap,capMultiple); } /// /// 画锚 /// /// Graphics /// 锚的方向 /// 锚的基点 /// pen /// 锚相对于Pen的倍数 private void DrawCap( Graphics g, double single, Point point, Pen pen, LineCaps cap, int capMultiple) { double iSize = pen.Width * capMultiple; if (iSize == 0) return; pen.Width = 1; int iTiems = 1; if ((cap & LineCaps.Flat) >0) iTiems = 2; cap &= ~LineCaps.Flat; switch (cap) { case LineCaps.ArrowAnchor ://尾部凹入的等腰三角形 float pr = 0.3f; double h1 = iSize / iTiems * pr; Point p4 = new Point(point.X + (int)(Math.Cos(single) * h1), point.Y + (int)(Math.Sin(single) * h1)); g.DrawLine(pen, point, p4); DrawArrowAnchor(point, iSize, iSize / iTiems, single, pen, g, pr); break; case LineCaps.DiamondAnchor://;菱形 DrawDiamondAnchor(point, iSize, iSize / iTiems, single, pen, g); break; //case LineCaps.Flat : // break; case LineCaps.Round ://半圆 DrawRound(point, iSize, iSize / iTiems, single, pen, g, 180.0f); break; case LineCaps.RoundAnchor://圆 DrawRound(point, iSize, iSize / iTiems, single, pen, g, 360.0f); break; case LineCaps.Round60F://60f圆弧 DrawRound(point, iSize, iSize / iTiems, single, pen, g, 60.0f); break; case LineCaps.Round30F://30f圆弧 DrawRound(point, iSize, iSize / iTiems, single, pen, g, 30.0f); break; case LineCaps.Square://长方形 DrawSquare (point, iSize, iSize / iTiems/2, single, pen, g); break; case LineCaps.SquareAnchor ://正方形 DrawSquare(point, iSize, iSize / iTiems, single, pen, g); break; case LineCaps.Triangle ://等腰三角形 DrawTriangle(point, iSize, iSize / iTiems, single, pen, g); break; } } /// /// 画尾部凹入的等腰三角形 /// /// 中线 /// 底边 /// 底边与中线交点 /// 中线与X的夹角 /// pen /// Graphics /// 凹入的比例 public void DrawArrowAnchor(Point point, double width, double height, double single, Pen pen, Graphics g, double pr) { double h1 = height * pr; int x1, y1, x2, y2, x3, y3,x4,y4; Point[] points; double sng = (double)((90.0f - single) * Math.PI / 180); single *= (double)(Math.PI / 180); int x = point.X - (int)(h1 * Math.Cos (single)); int y = point.Y - (int)(h1 * Math.Sin (single)); width /= 2; x1 = x - (int)(Math.Cos(sng) * width); y1 = y + (int)(Math.Sin(sng) * width); x2 = x + (int)(Math.Cos(sng) * width); y2 = y - (int)(Math.Sin(sng) * width); x3 = x + (int)(Math.Cos(single) * height); y3 = y + (int)(Math.Sin(single) * height); Point p1 = new Point(x1, y1); Point p2 = new Point(x2, y2); Point p3 = new Point(x3, y3); if (pr > 0) {//计算第四点 x4 = x + (int)(Math.Cos(single) * h1); y4 = y + (int)(Math.Sin(single) * h1); Point p4 = new Point(x4,y4); points = new Point[] { p1, p4, p2, p3 }; } else {//没有第四点 points = new Point[] { p1, p2, p3 }; } DrawPolygon(g, points,pen ); } /// /// 画等腰三角形 /// /// 底边与中线交点 /// 中线 /// 底边 /// 中线与X的夹角 /// pen /// /// Graphics public void DrawTriangle(Point point, double width, double height, double single, Pen pen, Graphics g) { DrawArrowAnchor(point, width, height, single, pen, g, 0); } /// /// 画菱形 /// /// 中心点 /// 对角线的1/2 /// 对角线 /// height与X的夹角 /// pen /// Graphics public void DrawDiamondAnchor(Point point, double width, double height, double single, Pen pen, Graphics g) { DrawTriangle(point, width, height, single, pen, g); DrawTriangle(point, width, height, 180.0f + single, pen, g); } /// /// 画圆弧 /// /// 圆心 /// 宽 /// 高 /// 锚方向与X的夹角 /// pen /// Graphics /// 圆弧度数 public void DrawRound(Point point, double width, double height, double single, Pen pen, Graphics g, double fie) { fie /= 2; height /= 2; width /= 2; double offset = Math.Abs(fie * 2); using (SolidBrush brush = new SolidBrush(pen.Color)) { g.FillPie(brush, (float)(point.X - width), (float)(point.Y - height), (float)width * 2, (float)height * 2, (float)(single - fie), (float)offset); g.DrawPie(pen, (float)(point.X - width), (float)(point.Y - height), (float)width * 2, (float)height * 2, (float)(single - fie), (float)offset); } } /// /// 画长方形 /// /// 中心 /// 宽 /// 高 /// 高与X的夹角 /// pen /// Graphics public void DrawSquare(Point point, double width, double height, double single, Pen pen, Graphics g) { int x = point.X; int y = point.Y; int x1, x2, x3, x4, y1, y2, y3, y4; Point p1, p2, p3, p4; double sng; double len; double sng1; double sng2; width /= 2; height /= 2; sng1 = Math.Atan(width / height) * 180 / Math.PI; sng2 = Math.Atan(height / width) * 180 / Math.PI; len = Math.Sqrt (height*height +width *width); sng =((sng1 + single) * Math.PI / 180); x1 = x - (int)(Math.Cos(sng)*len); y1 = y - (int )(Math.Sin(sng)*len); sng = ((90.0f - single - sng2) * Math.PI / 180); x2 = x - (int)(Math.Cos(sng) * len); y2 = y + (int)(Math.Sin(sng) * len); sng = ((sng1 + single) * Math.PI / 180); x3 = x + (int)(Math.Cos(sng) * len); y3 = y + (int)(Math.Sin(sng) * len); sng = ((90.0f - single - sng2) * Math.PI / 180); x4 = x + (int)(Math.Cos(sng) * len); y4 = y - (int)(Math.Sin(sng) * len); p1 = new Point(x1, y1); p2 = new Point(x2, y2); p3 = new Point(x3, y3); p4 = new Point(x4, y4); Point[] points = new Point[] { p1,p2,p3,p4}; DrawPolygon(g,points,pen); } /// /// 画多边形 /// /// Graphics /// 点阵 public void DrawPolygon(Graphics g, Point[] points, Pen pen) { using (SolidBrush brush = new SolidBrush(pen.Color )) { g.FillPolygon(brush, points); g.DrawPolygon(pen, points); } } #endregion//用法与用量: private void panel1_Paint(object sender, PaintEventArgs e) { e.Graphics .Clear(System.Drawing.SystemColors.Control); using (Pen p = new Pen(Color.Black)) { p.Width = 1; LineCaps lc = (LineCaps)Enum.Parse(typeof(LineCaps), this.comboBox1.SelectedValue.ToString()); if (this.checkBox1.Checked) lc |= LineCaps.Flat; DrawLine(e.Graphics, this.startPoint , this.endPoint, p, LineCaps.NoAnchor, lc, 50); } }