You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

195 lines
6.8 KiB

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
{
/// <summary>
/// 画图类
/// </summary>
public class Draw
{
/// <summary>
/// 初始化对象
/// </summary>
/// <param name="broad">画布</param>
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;
}
/// <summary>
/// 以矩形的两个对角坐标为根据画矩形
/// </summary>
/// <param name="a">一个顶点坐标</param>
/// <param name="c">与a相对点的坐标</param>
/// <returns></returns>
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;
}
/// <summary>
/// 绘制三角形
/// </summary>
/// <param name="a">点a</param>
/// <param name="b">点b</param>
/// <param name="c">点c</param>
/// <returns></returns>
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;
}
/// <summary>
/// 绘制多边形
/// </summary>
/// <param name="points">多边形的顶点</param>
/// <returns></returns>
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));
}
}
}
/*
备注:
*网上关于“中点画圆法”的弧上点位置的说法只适用于单纯用点画圆,
而不适用于用线来画圆。
* 我的方法是,创建一个字段,用来存储圆的坐标,当遇到奇数弧时坐标列表从后往前
存储。
*/