使用C#绘制光谱反射率曲线
来源:互联网 发布:shake it 下载 编辑:程序博客网 时间:2024/06/08 06:30
花了三四个小时,终于还算把曲线画出来了。
大致过程是:
首先从txt文件里取出数据,另存进其他变量里。
其次就是指定自己所要绘制曲线的坐标系的原点,以及规划好坐标轴的刻度等。
然后就是要考虑数据情况,坐标系变换等
最后就可以实现在form上绘制曲线了,过程比较繁琐,细节较多。
代码如下:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Diagnostics;using GDIDemo;using System.Drawing.Drawing2D;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } int[] XAxis = new int[601]; double[] YAxis = new double[601]; private void buttonX2_Click(object sender, EventArgs e) { //数据准备,读取txt文件 StreamReader sr = new StreamReader("E:\\1021grass2003-0001-filtered.txt", Encoding.Default); String line; string[] temp1 = new string[20]; string[] temp2 = new string[5]; string[] info1 = new string[601]; string[] info2 = new string[601]; int i = 0, j = 0; while ((line = sr.ReadLine()) != null) { temp1 = line.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//去掉Split出来的空白字符串 info1[i] = temp1[0]; info2[j] = temp1[1]; XAxis[i] = Convert.ToInt32(temp1[0]); YAxis[j] = Convert.ToDouble(temp1[1]); i++; j++; } //foreach (int iii in XAxis) // Debug.WriteLine(iii); //Debug.WriteLine(XAxis.Length); //创建Graphics对象 Graphics myGraphic = this.CreateGraphics(); //确定坐标系原点 Point centerPoint = new Point(250, 350); PointF[] dataPoint1 = new PointF[601]; Font font = new Font("宋体", 12); //重新new了一只笔,可以自己设置一些更详细的属性 Pen pen = new Pen(Color.Black, 1); pen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor; //画x轴 myGraphic.DrawLine(pen, centerPoint, new Point(centerPoint.X + 500, centerPoint.Y)); //画X轴的刻度,80像素为单位 for (int ix = 0; ix < 6; ix++) { myGraphic.DrawLine(Pens.Red, new Point(centerPoint.X + (ix + 1) * 80, centerPoint.Y), new Point(centerPoint.X + (ix + 1) * 80, centerPoint.Y - 5)); //绘制x轴显示的汉字“nm” myGraphic.DrawString(((ix+1)*200).ToString(), font, Brushes.Black, new PointF((centerPoint.X + (ix + 1) * 80) - 10, centerPoint.Y + 3)); } myGraphic.DrawString("nm", font, Brushes.Black, new PointF(760, 350)); //画Y轴,刻度以20像素为单位 myGraphic.DrawLine(pen, centerPoint, new Point(centerPoint.X, 130)); for (int iy = 0; iy < 10; iy++) { myGraphic.DrawLine(Pens.Black, new Point(centerPoint.X, centerPoint.Y - (iy + 1) * 20), new Point(centerPoint.X + 5, centerPoint.Y - (iy + 1) * 20)); //绘制y轴显示的数字0.1、0.2、0.3、、、、、、、 myGraphic.DrawString(string.Format("{0}", (iy + 1) * 0.1), font, Brushes.Black, new PointF(centerPoint.X - 40, (centerPoint.Y - (iy + 1) * 20) - 5)); } //计算光谱曲线对应的坐标点 for (int iz = 0; iz < YAxis.Length; iz++) { //float y = centerPoint.Y - (float)(YAxis[iz] * 2.5); //float x = centerPoint.X + (iz + 1) * 1000; PointF point = new PointF((float)XAxis[iz], (float)YAxis[iz]); dataPoint1[iz] = point; } //Debug.WriteLine(dataPoint.Length); //foreach (PointF myp in dataPoint) // Debug.WriteLine(myp); GraphicsPath path = new GraphicsPath(); //绘制光谱反射曲线的601个点 //坐标转换 PointF[] dataPoint = new PointF[601]; for (int ic = 0; ic < dataPoint1.Length; ic++) { PointF point = new PointF((float)(dataPoint1[ic].X/200*80+250), (float)(350-dataPoint1[ic].Y/0.1*20)); dataPoint[ic] = point; Debug.WriteLine(dataPoint[ic]); //myGraphic.DrawRectangle(Pens.Black, (int)dataPoint[i].X, (int)dataPoint[i].Y, 2, 2); path.AddRectangle(new RectangleF(dataPoint[ic], new SizeF(2, 2))); } path.AddLines(dataPoint); myGraphic.DrawPath(Pens.Blue, path); font.Dispose(); pen.Dispose(); } }}
其中,txt文件内容如下:
……………
绘制效果(因为txt的第二列数据非常接近所以曲线效果不太明显):
完!
0 0
- 使用C#绘制光谱反射率曲线
- C# 曲线绘制
- C#绘制实时曲线
- 高光谱遥感图像处理(9)-----ENVI使用教程之光谱曲线界面修改
- C# 使用“抗锯齿”功能绘制直线和曲线
- c#画笔Pen绘制曲线
- C#绘制圆柱和曲线
- C# Winform绘制动态曲线
- 串口绘制曲线(基于C#绘制曲线类)
- 使用GDI绘制简单曲线
- 使用R绘制ROC曲线
- 使用Qwt绘制历史曲线
- c#画笔Pen绘制光滑模式曲线
- Python 处理遥感图像:光谱辐射定标、大气校正和计算反射率
- 如何使用MATLAB绘制平滑曲线
- 使用html5,鼠标绘制贝赛尔曲线
- HTML5开发实例-使用canvas绘制曲线
- 使用CAShapeLayer和贝塞尔曲线绘制进度条
- 听说可以用Word发博客了
- 关于C++ const 的全面总结
- unity之属性封装,继承
- GUID格式化
- hdu 5125 magic balls
- 使用C#绘制光谱反射率曲线
- HDU 5025 Saving Tang Monk(状压搜索)
- 高版本jquery判断checkbox是否被选中问题
- [SDOI2011]工作安排
- Android创建Menu菜单的一种方法:利用MenuInflater-2014年12月3日
- 博客学习日记-2014/12/03
- JAVA笔记:前言
- 给对话框添加菜单 工具栏 状态栏简易方法
- JAVA爬虫 WebCollector