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

简介: 原文:C#WPF 如何绘制几何图形 图示教程 绘制sin曲线 正弦 绘制2D坐标系 有图有代码 C#WPF 如何绘制几何图形? 怎么绘制坐标系?绘制sin曲线(正弦曲线)? 这离不开Path(System.Windows.Shapes)和StreamGeometry(System.Windows.Media)类。
原文: C#WPF 如何绘制几何图形 图示教程 绘制sin曲线 正弦 绘制2D坐标系 有图有代码

C#WPF 如何绘制几何图形? 怎么绘制坐标系?绘制sin曲线(正弦曲线)?

这离不开Path(System.Windows.Shapes)和StreamGeometry(System.Windows.Media)类。

完成该工程,我们首先要建立并绘制一个坐标系,然后在该坐标系中绘制sin曲线的点(x,y),最后,把曲线的点转换为屏幕坐标并连接;这样坐标系和sin曲线就绘制完成了。

 代码下载:http://download.csdn.net/detail/wyx100/8320225

如果有帮助,别忘了给评价!

 

一、建立WPF工程  

 

 

二、添加代码

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.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>
    /// 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();
            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>
        /// 绘制线段
        /// </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>
        /// 绘制星状线
        /// </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>
        /// 绘制坐标系和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)
            // 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>
        /// 绘制连续的线段
        /// </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;

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

            //绘制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)
            // 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);

        }

        //构建的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)
            // 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);

5.执行显示效果

点击“启动”或按键盘“F5”执行工程,显示界面。



 

 

目录
相关文章
|
1月前
|
缓存 C# Windows
C#程序如何编译成Native代码
【10月更文挑战第15天】在C#中,可以通过.NET Native和第三方工具(如Ngen.exe)将程序编译成Native代码,以提升性能和启动速度。.NET Native适用于UWP应用,而Ngen.exe则通过预编译托管程序集为本地机器代码来加速启动。不过,这些方法也可能增加编译时间和部署复杂度。
|
1月前
|
开发框架 NoSQL MongoDB
C#/.NET/.NET Core开发实战教程集合
C#/.NET/.NET Core开发实战教程集合
|
1月前
|
C#
C# 图形验证码实现登录校验代码
C# 图形验证码实现登录校验代码
76 2
C# WPF 中 外部图标引入iconfont,无法正常显示问题 【小白记录】
本文介绍了在C# WPF应用程序中引入外部iconfont图标时可能遇到的显示问题及其解决方法:1) 检查资源路径和引入格式是否正确,确保字体文件引用格式为“#xxxx”,并正确指向字体文件位置;2) 确保图标资源被包含在程序集中,通过设置字体文件的生成操作为Resource(资源)来实现。
C# WPF 中 外部图标引入iconfont,无法正常显示问题 【小白记录】
|
2月前
|
编解码 C# 数据库
C# + WPF 音频播放器 界面优雅,体验良好
【9月更文挑战第18天】这是一个用 C# 和 WPF 实现的音频播放器示例,界面简洁美观,功能丰富。设计包括播放/暂停按钮、进度条、音量控制滑块、歌曲列表和专辑封面显示。功能实现涵盖音频播放、进度条控制、音量调节及歌曲列表管理。通过响应式设计、动画效果、快捷键支持和错误处理,提升用户体验。可根据需求扩展更多功能。
120 3
|
1月前
|
中间件 数据库连接 API
C#数据分表核心代码
C#数据分表核心代码
36 0
|
2月前
|
设计模式 C# 开发者
C#设计模式入门实战教程
C#设计模式入门实战教程
|
3月前
|
C# Windows 开发者
当WPF遇见OpenGL:一场关于如何在Windows Presentation Foundation中融入高性能跨平台图形处理技术的精彩碰撞——详解集成步骤与实战代码示例
【8月更文挑战第31天】本文详细介绍了如何在Windows Presentation Foundation (WPF) 中集成OpenGL,以实现高性能的跨平台图形处理。通过具体示例代码,展示了使用SharpGL库在WPF应用中创建并渲染OpenGL图形的过程,包括开发环境搭建、OpenGL渲染窗口创建及控件集成等关键步骤,帮助开发者更好地理解和应用OpenGL技术。
247 0
|
3月前
|
开发者 C# 容器
【独家揭秘】当WPF邂逅DirectX:看这两个技术如何联手打造令人惊艳的高性能图形渲染体验,从环境搭建到代码实践,一步步教你成为图形编程高手
【8月更文挑战第31天】本文通过代码示例详细介绍了如何在WPF应用中集成DirectX以实现高性能图形渲染。首先创建WPF项目并使用SharpDX作为桥梁,然后在XAML中定义承载DirectX内容的容器。接着,通过C#代码初始化DirectX环境,设置渲染逻辑,并在WPF窗口中绘制图形。此方法适用于从简单2D到复杂3D场景的各种图形处理需求,为WPF开发者提供了高性能图形渲染的技术支持和实践指导。
226 0
|
3月前
|
API C# 开发者
WPF图形绘制大师指南:GDI+与Direct2D完美融合,带你玩转高性能图形处理秘籍!
【8月更文挑战第31天】GDI+与Direct2D的结合为WPF图形绘制提供了强大的工具集。通过合理地使用这两种技术,开发者可以创造出性能优异且视觉效果丰富的WPF应用程序。在实际应用中,开发者应根据项目需求和技术背景,权衡利弊,选择最合适的技术方案。
150 0