C#WPF How do I draw geometry? How to draw a coordinate system?
This is inseparable from the path (System.Windows.Shapes) and StreamGeometry (System.Windows.Media) classes.
I. Building a WPF Project
Second, add code
Code in MainWindow.xaml
<Window x:Class="WPFDrawingTraning.MainWindow"
xmlns="<a target=_blank href="http://schemas.microsoft.com/winfx/2006/xaml/presentation">http://schemas.microsoft.com/winfx/2006/xaml/presentation</a>"
xmlns:x="<a target=_blank href="http://schemas.microsoft.com/winfx/2006/xaml">http://schemas.microsoft.com/winfx/2006/xaml</a>"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Canvas Name="mainPanel" HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="517"/>
</Grid>
</Window>
Code in MainWindow.xaml.cs
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows;
Using System.Windows.Controls;
Using System.Windows.Data;
Using System.Windows.Documents;
Using System.Windows.Input;
Using System.Windows.Media;
Using System.Windows.Media.Imaging;
Using System.Windows.Navigation;
Using System.Windows.Shapes;
Namespace WPFDrawingTraning
{
/// <summary>
/// Interaction logic of MainWindow.xaml
/// </summary>
Public partial class MainWindow : System.Windows.Window
{
//Canvas mainPanel = new Canvas();
Public MainWindow()
{
InitializeComponent();
Drawsin();//Draw 2D coordinate system and sin curve
Drawpentagon();
}
/// <summary>
/// draw a set of line segments
/// </summary>
Protected void Drawing()
{
PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = new Point(10, 50);
LineSegment myLineSegment = new LineSegment();
myLineSegment.Point = new Point(200, 70);
PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);
myPathFigure.Segments = myPathSegmentCollection;
PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);
PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = myPathGeometry;
// Add path shape to the UI.
StackPanel mainPanel = new StackPanel();
mainPanel.Children.Add(myPath);
this.Content = mainPanel;
}
/// <summary>
/// draw a line segment
/// </summary>
Protected void DrawingLine(Point startPt,Point endPt)
{
LineGeometry myLineGeometry = new LineGeometry();
myLineGeometry.StartPoint = startPt;
myLineGeometry.EndPoint = endPt;
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = myLineGeometry;
mainPanel.Children.Add(myPath);
}
/// <summary>
/// draw a star line
/// </summary>
Protected void DrawingAstroid(Point center,double r)
{
Double h1 = r * Math.Sin(18 * Math.PI / 180);
Double h2 = r * Math.Cos(18*Math.PI/180);
Double h3 = r * Math.Sin(36 * Math.PI / 180);
Double h4 = r * Math.Cos(36 * Math.PI / 180); ;
Point p1 = new Point(r, 0);
Point p2 = new Point(r - h2, r - h1);
Point p3 = new Point(r - h3, r + h4);
Point p4 = new Point(r + h3, p3.Y);
Point p5 = new Point(r + h2, p2.Y);
Point[] values = new Point[] { p1, p2, p3, p4, p5 };
PathFigureCollection myPathFigureCollection = new PathFigureCollection();
PathGeometry myPathGeometry = new PathGeometry();
For (int i = 0; i < values.Length; i++)
{
//DrawingLine(center, values[i]);
PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = center;
LineSegment myLineSegment = new LineSegment();
myLineSegment.Point = values[i];
PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);
myPathFigure.Segments = myPathSegmentCollection;
myPathFigureCollection.Add(myPathFigure);
}
myPathGeometry.Figures = myPathFigureCollection;
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = myPathGeometry;
mainPanel.Children.Add(myPath);
}
/// <summary>
/// Draw a five-pointed star
/// </summary>
Private void Drawpentagon()
{
Point center = new Point(50, 50);
Double r = 50;
DrawingAstroid(center, r);
Double h1 = r * Math.Sin(18 * Math.PI / 180);
Double h2 = r * Math.Cos(18 * Math.PI / 180);
Double h3 = r * Math.Sin(36 * Math.PI / 180);
Double h4 = r * Math.Cos(36 * Math.PI / 180); ;
Point p1 = new Point(r, 0);
Point p2 = new Point(r - h2, r - h1);
Point p3 = new Point(r - h3, r + h4);
Point p4 = new Point(r + h3, p3.Y);
Point p5 = new Point(r + h2, p2.Y);
Point[] values = new Point[] { p1, p3, p5, p2, p4 };
// Create a path to draw a geometry with.
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
StreamGeometry theGeometry = BuildRegularPolygon(values, true, false);
// Create a StreamGeometry to use to specify myPath.
theGeometry.FillRule = FillRule.EvenOdd;
// Freeze the geometry (make it unmodifiable)
// for additional performance benefits.
theGeometry.Freeze();
// Use the StreamGeometry returned by the BuildRegularPolygon to
// specify the shape of the path.
myPath.Data = theGeometry;
// Add path shape to the UI.
mainPanel.Children.Add(myPath);
}
/// <summary>
/// draw continuous segments
/// </summary>
/// <param name="values"></param>
/// <returns></returns>
Private StreamGeometry BuildRegularPolygon(Point[] values, bool isClosed, bool isfilled)
{
// c is the center, r is the radius,
// numSides the number of sides, offsetDegree the offset in Degrees.
// Do not add the last point.
StreamGeometry geometry = new StreamGeometry();
Using (StreamGeometryContext ctx = geometry.Open())
{
ctx.BeginFigure(values[0], isfilled /* is filled */, isClosed /* is closed */);
For (int i = 1; i < values.Length; i++)
{
ctx.LineTo(values[i], true /* is stroked */, false /* is smooth join */);
}
}
Return geometry;
}
/// <summary>
/// Draw a five-pointed star
/// </summary>
Private void Drawsin()
{
Point point = new Point(this.mainPanel.Width, this.mainPanel.Height);
Point xypoint = new Point(point.X / 2, point.Y / 2); / / new coordinate origin
//x axis coordinate starting point
Point xstartpoint = new Point(0, point.Y / 2);
//x-axis coordinate end point
Point xendpoint = new Point(point.X, point.Y / 2);
//y axis coordinate starting point
Point ystartpoint = new Point(point.X / 2, point.Y);
//y axis coordinate end point
Point yendpoint = new Point(point.X / 2, 0);
Line xline = new Line();
xline.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
xline.X1 = 0;
xline.Y1 = this.mainPanel.Height / 2;
xline.X2 = this.mainPanel.Width;
xline.Y2 = this.mainPanel.Height / 2;
this.mainPanel.Children.Add(xline);
Line yline = new Line();
yline.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
yline.X1 = this.mainPanel.Width / 2;
yline.Y1 = this.mainPanel.Height;
yline.X2 = this.mainPanel.Width / 2;
yline.Y2 = 0;
this.mainPanel.Children.Add(yline);
Point[] points=new Point[1000];
/ / Draw sin curve, starting from the origin (0,0)
Point zpoint = new Point(0, 0);
Zpoint = XYTransf(zpoint, xypoint);
Points[0] = zpoint;//the starting point of the sin curve
For (int i = 1; i < 1000; i++)
{
/ / Calculate sin (x, y)
point.X =10 * i;//x
point.Y =10 * Math.Sin(i);//y
/ / coordinate conversion
Point = XYTransf(point, xypoint);
Points[i] = point;
}
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
StreamGeometry theGeometry = BuildRegularPolygon(points, true, false);
// Create a StreamGeometry to use to specify myPath.
theGeometry.FillRule = FillRule.EvenOdd;
// Freeze the geometry (make it unmodifiable)
// for additional performance benefits.
theGeometry.Freeze();
// Use the StreamGeometry returned by the BuildRegularPolygon to
// specify the shape of the path.
myPath.Data = theGeometry;
// Add path shape to the UI.
mainPanel.Children.Add(myPath);
}
/ / The coordinates in the constructed XY coordinate system are converted to the interface coordinate system
Public Point XYTransf(Point point, Point xypoint)
{
point.X += xypoint.X;
point.Y = xypoint.Y - point.Y;
Return point; / / display the position of the screen coordinate system
}
}
}
Third, the page effect
C#WPF how to draw a Geometry diagram tutorial drawing a sin curve sine-drawn 2D coordinate system there's a diagram with the code