在pictureBox中绘制图像,将坐标轴平移到中心(默认在pictureBox的左上角),并绘制坐标轴
--该段代码可以放在pictureBox的Paint事件里
Graphics myGraohics = e.Graphics;
//坐标系变换-先平移后缩放
myGraphics.TranslateTransform(pictureBox1.Width / 2, pictureBox1.Height / 2, MatrixOrder.Prepend); //平移变换
myGraphics.ScaleTransform(1, -1, MatrixOrder.Prepend); //继续进行缩放变换
//绘制平移、缩放后的坐标系,画笔采用蓝色-此时坐标原点已经到中心
Pen myPen = new Pen(Color.Black,3);
myPen.EndCap = LineCap.ArrowAnchor; //设置线段终点的样式
myGraphics.DrawLine(myPen, -pictureBox1.Width / 2, 0, pictureBox1.Width / 2, 0); //绘制X轴
myGraphics.DrawLine(myPen, 0, -pictureBox1.Height / 2, 0, pictureBox1.Height / 2); //绘制Y轴
#region 绘刻度线
myPen.EndCap = LineCap.NoAnchor;
int k = pictureBox1.Width / 50;
for (int i = 0; i <= k; i++) //X轴刻度线
{
myGraphics.DrawLine(myPen, 50 * i, 0, 50 * i, 15);
myGraphics.DrawLine(myPen, -50 * i, 0, -50 * i, 15);
}
k = pictureBox1.Height / 100;
for (int i = 0; i <= k; i++) //Y轴刻度线
{
myGraphics.DrawLine(myPen, 0, 50 * i, 10, 50 * i);
myGraphics.DrawLine(myPen, 0, -50 * i, 10, -50 * i);
}
#endregion