C#绘制等值线(源码)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
下面是C#随机数格点值等值线绘制案例(包含源码),希望对大家有些帮助!
源码:
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyContour
{
public partial class frmMain : Form
{
#region属性
public static bool showPoint = false;
public static bool drawLines = true;
private float[,] _Data;
///
///等值线原始格点数据
///
public float[,] Data
{
get { return _Data; }
set { _Data = value; }
}
private float[] _ContourValues;
///
///等值线值数组
///
public float[] ContourValues
{
get { return _ContourValues; }
set { _ContourValues = value; }
}
#endregion
#region窗体所有操作
public frmMain()
{
InitializeComponent();
MakeTestData();
setColor();
gg = pictureBox1.CreateGraphics();
}
private void button1_Click(object sender, EventArgs e) {
this.timer1.Enabled = false;
initValue(); //重新初始化绘制路径的数组
MakeTestData();
pictureBox1.Refresh();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (this.Data == null) return;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Pen pen = new Pen(Color.DarkGray, 1);
//绘制等值线
if (drawLines)
{
this.plotContour(e.Graphics, pictureBox1.Size);
}
//绘制网格
if (frmMain.showPoint)
{
int ww = pictureBox1.Width / (Data.GetLength(0) - 1); //x轴间隔长度
int yy = pictureBox1.Height / (Data.GetLength(1) - 1);//y轴间隔长度
//绘制网格横竖线
for (int p = 0; p < Data.GetLength(0); p++)
{
e.Graphics.DrawLine(pen, new Point(p * ww, 0), new Point((p) * ww, pictureBox1.Height));
}
for (int k = 0; k < Data.GetLength(1); k++)
{
e.Graphics.DrawLine(pen, new Point(0, k * yy), new Point(pictureBox1.Width, k * yy));
}
//绘制网格斜线
for (int j = 0; j < Data.GetLength(0) - 1; j++)
{
for (int i = 1; i < Data.GetLength(1); i++)
{
e.Graphics.DrawLine(pen, new Point(j * ww, (i - 1) * yy), new Point((j + 1) * ww, i * yy));
e.Graphics.DrawLine(pen, new Point(j * ww, i * yy), new Point((j + 1) * ww, (i - 1) * yy));
}
}
//绘制格点值
for (int j = 0; j < Data.GetLength(1); j++)
{
for (int i = 0; i < Data.GetLength(0); i++)
{
e.Graphics.DrawString(Data[i, j].ToString("f0"), DefaultFont, Brushes.Blue, i * pictureBox1.Width / (Data.GetLength(0) - 1), j * pictureBox1.Height / (Data.GetLength(1) - 1), sf);
}
}
}
}
private void pictureBox1_SizeChanged(object sender, EventArgs e)
{
pictureBox1.Refresh();
}
private void setColor()
{
label1.BackColor = getSpectrumColor(0f);
label2.BackColor = getSpectrumColor(1f);
label3.BackColor = getSpectrumColor(2f);
label4.BackColor = getSpectrumColor(3f);
label5.BackColor = getSpectrumColor(4f);
label6.BackColor = getSpectrumColor(5f);
label7.BackColor = getSpectrumColor(6f);
label8.BackColor = getSpectrumColor(7f);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
frmMain.showPoint = checkBox1.Checked;
pictureBox1.Refresh();
}
//生成路径按钮
private void button2_Click(object sender, EventArgs e)
{
drawLines = false;
this.pictureBox1.Refresh();
nums = 0;
if (drawValues[nums].color != null)
{