[英]Drawing the X and y axis for a curved Line in C# Form
ii是c#的新手,它试图在c#中画一条曲线。 我想问问有没有可能创建X和Y轴的方法,以显示曲线各点的坐标。 请帮我解决这个问题,因为我坚持如何执行它。
protected override void OnPaint(PaintEventArgs e)
{
float a = 1, b = 5, c = 1;
double x1, x2, x3,x4,x5,x6, y1, y2, y3,y4,y5, delta;
delta = (b * b) - (4 * a * c);
x1=0;
y1 = a * (x1 * x1) + (b * (x1)) + c;
x2 = 3;
y2 = a * (x2 * x2) + (b * (x2)) + c;
x3 = - 3;
y3 = a * (x3 * x3) + (b * (x3)) + c;
x4 = 5;
y4 = a * (x4 * x4) + (b * (x4)) + c;
x5 = -10;
y5 = a * (x5 * x5) + (b * (x5)) + c;
int cx1 = Convert.ToInt32(x1);
int cx2 = Convert.ToInt32(x2);
int cx3 = Convert.ToInt32(x3);
int cy1 = Convert.ToInt32(y1);
int cy2 = Convert.ToInt32(y2);
int cy3 = Convert.ToInt32(y3);
int cx4 = Convert.ToInt32(x4);
int cy4 = Convert.ToInt32(y4);
int cx5 = Convert.ToInt32(x5);
int cy5 = Convert.ToInt32(y5);
Graphics g = e.Graphics;
int deltaX = 100;
int deltaY = 100;
g.TranslateTransform(deltaX, deltaY);
float factor = 2.5f;
Matrix m = new Matrix();
m.Scale(factor, factor);
g.MultiplyTransform(m);
Pen aPen = new Pen(Color.Blue, 1);
Point point1 = new Point(cx1, cy1);
Point point2 = new Point(cx2, cy2);
Point point3 = new Point(cx3, cy3);
Point point4 = new Point(cx4, cy4);
Point point5 = new Point(cx5, cy5);
Point[] Points = { point5, point3, point1,point2,point4 };
g.DrawCurve(aPen, Points);
也许我误会了您,但听起来您想使GDI +图形随窗口大小缩放(即,您想随窗口大小缩放X和Y轴),对吗?
这非常简单,您只需要确定要在窗口中显示的空间大小即可-例如,如果要使轴从左上角的0,0到右下角的512x512,则您只需要将X轴缩放为512 /宽度,将Y轴缩放为512 /高度。
因此,您可以通过对Graphics对象执行ScaleTransform来实现。 您需要使用Form的ClientSize来获取宽度和高度。 (常规Form的.Width和.Height属性包括所有边框和标题栏,填充像素等-因此此计算无益。)
然后,您将需要在窗体的Resize事件期间强制执行Invalidation(在没有此功能的情况下可以使用,当您使窗口变小时,但是当您将其变大时,这将是必需的,否则它将仅重画边缘)。
值得考虑的另一件事是打开窗体的DoubleBuffered属性,重绘会更加平滑。
因此,假设您要在512x512“像素”的虚拟空间中工作,其中0,0始终是左上角,而512,512是右下角。 您可以将以下代码添加到OnPaint事件处理程序的顶部:
float scaleX = 512f / ((float)this.ClientSize.Width);
float scaleY = 512f / ((float)this.ClientSize.Height);
e.Graphics.ScaleTransform(scaleX, scaleY);
然后为Form的Resize事件添加一个处理程序,并添加如下内容:
this.Invalidate(true);
这应该够了吧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.