放大c#中的二次曲线
Zooming in on quadratic curve line in c#
我对 c# 比较陌生,我正在尝试使用 X 和 Y 图绘制二次曲线以进行缩放。我画的曲线虽然出现在屏幕的左上角,但很小,几乎看不出来。有没有办法放大我的曲线并将其对齐到中间以便正确显示?
protected override void OnPaint(PaintEventArgs e)
{
float a = 1, b = -3, c = -4;
double x1, x2, x3, y1, y2, y3, delta;
delta = (b * b) - (4 * a * c);
x1 = ((b * (-1)) + Math.Sqrt(delta)) / (2 * a);
y1 = a * (x1 * x1) + b * (x1) + c;
x2 = x1 + 1;
y2 = a * (x2 * x2) + b * (x2) + c;
x3 = x1 - 3;
y3 = a * (x3 * x3) + b * (x3) + 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);
Graphics g = e.Graphics;
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[] Points = { point1, point2, point3 };
g.DrawCurve(aPen, Points);
我建议您研究一下 Microsoft Chart controls,它有很多关于如何制作这种曲线的有趣功能以及参数化它们的能力。
A link 到它的更新版本:here
是的 通过使用 Graphics.TranslateTransform and Matrix and Graphics.MultiplyTransform 移动(翻译)和放大(缩放)Graphics
结果是可能的,甚至相当简单:
using System.Drawing.Drawing2D;
//..
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);
请注意,缩放就像镜头一样工作,会放大像素。因此,当您放大 Graphics
..
时,您可能希望缩小 Pen.Width
之前用过一个..
g.DrawEllipse(Pens.Blue, 11, 11, 55, 55);
..还有两个转换后..
g.DrawEllipse(Pens.Red, 11, 11, 55, 55);
using (Pen pen = new Pen(Color.Green, 1/factor))
g.DrawEllipse(pen, 11, 11, 44, 44);
..这些调用产生了这张图片:
(我改变了绿色圆圈的半径以避免完全重叠..)
移动和缩放所需的数字由您决定;这可能涉及找到所涉及点的最小值和最大值..
我对 c# 比较陌生,我正在尝试使用 X 和 Y 图绘制二次曲线以进行缩放。我画的曲线虽然出现在屏幕的左上角,但很小,几乎看不出来。有没有办法放大我的曲线并将其对齐到中间以便正确显示?
protected override void OnPaint(PaintEventArgs e)
{
float a = 1, b = -3, c = -4;
double x1, x2, x3, y1, y2, y3, delta;
delta = (b * b) - (4 * a * c);
x1 = ((b * (-1)) + Math.Sqrt(delta)) / (2 * a);
y1 = a * (x1 * x1) + b * (x1) + c;
x2 = x1 + 1;
y2 = a * (x2 * x2) + b * (x2) + c;
x3 = x1 - 3;
y3 = a * (x3 * x3) + b * (x3) + 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);
Graphics g = e.Graphics;
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[] Points = { point1, point2, point3 };
g.DrawCurve(aPen, Points);
我建议您研究一下 Microsoft Chart controls,它有很多关于如何制作这种曲线的有趣功能以及参数化它们的能力。
A link 到它的更新版本:here
是的 通过使用 Graphics.TranslateTransform and Matrix and Graphics.MultiplyTransform 移动(翻译)和放大(缩放)Graphics
结果是可能的,甚至相当简单:
using System.Drawing.Drawing2D;
//..
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);
请注意,缩放就像镜头一样工作,会放大像素。因此,当您放大 Graphics
..
Pen.Width
之前用过一个..
g.DrawEllipse(Pens.Blue, 11, 11, 55, 55);
..还有两个转换后..
g.DrawEllipse(Pens.Red, 11, 11, 55, 55);
using (Pen pen = new Pen(Color.Green, 1/factor))
g.DrawEllipse(pen, 11, 11, 44, 44);
..这些调用产生了这张图片:
(我改变了绿色圆圈的半径以避免完全重叠..)
移动和缩放所需的数字由您决定;这可能涉及找到所涉及点的最小值和最大值..