原文: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 中代码

  1. <Window x:Class="WPFDrawingTraning.MainWindow"
  2.         xmlns="<a target=_blank href="http://schemas.microsoft.com/winfx/2006/xaml/presentation">http://schemas.microsoft.com/winfx/2006/xaml/presentation</a>"
  3.         xmlns:x="<a target=_blank href="http://schemas.microsoft.com/winfx/2006/xaml">http://schemas.microsoft.com/winfx/2006/xaml</a>"
  4.         Title="MainWindow" Height="350" Width="525">
  5.     <Grid>
  6.         <Canvas Name="mainPanel" HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="517"/>
  7.     </Grid>
  8. </Window>
  9.  

MainWindow.xaml.cs中代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace WPFDrawingTraning
  16. {
  17. /// <summary>
  18. /// MainWindow.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class MainWindow : System.Windows.Window
  21. {
  22. //Canvas mainPanel = new Canvas();
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. Drawsin();//绘制2D坐标系和sin曲线
  27. Drawpentagon();
  28. }
  29. /// <summary>
  30. /// 绘制一组线段
  31. /// </summary>
  32. protected void Drawing()
  33. {
  34. PathFigure myPathFigure = new PathFigure();
  35. myPathFigure.StartPoint = new Point(10, 50);
  36. LineSegment myLineSegment = new LineSegment();
  37. myLineSegment.Point = new Point(200, 70);
  38. PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
  39. myPathSegmentCollection.Add(myLineSegment);
  40. myPathFigure.Segments = myPathSegmentCollection;
  41. PathFigureCollection myPathFigureCollection = new PathFigureCollection();
  42. myPathFigureCollection.Add(myPathFigure);
  43. PathGeometry myPathGeometry = new PathGeometry();
  44. myPathGeometry.Figures = myPathFigureCollection;
  45. Path myPath = new Path();
  46. myPath.Stroke = Brushes.Black;
  47. myPath.StrokeThickness = 1;
  48. myPath.Data = myPathGeometry;
  49. // Add path shape to the UI.
  50. StackPanel mainPanel = new StackPanel();
  51. mainPanel.Children.Add(myPath);
  52. this.Content = mainPanel;
  53. }
  54. /// <summary>
  55. /// 绘制线段
  56. /// </summary>
  57. protected void DrawingLine(Point startPt,Point endPt)
  58. {
  59. LineGeometry myLineGeometry = new LineGeometry();
  60. myLineGeometry.StartPoint = startPt;
  61. myLineGeometry.EndPoint = endPt;
  62. Path myPath = new Path();
  63. myPath.Stroke = Brushes.Black;
  64. myPath.StrokeThickness = 1;
  65. myPath.Data = myLineGeometry;
  66. mainPanel.Children.Add(myPath);
  67. }
  68. /// <summary>
  69. /// 绘制星状线
  70. /// </summary>
  71. protected void DrawingAstroid(Point center,double r)
  72. {
  73. double h1 = r * Math.Sin(18 * Math.PI / 180);
  74. double h2 = r * Math.Cos(18*Math.PI/180);
  75. double h3 = r * Math.Sin(36 * Math.PI / 180);
  76. double h4 = r * Math.Cos(36 * Math.PI / 180); ;
  77. Point p1 = new Point(r, 0);
  78. Point p2 = new Point(r - h2, r - h1);
  79. Point p3 = new Point(r - h3, r + h4);
  80. Point p4 = new Point(r + h3, p3.Y);
  81. Point p5 = new Point(r + h2, p2.Y);
  82. Point[] values = new Point[] { p1, p2, p3, p4, p5 };
  83. PathFigureCollection myPathFigureCollection = new PathFigureCollection();
  84. PathGeometry myPathGeometry = new PathGeometry();
  85. for (int i = 0; i < values.Length; i++)
  86. {
  87. //DrawingLine(center, values[i]);
  88. PathFigure myPathFigure = new PathFigure();
  89. myPathFigure.StartPoint = center;
  90. LineSegment myLineSegment = new LineSegment();
  91. myLineSegment.Point = values[i];
  92. PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
  93. myPathSegmentCollection.Add(myLineSegment);
  94. myPathFigure.Segments = myPathSegmentCollection;
  95. myPathFigureCollection.Add(myPathFigure);
  96. }
  97. myPathGeometry.Figures = myPathFigureCollection;
  98. Path myPath = new Path();
  99. myPath.Stroke = Brushes.Black;
  100. myPath.StrokeThickness = 1;
  101. myPath.Data = myPathGeometry;
  102. mainPanel.Children.Add(myPath);
  103. }
  104. /// <summary>
  105. /// 绘制坐标系和sin曲线
  106. /// </summary>
  107. private void Drawpentagon()
  108. {
  109. Point center = new Point(50, 50);
  110. double r = 50;
  111. DrawingAstroid(center, r);
  112. double h1 = r * Math.Sin(18 * Math.PI / 180);
  113. double h2 = r * Math.Cos(18 * Math.PI / 180);
  114. double h3 = r * Math.Sin(36 * Math.PI / 180);
  115. double h4 = r * Math.Cos(36 * Math.PI / 180); ;
  116. Point p1 = new Point(r, 0);
  117. Point p2 = new Point(r - h2, r - h1);
  118. Point p3 = new Point(r - h3, r + h4);
  119. Point p4 = new Point(r + h3, p3.Y);
  120. Point p5 = new Point(r + h2, p2.Y);
  121. Point[] values = new Point[] { p1, p3, p5, p2, p4 };
  122. // Create a path to draw a geometry with.
  123. Path myPath = new Path();
  124. myPath.Stroke = Brushes.Black;
  125. myPath.StrokeThickness = 1;
  126. StreamGeometry theGeometry = BuildRegularPolygon(values, true, false);
  127. // Create a StreamGeometry to use to specify myPath.
  128. theGeometry.FillRule = FillRule.EvenOdd;
  129. // Freeze the geometry (make it unmodifiable)
  130. // for additional performance benefits.
  131. theGeometry.Freeze();
  132. // Use the StreamGeometry returned by the BuildRegularPolygon to
  133. // specify the shape of the path.
  134. myPath.Data = theGeometry;
  135. // Add path shape to the UI.
  136. mainPanel.Children.Add(myPath);
  137. }
  138. /// <summary>
  139. /// 绘制连续的线段
  140. /// </summary>
  141. /// <param name="values"></param>
  142. /// <returns></returns>
  143. private StreamGeometry BuildRegularPolygon(Point[] values, bool isClosed,bool isfilled)
  144. {
  145. // c is the center, r is the radius,
  146. // numSides the number of sides, offsetDegree the offset in Degrees.
  147. // Do not add the last point.
  148. StreamGeometry geometry = new StreamGeometry();
  149. using (StreamGeometryContext ctx = geometry.Open())
  150. {
  151. ctx.BeginFigure(values[0], isfilled /* is filled */, isClosed /* is closed */);
  152. for (int i = 1; i < values.Length; i++)
  153. {
  154. ctx.LineTo(values[i], true /* is stroked */, false /* is smooth join */);
  155. }
  156. }
  157. return geometry;
  158. }
  159. /// <summary>
  160. /// 绘制五角星
  161. /// </summary>
  162. private void Drawsin()
  163. {
  164. Point point = new Point(this.mainPanel.Width, this.mainPanel.Height);
  165. Point xypoint = new Point(point.X / 2, point.Y / 2);//新坐标原点
  166. //x轴坐标起点
  167. Point xstartpoint = new Point(0, point.Y / 2);
  168. //x轴坐标终点
  169. Point xendpoint = new Point(point.X, point.Y / 2);
  170. //y轴坐标起点
  171. Point ystartpoint = new Point(point.X / 2, point.Y);
  172. //y轴坐标终点
  173. Point yendpoint = new Point(point.X / 2, 0);
  174. Line xline = new Line();
  175. xline.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
  176. xline.X1 = 0;
  177. xline.Y1 = this.mainPanel.Height / 2;
  178. xline.X2 = this.mainPanel.Width;
  179. xline.Y2 = this.mainPanel.Height / 2;
  180. this.mainPanel.Children.Add(xline);
  181. Line yline = new Line();
  182. yline.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
  183. yline.X1 = this.mainPanel.Width / 2;
  184. yline.Y1 = this.mainPanel.Height;
  185. yline.X2 = this.mainPanel.Width / 2;
  186. yline.Y2 = 0;
  187. this.mainPanel.Children.Add(yline);
  188. Point[] points=new Point[1000];
  189. //绘制sin曲线,从原点(0,0)开始
  190. Point zpoint = new Point(0, 0);
  191. zpoint = XYTransf(zpoint, xypoint);
  192. points[0] = zpoint;//sin曲线的起点
  193. for (int i = 1; i < 1000; i++)
  194. {
  195. //计算sin(x,y)
  196. point.X =10 * i;//x
  197. point.Y =10 * Math.Sin(i);//y
  198. //坐标转换
  199. point = XYTransf(point, xypoint);
  200. points[i] = point;
  201. }
  202. Path myPath = new Path();
  203. myPath.Stroke = Brushes.Black;
  204. myPath.StrokeThickness = 1;
  205. StreamGeometry theGeometry = BuildRegularPolygon(points, true, false);
  206. // Create a StreamGeometry to use to specify myPath.
  207. theGeometry.FillRule = FillRule.EvenOdd;
  208. // Freeze the geometry (make it unmodifiable)
  209. // for additional performance benefits.
  210. theGeometry.Freeze();
  211. // Use the StreamGeometry returned by the BuildRegularPolygon to
  212. // specify the shape of the path.
  213. myPath.Data = theGeometry;
  214. // Add path shape to the UI.
  215. mainPanel.Children.Add(myPath);
  216. }
  217. //构建的XY坐标系中的坐标转换为界面坐标系
  218. public Point XYTransf(Point point, Point xypoint)
  1. {
  2. point.X += xypoint.X;
  3. point.Y = xypoint.Y - point.Y;
  4. return point;//显示屏幕坐标系的位置
  5. }
  6. }
  7. }

三、页面效果

 

四、介绍

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)

  1. {
  2. point.X += xypoint.X;
  3. point.Y = xypoint.Y - point.Y;
  4. return point;//显示屏幕坐标系的位置
  5. }

 

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

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

                                                                                           图 坐标点转换

 

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

  1. for (int i = 1; i < 1000; i++)
  2. {
  3. //计算sin(x,y)
  4. point.X =10 * i;//sin x
  5. point.Y =10 * Math.Sin(i);//sin y
  6. //坐标转换
  7. point = XYTransf(point, xypoint);
  8. points[i] = point;
  9. }

4.连接1000个sin(x,y)的屏幕坐标点,并显示在Canvas中

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

  1. Path myPath = new Path();
  2. myPath.Stroke = Brushes.Black;
  3. myPath.StrokeThickness = 1;
  4. StreamGeometry theGeometry = BuildRegularPolygon(points, true, false);
  5. // Create a StreamGeometry to use to specify myPath.
  6. theGeometry.FillRule = FillRule.EvenOdd;
  7. // Freeze the geometry (make it unmodifiable)
  8. // for additional performance benefits.
  9. theGeometry.Freeze();
  10. // Use the StreamGeometry returned by the BuildRegularPolygon to
  11. // specify the shape of the path.
  12. myPath.Data = theGeometry;
  13. // Add path shape to the UI.
  14. mainPanel.Children.Add(myPath);

5.执行显示效果

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

 

 

C#WPF 如何绘制几何图形 图示教程 绘制sin曲线 正弦 绘制2D坐标系 有图有代码的更多相关文章

  1. 最全Pycharm教程(43)——Pycharm扩展功能之UML类图使用 代码结构

    版权声明:本文为博主原创文章,转载时麻烦注明源文章链接,谢谢合作 https://blog.csdn.net/u013088062/article/details/50353202 1.什么是UML ...

  2. Cocos2D中使用CCDrawNode绘制几何图形崩溃的解决

    在cocos2D v3.x中已经不能像在v2.x中那样直接调用ccDrawXXX函数来绘制几何图形了. 我们可以使用CCDrawNode或者CCRenderer来绘制图形. 但是官方的Api手册中说的 ...

  3. Win10系列:VC++绘制几何图形2

    新建了Direct2D中的资源后,接下来初始化用于绘制图形的应用窗口.在解决方案资源管理器窗口中右键点击项目图标,在弹出的菜单栏中选中"添加", 并在"添加"的 ...

  4. Win10系列:VC++绘制几何图形1

    本小节主要介绍如何使用Direct2D来绘制几何图形,其中会使用到FillGeometry函数和FillEllipse函数,FillGeometry函数用于填充几何图形的内部区域,而FillEllip ...

  5. HTML5绘制几何图形

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  6. OpenGL入门学习 课程 (三) 绘制几何图形的一些细节问题

    http://oulehui.blog.163.com/blog/static/79614698201191832753312/ 先回顾一下我们都学习了些什么: 第一课,编写第一个OpenGL程序第二 ...

  7. HTML5实现绘制几何图形

    HTML5新增了一个<canvas.../>属性.该元素自身并不绘制图形,只是相当于一张空画布.如果开发者需要向<canvas.../>上绘制图形则必须使用JavaScript ...

  8. Shadertoy 教程 Part 5 - 运用SDF绘制出更多的2D图形

    Note: This series blog was translated from Nathan Vaughn's Shaders Language Tutorial and has been au ...

  9. WPF入门(三)->几何图形之不规则图形(PathGeometry) (2)

    原文:WPF入门(三)->几何图形之不规则图形(PathGeometry) (2) 上一节我们介绍了PathGeometry中LineSegment是点与点之间绘制的一条直线,那么我们这一节来看 ...

随机推荐

  1. ArcGIS 中要素的查询与修改

    转自nimeila的回答 求C# ArcGIS Engine修改选中要素的属性,单要素都行 RLAlterFrm RLalter = new RLAlterFrm(); RLalter.ShowDia ...

  2. [机器学习] Coursera ML笔记 - 逻辑回归(Logistic Regression)

    引言 机器学习栏目记录我在学习Machine Learning过程的一些心得笔记,涵盖线性回归.逻辑回归.Softmax回归.神经网络和SVM等等.主要学习资料来自Standford Andrew N ...

  3. 借助gdb实现pstack

    pstack.sh: #! /bin/sh if [ -z $1 ] then echo "gdb script for print stack" echo "usage ...

  4. Java基本数据类型的取值范围

    版权声明:本文为博主原创文章,未经博主允许不得转载. 先看一段代码public class Hello{    public static void main(String[] args){      ...

  5. 机器学习:Softmax Classifier (两个隐含层)

    程序实现 softmax classifier, 含有两个隐含层的情况.activation function 是 ReLU : f(x)=max(0,x) f1=w1x+b1 h1=max(0,f1 ...

  6. 如何在一个div中使其子div居中

    网上其他地方已讲述过对其的不同实现方式,今天主要做一个详细的汇总,希望对大家有帮助. ps:我面试的时候就被问到过这个问题,当时都回答错了,蓝瘦. 假设父div的类名为father,子div的类名为s ...

  7. java--css+js做的树形菜单(完整版)

    jsp页面: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8 ...

  8. SQLite编码

    •SQLite编码 •讲师:李明杰 •技术博客:http://www.cnblogs.com/mjios •SQLite3 •在iOS中使用SQLite3,首先要添加库文件libsqlite3.dyl ...

  9. 【u204】高级砝码称重

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 现有n个砝码,重量分别为a1,a2,a3,--,an,在去掉m个砝码后,问最多能称量出多少不同的重量( ...

  10. 《iOS开发全然上手——使用iOS 7和Xcode 5开发移动与平板应用》之Objective-C新手训练营

    编写Hello World应用程序通常被觉得,是学习不论什么编程语言的第一步.在这一章,你将创建iOS版的Hello World应用程序作为起步,高速了解Xcode这个开发iOS应用程序的主要工具. ...