What is Geometry?
Geometry objects, such as EllipseGeometry, PathGeometry, and GeometryGroup, can be used to depict geometry of two-dimensional (2-d) shapes. These geometries are depicted for many purposes, such as defining a shape to be drawn to the screen or defining a clipping region. Geometry objects can be simple (such as rectangles and circles), or they can be composite shapes that are created based on two or more Geometry objects. Use the PathGeometry object to create more complex geometries that can be used to depict arcs and curves.
Comparison of Geometry and Shape
The similarity between Geometry and shape classes is that they both depict two-dimensional shapes (for example, compare EllipseGeometry and Ellipse), but there are some important differences between them. For example, a Shape object is a UIElement object, and a Geometry object is not. Because it is a UIElement object, a Shape object can render itself and have other graphic properties that Opacity, OpacityMask, and Geometry objects do not have. Although a Shape object is easier to use than a Geometry object, the Geometry object is more versatile.
A Shape (the Path Class) uses Geometry to depict the content. You can render Geometry by using Geometry to set the Data property of the Path and to set its Fill and Stroke properties.
Common properties that take Geometry
The following table lists some of the properties that take the Geometry object.
Type |
Property |
Path |
Data |
UIElement |
Clip |
Simple geometry type
Geometry objects can be grouped into three categories: simple geometries, path geometries, and composite geometries.
Simple geometry classes include LineGeometry, RectangleGeometry, and EllipseGeometry, which are used to create basic geometric shapes such as lines, rectangles, and circles.
LineGeometry is defined by specifying the starting and ending points of a line.
RectangleGeometry is defined by using the RECT structure, which specifies the relative position, height, and width of the rectangle. You can create rounded rectangles by setting the RadiusX and RadiusY properties.
The ellipsegeometry are defined by the center point, the X radius, and the y radius.
Although you can create these and more complex shapes by using pathgeometry or by combining the Geometry objects, the simple geometry class provides a simple way to generate these basic geometry shapes.
The following example shows how to create and render linegeometry. As mentioned earlier, the Geometry object cannot draw itself, so this example uses the Path shape to render a line. Because the line has no area, the Fill property that sets Path has no effect; therefore, only the Stroke and StrokeThickness properties are specified.
Xaml
<Canvas Width="200" Height="200">
<Path Stroke="Black" StrokeThickness="1" >
<Path.Data>
<LineGeometry StartPoint="10,20" EndPoint="100,130" />
</Path.Data>
</Path>
</Canvas>