110,535
社区成员
发帖
与我相关
我的任务
分享
public class CoordinatePanel : Panel
{
public CoordinatePanel()
{
m_CoordinateData = new List<Coordinate>();
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
public List<Coordinate> CoordinateData
{
get { return m_CoordinateData; }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Draw axis
using (Pen pen = new Pen(Brushes.Black))
{
pen.EndCap = LineCap.ArrowAnchor;
e.Graphics.DrawLine(
pen,
new Point(m_Margin, this.Height / 2),
new Point(this.Width - m_Margin, this.Height / 2)
);
e.Graphics.DrawLine(
pen,
new Point(this.Width / 2, this.Height - m_Margin),
new Point(this.Width / 2, m_Margin)
);
}
// Draw data
if (m_CoordinateData.Count == 0)
return;
Point[] pointArray = new Point[m_CoordinateData.Count];
for (Int32 i = 0; i < m_CoordinateData.Count; i++)
{
Coordinate coordinate = m_CoordinateData[i];
Point p = new Point(
Convert.ToInt32(coordinate.X * m_XScale + this.Width / 2),
Convert.ToInt32(this.Height / 2 - coordinate.Y * m_YScale)
);
pointArray[i] = p;
}
e.Graphics.DrawLines(Pens.Black, pointArray);
}
private List<Coordinate> m_CoordinateData;
private Int32 m_Margin = 20;
// set the y scale, the result will be extend on Y axis
private Int32 m_YScale = 80;
// set the x scale, the result will be extend on Y axis
private Int32 m_XScale = 20;
}
public class Coordinate
{
public Coordinate(Double x, Double y)
{
m_X = x;
m_Y = y;
}
public Double X
{
get { return m_X; }
set { m_X = value; }
}
public Double Y
{
get { return m_Y; }
set { m_Y = value; }
}
private Double m_X;
private Double m_Y;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
for (Double i = -Math.PI; i <= Math.PI; i += 0.01)
{
Coordinate coordinate = new Coordinate(i, Math.Sin(i));
coordinatePanel1.CoordinateData.Add(coordinate);
}
}
}