2011-02-23 43 views
2

我正在处理与频率为M重复的数据(像正弦/余弦波等东西)。我已经写了一个简单的显示控件,它将数据和绘制连接的线条绘制成代表数据的数据一张漂亮的照片。图形转换

我的问题是,如果在位图上绘制数据,那么象限1数据位于象限3,而象限2数据位于象限4(反之亦然)。

位图的宽度为M,高度为array.Max - array.Min

是否有一个简单的变换来更改数据,以便它显示在适当的象限中?

回答

3

思考它的一个好方法是,(0,0)在世界上,而(宽度/ 2,高度/ 2)的图像坐标之间

(0,0), (width, 0), (0,height), (width, height)

坐标被划分。

从那里,改造将是:

Data(x,y) => x = ABS(x - (width/2)), y = ABS(y - (Height/2)) 
2

尝试使用反转

g.ScaleTransform(1, -1); 
+0

我不熟悉ScaleTransform。我会研究它。如果我能把你抬高,我会!但我需要x和y轴。 Joe在解释它的时候做得比我更好 – 2011-02-23 22:45:02

3

Graphics.ScaleTransform是不是一个好主意y轴,因为它不仅会影响布局,但也绘制自己(笔划厚度,文字等)。

我建议你准备点列表,然后使用Matrix类对它们进行转换。这是我为你做的一个小例子,希望它会有所帮助。

private PointF[] sourcePoints = GenerateFunctionPoints(); 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 

     e.Graphics.Clear(Color.Black); 

     // Wee need to perform transformation on a copy of a points array. 
     PointF[] points = (PointF[])sourcePoints.Clone(); 

     // The way to calculate width and height of our drawing. 
     // Of course this operation may be performed outside this method for better performance. 
     float drawingWidth = points.Max(p => p.X) - points.Min(p => p.X); 
     float drawingHeight = points.Max(p => p.Y) - points.Min(p => p.Y); 

     // Calculate the scale aspect we need to apply to points. 
     float scaleAspect = Math.Min(ClientSize.Width/drawingWidth, ClientSize.Height/drawingHeight); 

     // This matrix transofrmation allow us to scale and translate points so the (0,0) point will be 
     // in the center of the screen. X and Y axis will be scaled to fit the drawing on the screen. 
     // Also the Y axis will be inverted. 
     Matrix matrix = new Matrix(); 
     matrix.Scale(scaleAspect, -scaleAspect); 
     matrix.Translate(drawingWidth/2, -drawingHeight/2); 

     // Perform a transformation and draw curve using out points. 
     matrix.TransformPoints(points); 
     e.Graphics.DrawCurve(Pens.Green, points); 
    } 

    private static PointF[] GenerateFunctionPoints() 
    { 
     List<PointF> result = new List<PointF>(); 

     for (double x = -Math.PI; x < Math.PI; x = x + 0.1) 
     { 
      double y = Math.Sin(x); 
      result.Add(new PointF((float)x, (float)y)); 
     } 

     return result.ToArray(); 
    } 

    protected override void OnSizeChanged(EventArgs e) 
    { 
     base.OnSizeChanged(e); 
     Invalidate(); 
    } 
+0

谢谢,你在哪里找到Matrix数据集? – 2011-02-23 23:14:51

+0

对不起,我忘了提及,你需要导入命名空间System.Drawing.Drawing2D – 2011-02-23 23:51:54

1

还记得在缩放背景图,如果你要,笔例如发生在一些构造的宽度为单身,意味着成反比的分数值可以被用来制造的一个不变的补偿ScaleTransform的效果。

更新:忘记,Pen有它自己的本地ScaleTransform,所以x和y都可以得到补偿。