使用C#绘制光谱反射率曲线

花了三四个小时,终于还算把曲线画出来了。

大致过程是:

首先从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的第二列数据非常接近所以曲线效果不太明显):

完!


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值