簡體   English   中英

以C#形式繪制曲線的X和y軸

[英]Drawing the X and y axis for a curved Line in C# Form

X和Y軸指出我的圖表我的曲線 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來實現。 您需要使用FormClientSize來獲取寬度和高度。 (常規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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM