• WPF绘图(一):几何(Geometry)与形状(Shape)


    1. Geometry

    在数学中,我们可以用一个方程描述圆:x2+y2=25。这个方程描述的是,一个半径为5,中心点在(0,0)的圆。这种纯数学的描述就是Geometry(几何)。

    但此时,这个“圆”我们是看不见,摸不着的。如果想要看到这个几何图形,就必须用画笔,颜色等信息,去“绘制”它。

    .Net中,Geometry类就是用于描述这种几何图形的类。它只包含描述几何图形的必要属性,并提供包括命中测试、动画等功能。它派生出以下七个类,以简化对图形的描述:

    类名
    功能
    LineGeometry
    直线
    EllipseGeometry
    椭圆(圆也是椭圆的一种)
    RectangleGeometry
    矩形
    PathGeometry
    路线(类似与随手画的一条任意线)
    GeometryGroup
    几何图形组(把多个几何图形放在一起)
    CombinedGeometry
    组合图形(将多个几何图形做布尔运算)
    StreamGeometry
    它是PathGeometry的轻量级对象。

    1.1 LineGeometry

    它表示一条直线。

    它主要属性有EndPoint和StartPoint,用于定义直线的终点和起点。

    Point start = new Point(0,0); 
    Point end = new Point(50,50);
    LineGeometry line = new LineGeometry(start,end);

    1.2 EllipseGeometry

    它表示一个椭圆(圆也是椭圆的一种)。

    它主要属性有Center(中心点坐标)、RadiusX(x轴半径)、RadiusY(Y轴半径)。

    Point center = new Point(50,50); 
    EllipseGeometry ellipseGeo = new EllipseGeometry(center , 45, 20);

    1.3 RectangleGeometry

    它表示一个矩形(正方形也是矩形的一种)。

    它主要属性有Rect(矩形框)、RadiusX(x轴半径)、RadiusY(y轴半径)。当定义了Rect属性后,RadiusX和RadiusY的值也就确定了;当给定了RadiusX和RadiusY的值,Rect属性的值也就确定了。

    RectangleGeometry rect = new RectangleGeometry(10,20);

    1.4 PathGeometry

    它表示一条路线。这条路线中,可以有直线给,也可以有曲线。

    它最重要的属性是Figures(路径图元),类型是PathFigureCollection,意味着它是一个集合,可以装载多个PathFigure对象。

    PathFigure类,有一个重要的属性Segments,类型是PathSegmentCollection,意味着它也是一个集合,可以装载多个PathSegment(路径中的一部分)对象。

    由此可见:多个Segment组成了一个PathSegmentCollection(PathFigure的Segment属性);多个PathFigure组成了一个PathFigureCollection(PathGeometry的PathFigure属性)。

    复制代码
    // 定义SegmentCollection1 
    LineSegment line1 = new(...);
    // 定义直线部分
    PolyLineSegment pline1 = new (...);
    // 定义多段线部分
    ArcSegment arc1 = new(...);
    // 定义弧形线段部分
    PathSegmentCollection segments1 = new(new PathSegment[] { line1, pline1, arc1 });
    // 定义PathSegment集合
    PathFigure path1 = new(new Point(0, 0), segments1, false);
    // 定义第一个的PathFigure

    // 定义SegmentCollection2
    LineSegment line2 = new(...);
    BezierSegment beszier = new(...);
    // 定义Bezier曲线部分
    PathSegmentCollection segments2 = new(new PathSegment[] { line2, beszier });
    PathFigure path2 = new(new Point(300, 0), segments2, false);
    // 定义第二个的PathFigure


    // 定义PathFigureCollection
    PathFigureCollection pathFigures = new(new PathFigure[] { path1, path2 });

    PathGeometry pathGeo = new() { Figures = pathFigures };
    // 定义PathGeometry
    
    
    复制代码

    0

     备注:.Net提供“路径标记语法”用于简化PathFigures的创建。

    1.5 GeometryGroup

    它表示多个集合图形的集合。

    它的主要属性有Children(包含的子图形),类型是GeometryCollection,意味着它可以可以装载多个Geometry对象。

    LineGeometry lineGeo = new(); 
    RectangleGeometry rectGeo = new(); 
    PathGeometry pathGeo = new(); 
    GeometryGroup geoGroup = new() { Children = new GeometryCollection() { lineGeo, rectGeo, pathGeo } };

    GeometryGroup中的几何图形,也可以设置FillRule属性,以设置多个几何图形交叉区域的合并方式。

    1.6 CombinedGeometry

    它表示一个,由两个几何图形组合而成的图形。

    它的主要属性有Geometry1(几何图形1)、Geometry2(几何图形2)、GeometryCombineMode(图形结合的方式)

    RectangleGeometry rectGeo = new(); 
    PathGeometry pathGeo = new(); 
    CombinedGeometry CombineGeo = new(GeometryCombineMode.Exclude, rectGeo, pathGeo);

    备注:

    • 该类的GeometryCombineMode指的是多个图形的“结合”方式,这种方式通常指的是布尔运算;而部分Geometry对象的FillRule,指的是交叠的图形的“填充”方式。指的是图形的重叠闭合区域的“填充”方式;
    • 虽然CombineGeometry只能“结合”两个Geometry对象。但是要注意,Geometry1和Geometry2的类型都是Geometry,意味只要只要是Geometry的子类,都可以赋值给这两个属性。因此,可以将多个Geometry填到GemetryGroup中,也可以两两CombinedGeometry,然后再将CombinedGeometry再CombinedGeometry。

    1.7 StreamGeometry

    它是PathGeometry的轻量级表示,不支持数据绑定、动画或修改。

    这意味着,如果PathGeometry占用太多资源,且不需要数据绑定、动画、修改等功能,可以用StreamGeometry来替代。

    它的工作原理是创建一个StreamGeometryContext对象,并在这个对象中绘制图形。所以,它没有图形数据相关的属性,

    StreamGeometry streamGoe = new(); 
    StreamGeometryContext context = streamGoe.Open(); 
    context.ArcTo(...); // SteamGeometryContext类提供了多种绘图方法。 context.PolyLineTo(...); 
    context.Close();

     2. Shape

    形状(Shape),就是一个能看得到的几何图形。例如我们在白纸上,用红色的笔画一条直线,一个矩形。

    由此可见,Shape的本质,就是在几何图形(Geometry)的基础上,加上画笔,颜色等信息,绘制而成。

    .Net中,Shape就是表示“形状”的类。它是一个抽象类,继承自FrameworkElement,这意味着它是一个元素,可以直接显示在窗体上。

    Shape类派生出了以下几个类:

    类型
    功能
    Ellipse
    椭圆形
    Line
    直线
    Path
    路径
    Polygon
    多边形
    Polyline
    多段线
    Rectangle
    矩形

    这些类都比较容易使用,就不再一一赘述。

  • 相关阅读:
    Redis 管道
    华为---- RIP路由协议基本配置
    中医实训室:在传统针灸教学中的应用与创新
    数据结构与算法系列二之链表、哈希表及栈
    黑客(网络安全)技术自学30天
    基于单片机设计的水平仪(STC589C52+MPU6050)
    Redis 常用命令和基本数据结构(数据类型)
    Centos安装postgresql
    webrtc中的任务队列TaskQueue
    Mac nginx安装,通过源码安装教程
  • 原文地址:https://www.cnblogs.com/raynado/p/17775333.html