so true

心怀未来,开创未来!
随笔 - 160, 文章 - 0, 评论 - 40, 引用 - 0
数据加载中……

坐标空间

Win32应用程序设计接口(API)使用四种坐标空间:世界坐标系空间、页面空间、设备空间、和物理设备空间。应用程序运用世界坐标系空间对图形输出进行旋转、斜切或者反射(一般坐标系完不成这些特殊的功能)。
Win32 API把世界坐标系空间和页面空间称为逻辑空间;最后一种坐标空间(即物理设备空间)通常指应用程序窗口的客户区;但是它也包括整个桌面、完整的窗口(包括框架、标题栏和菜单栏)或打印机的一页或绘图仪的一页纸。
如果该应用程序调用了SetWorldTransform函数,那么映射就从应用程序的世界坐标系空间开始;否则,映射在页面空间中进行。
下面有一个从MSDN中拷贝出来的例子如下:
enum TransformStyle { SCALE, TRANSLATE, ROTATE, SHEAR, REFLECT, NORMAL};

void CXXXXView::TransformAndDraw(int iTransform, HWND hWnd)
{
    HDC hDC;
    XFORM xForm;
    RECT rect;
 
    // Retrieve a DC handle for the application's window.
 
    hDC = ::GetDC(hWnd);
 
    // Set the mapping mode to LOENGLISH. This moves the
    // client area origin from the upper left corner of the
    // window to the lower left corner (this also reorients
    // the y-axis so that drawing operations occur in a true
    // Cartesian space). It guarantees portability so that
    // the object drawn retains its dimensions on any display.

    SetGraphicsMode(hDC, GM_ADVANCED);
    SetMapMode(hDC, MM_LOENGLISH);
 
    // Set the appropriate world transformation (based on the
    // user's menu selection).
 
    switch (iTransform)
    {
        case SCALE:        // Scale to 1/2 of the original size.
            xForm.eM11 = (FLOAT) 0.5;
            xForm.eM12 = (FLOAT) 0.0;
            xForm.eM21 = (FLOAT) 0.0;
            xForm.eM22 = (FLOAT) 0.5;
            xForm.eDx  = (FLOAT) 0.0;
            xForm.eDy  = (FLOAT) 0.0;
            SetWorldTransform(hDC, &xForm);
            break;
 
        case TRANSLATE:   // Translate right by 3/4 inch.
            xForm.eM11 = (FLOAT) 1.0;
            xForm.eM12 = (FLOAT) 0.0;
            xForm.eM21 = (FLOAT) 0.0;
            xForm.eM22 = (FLOAT) 1.0;
            xForm.eDx  = (FLOAT) 75.0;
            xForm.eDy  = (FLOAT) 0.0;
            SetWorldTransform(hDC, &xForm);
            break;
 
        case ROTATE:      // Rotate 30 degrees counterclockwise.
            xForm.eM11 = (FLOAT) 0.8660;
            xForm.eM12 = (FLOAT) 0.5000;
            xForm.eM21 = (FLOAT) -0.5000;
            xForm.eM22 = (FLOAT) 0.8660;
            xForm.eDx  = (FLOAT) 0.0;
            xForm.eDy  = (FLOAT) 0.0;
            SetWorldTransform(hDC, &xForm);
            break;
 
        case SHEAR:       // Shear along the x-axis with a
                          // proportionality constant of 1.0.
            xForm.eM11 = (FLOAT) 1.0;
            xForm.eM12 = (FLOAT) 1.0;
            xForm.eM21 = (FLOAT) 0.0;
            xForm.eM22 = (FLOAT) 1.0;
            xForm.eDx  = (FLOAT) 0.0;
            xForm.eDy  = (FLOAT) 0.0;
            SetWorldTransform(hDC, &xForm);
            break;
 
        case REFLECT:     // Reflect about a horizontal axis.
            xForm.eM11 = (FLOAT) 1.0;
            xForm.eM12 = (FLOAT) 0.0;
            xForm.eM21 = (FLOAT) 0.0;
            xForm.eM22 = (FLOAT) -1.0;
            xForm.eDx  = (FLOAT) 0.0;
            xForm.eDy  = (FLOAT) 0.0;
            SetWorldTransform(hDC, &xForm);
            break;
 
        case NORMAL:      // Set the unity transformation.
            xForm.eM11 = (FLOAT) 1.0;
            xForm.eM12 = (FLOAT) 0.0;
            xForm.eM21 = (FLOAT) 0.0;
            xForm.eM22 = (FLOAT) 1.0;
            xForm.eDx  = (FLOAT) 0.0;
            xForm.eDy  = (FLOAT) 0.0;
            SetWorldTransform(hDC, &xForm);
            break;
  default:
   break;
    }
 
    // Find the midpoint of the client area.
 
    ::GetClientRect(hWnd, (LPRECT) &rect);
    DPtoLP(hDC, (LPPOINT) &rect, 2);
 
    // Select a hollow brush.
 
    SelectObject(hDC, GetStockObject(HOLLOW_BRUSH));
 
    // Draw the exterior circle.
 
    Ellipse(hDC, (rect.right / 2 - 100), (rect.bottom / 2 + 100),
        (rect.right / 2 + 100), (rect.bottom / 2 - 100));
 
    // Draw the interior circle.
 
    Ellipse(hDC, (rect.right / 2 -94), (rect.bottom / 2 + 94),
        (rect.right / 2 + 94), (rect.bottom / 2 - 94));
 
    // Draw the key.
 
    Rectangle(hDC, (rect.right / 2 - 13), (rect.bottom / 2 + 113),
        (rect.right / 2 + 13), (rect.bottom / 2 + 50));
    Rectangle(hDC, (rect.right / 2 - 13), (rect.bottom / 2 + 96),
        (rect.right / 2 + 13), (rect.bottom / 2 + 50));
 
    // Draw the horizontal lines.
 
    MoveToEx(hDC, (rect.right/2 - 150), (rect.bottom / 2 + 0), NULL);
    LineTo(hDC, (rect.right / 2 - 16), (rect.bottom / 2 + 0));
 
    MoveToEx(hDC, (rect.right / 2 - 13), (rect.bottom / 2 + 0), NULL);
    LineTo(hDC, (rect.right / 2 + 13), (rect.bottom / 2 + 0));
 
    MoveToEx(hDC, (rect.right / 2 + 16), (rect.bottom / 2 + 0), NULL);
    LineTo(hDC, (rect.right / 2 + 150), (rect.bottom / 2 + 0));
 
    // Draw the vertical lines.
 
    MoveToEx(hDC, (rect.right/2 + 0), (rect.bottom / 2 - 150), NULL);
    LineTo(hDC, (rect.right / 2 + 0), (rect.bottom / 2 - 16));
 
    MoveToEx(hDC, (rect.right / 2 + 0), (rect.bottom / 2 - 13), NULL);
    LineTo(hDC, (rect.right / 2 + 0), (rect.bottom / 2 + 13));
 
    MoveToEx(hDC, (rect.right / 2 + 0), (rect.bottom / 2 + 16), NULL);
    LineTo(hDC, (rect.right / 2 + 0), (rect.bottom / 2 + 150));
 
    ::ReleaseDC(hWnd, hDC);
}
页面空间到设备空间的转换是原Windows接口的一部分,也是我们可以用代码控制的一种转换,页面空间到设备空间的转换所用的是两个矩形的宽与高的比率,其中页面空间中的矩形被称为窗口,设备空间中的矩形被称为视口,Windows把窗口原点映射到视口原点,把窗口范围映射到视口范围,就完成了这种转换。

设备空间到物理空间的转换有几个独特之处:它只限于平移,并由Windows的窗口管理部分控制,这种转换的唯一用途是确保设备空间的原点被映射到物理设备上的适当点上。没有函数能设置这种转换,也没有函数可以获取有关数据。因此我们不能人为干预。

实际上我们需要完成的是从页面空间到物理设备空间的映射,由于物理设备空间通常指应用程序窗口的客户区,因此我们只用关注于从页面空间到设备空间的映射就可以了。

一旦应用程序建立了设备描述表(DC),并立即开始调用GDI绘图或输出函数,则运用默认页面空间到设备空间的转换和设备空间到客户区的转换(在应用程序调用SetWorldTransform函数之前,不会出现世界坐标空间到页面空间的转换)。

默认页面空间到设备空间的转换结果是一对一的映射;即页面空间上给出的一点映射到设备空间的一个点。正如前文讲到的,这种转换没有以矩阵指定,而是通过把视口宽除以窗口宽,把视口高除以窗口高而得出的。在默认的情况下,视口尺寸为1x1个象素,窗口尺寸为1x1页单位。

几乎在所有的GDI函数中使用的坐标值都是采用的逻辑单位。Windows必须将逻辑单位转换为“设备单位”,即像素。Windows对所有的消息(如WM_SIZE、WM_MOUSEMOVE、WM_LBUTTONDOWN、WM_LBUTTONUP),所有的非GDI函数和一些GDI函数(例如GetDeviceCaps函数),永远使用设备坐标。“窗口”是基于逻辑坐标的,逻辑坐标可以是象素、毫米、英寸等单位;“视口”是基于设备坐标(象素)的。通常,视口和客户区是相同的。

窗口(逻辑)坐标转换为视口(设备)坐标的两个公式:
xViewport=(xWindow-xWinOrg)*xViewExt/xWinExt+xViewOrg
yViewport=(yWindow-yWinOrg)*yViewExt/yWinExt+yViewOrg

视口(设备)坐标转换为窗口(逻辑)坐标的两个公式:
xWindow=(xViewPort-xViewOrg)*xWinExt/xViewExt+xWinOrg
yWindow=(yViewPort-yViewOrg)*yWinExt/yViewExt+yWinOrg

在MM_TEXT映射方式下逻辑坐标和设备坐标的相互转换
窗口(逻辑)坐标转换为视口(设备)坐标的两个公式:
xViewport = xWindow-xWinOrg+xViewOrg
yViewport = yWindow-yWinOrg+yViewOrg
视口(设备)坐标转换为窗口(逻辑)坐标的两个公式:
xWindow = xViewport-xViewOrg+xWinOrg
yWindow = yViewport-yViewOrg+yWinOrg

CDC中提供了两个成员函数函数SetViewportOrg和SetWindowOrg,用来改变视口和窗口的原点。
如果将视口原点设置为(xViewOrg,yViewOrg),则逻辑点(0,0)就会被映射为设备点(xViewOrg,yViewOrg)。如果将窗口原点改变为(xWinOrg,yWinOrg),则逻辑点(xWinOrg,yWinOrg)将会被映射为设备点(0,0),即左上角。
不管对窗口和视口原点作什么改变,设备点(0,0)始终是客户区的左上角。

说一点自己的感悟:
坐标系有四种,上面介绍的很详细了,就不多说了。
对于很重要的从页面坐标到设备坐标的转换,页面坐标用逻辑单位表示,可以采用长度度量单位如米或英尺来与逻辑单位相挂钩,也可以用像素来和逻辑单位挂钩,而且我们说窗口;设备坐标只用像素表示,而且我们说视口。因此从逻辑坐标到设备坐标有很多种映射方式:
Mapping Mode Logical Unit Positive y-axis Extends...
MM_TEXT 1 pixel Downward
MM_HIMETRIC 0.01 mm Upward
MM_TWIPS 1/1440 in Upward
MM_HIENGLISH 0.001 in Upward
MM_LOMETRIC 0.1 mm Upward
MM_LOENGLISH 0.01 in Upward
还有MM_ISOTROPIC(长度和宽度的比例因子一致)和MM_ANISOTROPIC(长宽比例因子可以不一致)。
举个例子来说,比如MM_HIMETRIC,每个逻辑单元代表0.01毫米,比如逻辑横坐标100,代表1毫米的长度,而对于设备坐标(也可以说物理坐标)来说,如果分辨率是96dpi,那么也就是1英尺有96个像素,因此从1毫米先转换为多少英尺,然后再转换为多少个像素,就可以知道屏幕上在哪个位置显示这个位置了。

还需要注意的是,GDI函数都是用逻辑坐标来计算的,而非GDI函数以及少部分GDI函数都是用设备坐标也就是像素来表示的,设备坐标只能用像素来计算。

 

 

posted on 2008-02-23 19:18 so true 阅读(734) 评论(0)  编辑  收藏 所属分类: C&C++


只有注册用户登录后才能发表评论。


网站导航: