1

Closed

Getlines and GetlinesAutoClosed

description

add to extend method call:
GetLines

GetLinesAutoClosed

source code is here:
 
    public static System.Windows.Shapes.Polyline GetLines(this FrameworkElement fe,Brushs b,string points,bool isArea = false) {
        System.Windows.Shapes.Polyline line = null;
        if (!string.IsNullOrEmpty(points)) {
            if (isArea)
                line = new System.Windows.Shapes.Polyline { Fill = b.ToBrush() };
            else
                line = new System.Windows.Shapes.Polyline { Stroke = b.ToBrush() };
            var mark = points[0];
            if (mark != 'p') {
                var _points = points.Split(' ');
                foreach (var item in _points) {
                    var v1 = item.Split(',');
                    if (v1.Length != 2) throw new Exception("points 语法错误,空格分割的是数对由逗号分开");
                    line.Points.Add(new Point(double.Parse(v1[0]),double.Parse(v1[1])));
                }
            } else {
                var length = points.Length;
                var _points = points.Substring(1).Split(',');
                var original = _points[0].Split(' ');
                if (original.Length != 2) throw new Exception("初始点定义错误");
                var x = double.Parse(original[0]);
                var y = double.Parse(original[1]);
                line.Points.Add(new Point(x,y));
                var offsets = _points[1].Split(' ');
                var count = offsets.Length;
                if (count > 0 && count % 2 == 0)
                    for (int i = 0 ; i < count ; i += 2) {
                        var offestx = double.Parse(offsets[i]);
                        var offesty = double.Parse(offsets[i + 1]);
                        line.Points.Add(new Point(x + offestx,y + offesty));
                        x += offestx;
                        y += offesty;
                    }
            }
        }
        return line;
    }
    public static System.Windows.Shapes.Polygon GetLinesAutoClosed(this FrameworkElement fe,Brushs b,string points,bool isArea = false) {
        System.Windows.Shapes.Polygon line = null;
        if (!string.IsNullOrEmpty(points)) {
            if (isArea)
                line = new System.Windows.Shapes.Polygon { Fill = b.ToBrush() };
            else
                line = new System.Windows.Shapes.Polygon { Stroke = b.ToBrush() };
            var mark = points[0];
            if (mark != 'p') {
                var _points = points.Split(' ');
                foreach (var item in _points) {
                    var v1 = item.Split(',');
                    if (v1.Length != 2) throw new Exception("points 语法错误,空格分割的是数对由逗号分开");
                    line.Points.Add(new Point(double.Parse(v1[0]),double.Parse(v1[1])));
                }
            } else {
                var length = points.Length;
                var _points = points.Substring(1).Split(',');
                var original = _points[0].Split(' ');
                if (original.Length != 2) throw new Exception("初始点定义错误");
                var x = double.Parse(original[0]);
                var y = double.Parse(original[1]);
                line.Points.Add(new Point(x,y));
                var offsets = _points[1].Split(' ');
                var count = offsets.Length;
                if (count > 0 && count % 2 == 0)
                    for (int i = 0 ; i < count ; i += 2) {
                        var offestx = double.Parse(offsets[i]);
                        var offesty = double.Parse(offsets[i + 1]);
                        line.Points.Add(new Point(x + offestx,y + offesty));
                        x += offestx;
                        y += offesty;
                    }
            }
        }
        return line;
    }
---============
test code :
 
            var v1 = this.fGridPanel.GetLinesAutoClosed(Brushs.GreenYellow,"50,50 100,50 100,150 50,150",true);
            var v2 = this.fGridPanel.GetLines(Brushs.Red,"50,50 100,50 100,150 50,150");
            var v3 = this.fGridPanel.GetLinesAutoClosed(Brushs.Black,"50,50 100,50 100,150 50,150");
            var v4 = this.fGridPanel.GetLines(Brushs.Bisque,"p100 100,50 30 29 0 20 150");
            var v5 = this.fGridPanel.GetLinesAutoClosed(Brushs.Chocolate,"p100 100,50 30 29 0 20 150",true);
            this.fGridPanel.ShowCase(v1,v2,v3,v4,v5);
Closed Nov 17, 2011 at 2:22 AM by ProjectDD

comments