# C#WPF 如何绘制几何图形 图示教程 绘制sin曲线 正弦 绘制2D坐标系 有图有代码...

C#WPF 如何绘制几何图形？ 怎么绘制坐标系？绘制sin曲线（正弦曲线）？

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>



MainWindow.xaml.cs中代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;

namespace WPFDrawingTraning
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : System.Windows.Window
{
//Canvas mainPanel = new Canvas();
public MainWindow()
{
InitializeComponent();

Drawsin();//绘制2D坐标系和sin曲线

Drawpentagon();

}
/// <summary>
/// 绘制一组线段
/// </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();

myPathFigure.Segments = myPathSegmentCollection;

PathFigureCollection myPathFigureCollection = new PathFigureCollection();

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();
this.Content = mainPanel;

}
/// <summary>
/// 绘制线段
/// </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;

}
/// <summary>
/// 绘制星状线
/// </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();

myPathFigure.Segments = myPathSegmentCollection;

}
myPathGeometry.Figures = myPathFigureCollection;
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = myPathGeometry;

}

/// <summary>
/// 绘制坐标系和sin曲线
/// </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)
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.

}

/// <summary>
/// 绘制连续的线段
/// </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>
/// 绘制五角星
/// </summary>
private void Drawsin()
{
Point point = new Point(this.mainPanel.Width, this.mainPanel.Height);
Point xypoint = new Point(point.X / 2, point.Y / 2);//新坐标原点

//x轴坐标起点
Point xstartpoint = new Point(0, point.Y / 2);
//x轴坐标终点
Point xendpoint = new Point(point.X, point.Y / 2);

//y轴坐标起点
Point ystartpoint = new Point(point.X / 2, point.Y);
//y轴坐标终点
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;

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;

Point[] points=new Point[1000];

//绘制sin曲线,从原点（0,0）开始
Point zpoint = new Point(0, 0);
zpoint = XYTransf(zpoint, xypoint);
points[0] = zpoint;//sin曲线的起点

for (int i = 1; i < 1000; i++)
{
//计算sin（x，y）
point.X =10 * i;//x
point.Y =10 * Math.Sin(i);//y

//坐标转换
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)
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.

}

//构建的XY坐标系中的坐标转换为界面坐标系
public Point XYTransf(Point point, Point xypoint)
        {
point.X += xypoint.X;
point.Y = xypoint.Y - point.Y;

return point;//显示屏幕坐标系的位置
}
}
}


private void Drawsin()  函数中完成：坐标系绘制，sin曲线绘制；

point是绘图坐标系中的点，xypoint（maincanvas.Width/2,maincanvas.Height/2）是绘图屏幕坐标的几何中心点（ 图 坐标点转换，中x轴和y轴原点）的坐标。

public Point XYTransf(Point point, Point xypoint）函数返回值是在屏幕坐标绘制点的坐标。

//转换为界面坐标系
public Point XYTransf(Point point, Point xypoint）

        {
point.X += xypoint.X;
point.Y = xypoint.Y - point.Y;

return point;//显示屏幕坐标系的位置
}

1.mainPanel  是一个Canvas面板，我们在该面板绘制图形。

2.绘制坐标系，以mainPanel 的图形中心为坐标原点；

图 坐标点转换

3.计算sin（x，y）并转换为屏幕坐标点，取1000个坐标点，并存在points数组中

            for (int i = 1; i < 1000; i++)
{
//计算sin（x，y）
point.X =10 * i;//sin x
point.Y =10 * Math.Sin(i);//sin y

//坐标转换
point = XYTransf(point, xypoint);
points[i] = point;

}

4.连接1000个sin（x，y）的屏幕坐标点，并显示在Canvas中

StreamGeometry theGeometry = BuildRegularPolygon(points, true, false);   通过该函数连接points中所有的点；

            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)
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);

5.执行显示效果

posted on 2018-09-26 09:03 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

• 0
点赞
• 3
收藏
• 0
评论
04-23
11-20 4044
11-03 1620
03-25 1754
07-23 2921
09-08 50
01-02 1万+
08-22 81
08-10
05-08
08-24 7147
01-02 1842

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。