以 C# 形式绘制曲线的 X 轴和 y 轴
Drawing the X and y axis for a curved Line in C# Form
我是 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+ 图形缩放 window 大小(即你想用 [=35= 的大小缩放 X 和 Y 轴) ]), 对吧?
这很简单,您只需决定要在 window 中显示多大的 space —— 即,如果您想让轴从 0,0 开始在左上角,到右下角的 512x512,那么你只需要将 X 轴缩放 512/宽度,将 Y 轴缩放 512/高度。
因此,您可以通过执行 ScaleTransform on your Graphics object. You'll need to use your Form's ClientSize 来获取宽度和高度。 (常规 Form 的 .Width 和 .Height 属性包括所有边框和标题栏、填充像素等——因此不利于此计算。)
然后你需要强制一个 Invalidation during the form's Resize 事件(没有这个它会工作,当你把 window 变小,但是当你把它变大时,这将是必需的,否则它只会重绘边缘)。
另外一个值得考虑的是打开表格的DoubleBuffered属性,重绘会更顺畅
因此,假设您想要在 512x512 "pixels" 的虚拟 space 中工作,其中 0 ,0 始终位于左上角,512,512 位于右下角。您可以将此代码添加到 OnPaint 事件处理程序的顶部:
float scaleX = 512f / ((float)this.ClientSize.Width);
float scaleY = 512f / ((float)this.ClientSize.Height);
e.Graphics.ScaleTransform(scaleX, scaleY);
然后为表单的 Resize 事件添加处理程序并添加如下内容:
this.Invalidate(true);
这应该可以解决问题。
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+ 图形缩放 window 大小(即你想用 [=35= 的大小缩放 X 和 Y 轴) ]), 对吧?
这很简单,您只需决定要在 window 中显示多大的 space —— 即,如果您想让轴从 0,0 开始在左上角,到右下角的 512x512,那么你只需要将 X 轴缩放 512/宽度,将 Y 轴缩放 512/高度。
因此,您可以通过执行 ScaleTransform on your Graphics object. You'll need to use your Form's ClientSize 来获取宽度和高度。 (常规 Form 的 .Width 和 .Height 属性包括所有边框和标题栏、填充像素等——因此不利于此计算。)
然后你需要强制一个 Invalidation during the form's Resize 事件(没有这个它会工作,当你把 window 变小,但是当你把它变大时,这将是必需的,否则它只会重绘边缘)。
另外一个值得考虑的是打开表格的DoubleBuffered属性,重绘会更顺畅
因此,假设您想要在 512x512 "pixels" 的虚拟 space 中工作,其中 0 ,0 始终位于左上角,512,512 位于右下角。您可以将此代码添加到 OnPaint 事件处理程序的顶部:
float scaleX = 512f / ((float)this.ClientSize.Width);
float scaleY = 512f / ((float)this.ClientSize.Height);
e.Graphics.ScaleTransform(scaleX, scaleY);
然后为表单的 Resize 事件添加处理程序并添加如下内容:
this.Invalidate(true);
这应该可以解决问题。