using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; namespace ECMonitor.PlayerDraw.Code { /// /// 画图类 /// public class Draw { /// /// 初始化对象 /// /// 画布 public Draw(InkCanvas broad) { this.broad = broad; arcs = new StylusPointCollection[8]; for (int i = 0; i < arcs.Length; i++) { arcs[i] = new StylusPointCollection(); } } InkCanvas broad; public StrokeEx Line(StylusPoint a, StylusPoint b, int warningLevel) { StylusPointCollection points = new StylusPointCollection { a, b }; StrokeEx line = new StrokeEx(points); line.DrawType = DrawTypes.线段; line.WarningLevel = warningLevel; line.DrawingAttributes = broad.DefaultDrawingAttributes.Clone(); line.DrawingAttributes.Color = BlackboardClass.ChangeColor((ColorEnum)warningLevel); return line; } /// /// 以矩形的两个对角坐标为根据画矩形 /// /// 一个顶点坐标 /// 与a相对点的坐标 /// public StrokeEx Rectangle(StylusPoint a, StylusPoint c,int warningLevel) { StylusPoint b = new StylusPoint(c.X, a.Y), d = new StylusPoint(a.X, c.Y); StylusPointCollection points = new StylusPointCollection { a, b, c, d, a }; StrokeEx rectangle = new StrokeEx(points); rectangle.DrawType = DrawTypes.矩形; rectangle.WarningLevel = warningLevel; rectangle.DrawingAttributes = broad.DefaultDrawingAttributes.Clone(); rectangle.DrawingAttributes.StylusTip = StylusTip.Rectangle; rectangle.DrawingAttributes.Color = BlackboardClass.ChangeColor((ColorEnum)warningLevel); return rectangle; } /// /// 绘制三角形 /// /// 点a /// 点b /// 点c /// public Stroke Triangle(StylusPoint a, StylusPoint b, StylusPoint c) { StylusPointCollection points = new StylusPointCollection { a, b, c, a }; StrokeEx triangle = new StrokeEx(points); triangle.DrawType = DrawTypes.三角形; triangle.DrawingAttributes = broad.DefaultDrawingAttributes.Clone(); return triangle; } /// /// 绘制多边形 /// /// 多边形的顶点 /// public Stroke Polygon(StylusPointCollection points, int warningLevel) { points.Add(points[0]); StrokeEx polygon = new StrokeEx(points); polygon.DrawType = DrawTypes.多边形; polygon.WarningLevel = warningLevel; polygon.DrawingAttributes = broad.DefaultDrawingAttributes.Clone(); polygon.DrawingAttributes.Color = BlackboardClass.ChangeColor((ColorEnum)warningLevel); return polygon; } public Stroke Lines(StylusPointCollection points, int warningLevel) { StrokeEx polygon = new StrokeEx(points); polygon.DrawType = DrawTypes.多边形; polygon.WarningLevel = warningLevel; polygon.DrawingAttributes = broad.DefaultDrawingAttributes.Clone(); polygon.DrawingAttributes.Color = BlackboardClass.ChangeColor((ColorEnum)warningLevel); return polygon; } public Stroke Circle(StylusPoint o, StylusPoint p,int warningLevel) { StylusPointCollection points = OriginalArc(o, p); StrokeEx circle = new StrokeEx(points); circle.DrawType = DrawTypes.圆; circle.WarningLevel = warningLevel; circle.DrawingAttributes = broad.DefaultDrawingAttributes.Clone(); circle.DrawingAttributes.Color = BlackboardClass.ChangeColor((ColorEnum)warningLevel); for (int i = 0; i < arcs.Length; i++) { arcs[i] = new StylusPointCollection(); } return circle; } private StylusPointCollection OriginalArc(StylusPoint o, StylusPoint p) { StylusPointCollection points = new StylusPointCollection(); double r = Math.Sqrt(Math.Pow(p.X - o.X, 2) + Math.Pow(p.Y - o.Y, 2));//该圆的半径 double d = 3 - 2 * r; StylusPoint motionPoint = new StylusPoint(); motionPoint.X = 0; motionPoint.Y = r; CirclePoints(o, motionPoint); while (motionPoint.X < motionPoint.Y) { if (d < 0) d = d + 4 * motionPoint.X + 6; else { d = d + 4 * (motionPoint.X - motionPoint.Y) + 10; motionPoint.Y--; } motionPoint.X++; CirclePoints(o, motionPoint); } for (int i = 0; i < arcs.Length; i++) { if (i % 2 != 0) { for (int index = arcs[i].Count - 1; index >= 0; index--) { points.Add(arcs[i][index]); } } else points.Add(arcs[i]); } return points; } StylusPointCollection[] arcs;//表示一个圆的八个弧 private void CirclePoints(StylusPoint o, StylusPoint p) { arcs[0].Add(new StylusPoint(p.X + o.X, p.Y + o.Y)); arcs[1].Add(new StylusPoint(p.Y + o.X, p.X + o.Y)); arcs[2].Add(new StylusPoint(-p.Y + o.X, p.X + o.Y)); arcs[3].Add(new StylusPoint(-p.X + o.X, p.Y + o.Y)); arcs[4].Add(new StylusPoint(-p.X + o.X, -p.Y + o.Y)); arcs[5].Add(new StylusPoint(-p.Y + o.X, -p.X + o.Y)); arcs[6].Add(new StylusPoint(p.Y + o.X, -p.X + o.Y)); arcs[7].Add(new StylusPoint(p.X + o.X, -p.Y + o.Y)); } } } /* 备注: *网上关于“中点画圆法”的弧上点位置的说法只适用于单纯用点画圆, 而不适用于用线来画圆。 * 我的方法是,创建一个字段,用来存储圆的坐标,当遇到奇数弧时坐标列表从后往前 存储。 */