首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图形转换

图形转换
EN

Stack Overflow用户
提问于 2011-02-24 06:24:31
回答 4查看 3.9K关注 0票数 2

我正在处理以频率M重复的数据(如Sin/Cosin波等)。我已经编写了一个简单的显示控件,它获取数据并绘制连接线,以在漂亮的图片中表示数据。

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

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

是否有一个简单的转换来更改数据,使其显示在适当的象限中?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-02-24 06:39:18

思考这个问题的一个好方法是,世界坐标中的(0,0)被分为

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

这将在图像坐标中(width/2,height/2)。

从那里开始,转换将是:

代码语言:javascript
复制
Data(x,y) => x = ABS(x - (width/2)), y = ABS(y - (Height/2))
票数 3
EN

Stack Overflow用户

发布于 2011-02-24 07:05:27

Graphics.ScaleTransform不是一个好主意,因为它不仅会影响布局,还会影响绘图本身(笔触、文本等的粗细)。

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

代码语言:javascript
复制
    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();
    }
票数 3
EN

Stack Overflow用户

发布于 2011-02-24 06:39:59

尝试使用以下命令反转y轴

代码语言:javascript
复制
g.ScaleTransform(1, -1);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5097983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档