2015-02-11 66 views
0

我對c#比較新,我試圖繪製帶有X和Y圖形的二次曲線來進行縮放。我繪製的曲線雖然出現在屏幕的左上角,但非常小且幾乎不顯眼。是否有可能擴大我的曲線並將其對齊到中間以便正確顯示?在c中放大二次曲線line#

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); 
+0

如果你可以在這裏附加你想要的圖像會更好嗎? – 2015-02-11 04:39:24

回答

2

這是可能的,甚至相當簡單到兩個移動(翻譯)和放大(Scale)的Graphics結果通過Graphics.TranslateTransformMatrixGraphics.MultiplyTransform

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); 

。 。這些調用導致此圖像:

enter image description here

(我已經改變了綠色圓圈的半徑以避免完全重疊..)

這將由您來找到移動和縮放所需的數字;這可能涉及找到有關點的最小值和最大值。