绘制一组矩形时翻转坐标 [英] Flip coordinates when drawing set of rectangles

查看:46
本文介绍了绘制一组矩形时翻转坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一组正方形,但正方形的原点 (0,0) 最初位于左上角,我希望正方形从右下角渲染,我发现这段代码可以翻转坐标,但在 winform 上没有任何渲染.

I am trying to draw set of squares but the origin(0,0) of squares was in top left corner initially and I wanted squares to render from bottom right corner, and I found this code to flip the co ordinates, but nothing is rendering on winform.

我知道我在 TranslateTransform 的 Height 属性中出错了.我不明白为什么需要 Height ,因为我正在尝试绘制一组 2d 正方形.

I know that I have gone wrong in Height attribute of TranslateTransform. I dont get as to why Height is required as i am trying to draw a set of 2d squares.

我尝试硬编码高度属性仍然没有用.

I tried hardcoding the height attribute still of no use.

public void ScaleTransformFloat(PaintEventArgs e,List<Square> lstSquare)
    {
        int Height = 10;
        // Begin graphics container
        GraphicsContainer containerState = e.Graphics.BeginContainer();

        // Flip the Y-Axis
        e.Graphics.ScaleTransform(1.0F, -1.0F);

        // Translate the drawing area accordingly
        //
        e.Graphics.TranslateTransform(0.0F, -(float)Height);

        // Whatever you draw now (using this graphics context) will appear        as
        // though (0,0) were at the bottom left corner

    //User-defined function to draw a square    
        DrawSquare(e,lstSquare);
        // End graphics container
        e.Graphics.EndContainer(containerState);


    }

绘制一组正方形的方法

  public void DrawSquare(PaintEventArgs e,List<Square> lstSquare)
    {
      foreach(Square s in lstSquare){
        e.Graphics.DrawRectangle(Pens.Black, 0,0 ,s.m_Side, s.m_Side);
      }
    }

推荐答案

当进行矩阵运算时,通常顺序很重要.

When doing matrix operation like transformations usually the order does matter.

在您的情况下,您需要先翻译,然后缩放/翻转或反转翻译方向.您的代码似乎是正确的.

In your case you need to either translate first, then scale/flip or reverse the direction of the translation. Your code seems to get it right.

Height 不是 3d 高度.它是您要绘制的目标控件的总Height.

The Height is not a 3d height. It is the total Height of the target control you want to draw on.

想象一下在您的控件上放一张纸;控件是您的视口.现在想象一下通过左上边缘向上翻转纸张.它已经离开了视口.现在您需要将其向下移动.但是距离是多少?答:纸的高度..

Imagine laying a sheet of paper on your control; the control is your viewport. Now imagine flipping the paper topwise by the top left edge. It has left the viewport. Now you need to move it down. But by what distance? Answer: The paper's height..

您的代码具有 Height = 10 像素.这肯定不是你控制的大小吧?也许它来自你复制的代码;在那里它可能意味着 Form's Height.如果您还想在 Form 上绘图(通常不是一个好主意),只需删除行 int Height = 10;!

Your code has a Height = 10 pixels. This is certainly not the size of your control, right? Maybe it comes from code you copied; there it probably meant the Form's Height. If you want to draw on the Form also (usually not a good idea), simply delete the line int Height = 10;!

让我们做一个简单的例子:我们在Panel上绘制几个矩形:

Let's do a simple example: We draw a few rectangles onto a Panel:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    if (checkBox1.Checked)
    {
        e.Graphics.ScaleTransform(1, -1);
        e.Graphics.TranslateTransform(0, -panel1.Height);
    }
    e.Graphics.DrawRectangle(Pens.Violet, 1, 1, 33, 33);
    e.Graphics.DrawRectangle(Pens.OrangeRed, 11, 11, 133, 55);
    e.Graphics.DrawRectangle(Pens.Magenta, 44, 11, 233, 77);
    e.Graphics.DrawRectangle(Pens.Olive, 55, 44, 33, 99);
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    panel1.Invalidate();
}

检查CheckBox后结果翻转:

另请注意,您的 DrawSquare 在同一位置绘制所有正方形 (0,0).不确定这是否是您想要的..

Also note that your DrawSquare draws all squares at the same location (0,0). Not sure if that is what you want..

最后一点:DrawRectangle 有一个坏习惯,就是将底部和右侧过度绘制一个像素.将第一个位置从 (1,1) 更改为 (0,0) 后,(然后)底部将被切断.您可以考虑将图形坐标减少 1 个像素.

Final note: DrawRectangle has a nasty habit of overdrawing the botton and right sides by one pixel. After changing the first loaction from (1,1) to (0,0) the (then) bottom side will be cut off. You might consider translating the graphics coodinates by 1 pixel less..

这篇关于绘制一组矩形时翻转坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆