Logical coordinates and device coordinates

Source: Internet
Author: User
Creating a proper coordinate system can bring great convenience to our drawing. The following describes how to create the desired Coordinate System in VC.

Device coordinates and logical coordinates

Device coordinate, also known as physical coordinate, is the coordinate on the output device. Device coordinates on the screen are usually called screen coordinates. The device coordinate specifies the object position based on the horizontal distance and vertical distance between the object and the upper left corner of the window, expressed in pixels. the X axis of the device coordinate is positive to the right, and the Y axis is positive downward, the coordinate origin is located in the upper left corner of the window.

The logical coordinate is the coordinate used by the system as the record. In the default mode (mm_text), the direction and unit of the logical coordinates are the same as those of the device coordinates and are also expressed in pixels. the X axis is positive to the right, the Y axis is downward positive, and the coordinate origin is located in the upper left corner of the window. Logical coordinates and device coordinates may not be the same even in the default mode, except in the following two cases:

1. Non-scrolling window

2. The window is a rolling window, but the vertical scroll bar is located at the top of the scroll border and the horizontal scroll bar is located at the leftmost end. However, if the two coordinates of the scroll bar are moved, they are inconsistent.

In VC, the coordinates of the mouse coordinates are represented by the device coordinates, but all the GDI drawings are represented by the logical coordinates. Therefore, when drawing with the mouse, the device must be converted to the logical coordinates, you can use the CDC function dptolp () to convert the device coordinates to the logical coordinates. You can also use lptodp () to convert the logical coordinates to the device coordinates.

Binary coordinate Mode

To use logical coordinates in different fields, Windows provides the following eight coordinate modes:

Mm_text, mm_hienglish, mm_loenglish, mm_himetric, mm_lometric, mm_twips, mm_anisotropic, and mm_isotropic.

3. instance resolution

(1) Create coordinates with the upper left corner as the origin, and the X axis and Y axis as 1000, as shown in

We can use the following code:

  

void CTtView::OnDraw(CDC* pDC)   {    CTtDoc* pDoc = GetDocument();    ASSERT_VALID(pDoc);    CRect rect;    GetClientRect(&rect);    pDC->SetMapMode(MM_ANISOTROPIC);    pDC->SetViewportOrg(0,0);    pDC->SetViewportExt(rect.right,rect.bottom);    pDC->SetWindowOrg(0,0);    pDC->SetWindowExt(1000,1000);    pDC->MoveTo(50,50);    pDC->LineTo(50,950);    pDC->LineTo(950,950);    pDC->LineTo(50,50);   }

Code Analysis:

1. getclientrect (& rect); obtain the rectangle of the customer area and store it in rect.

2. Use PDC-> setmapmode (mm_anisotropic); set the ing mode

3. Use PDC-> setviewportorg (); To set the origin of logical coordinates

4. Use PDC-> setviewportext (rect. Right, rect. Bottom); and

PDC-> setw.wext (); To determine the size correspondence between logical and device coordinates

5. In mm_anisotropic mode, the X axis unit and Y axis unit can be different.

6. If the logical window range is the same as the window range symbol, the direction of the logical coordinates is the same as that of the view, that is, the X axis is right, and the Y axis is downward.

7. If you change the display mode to mm_isotropic, the X axis unit must be the same as the Y axis unit. Interested readers can make it by themselves.

(2) create coordinates with the Windows center as the origin, as shown in figure

Use the following code:

void CTtView::OnDraw(CDC* pDC)   {    CTtDoc* pDoc = GetDocument();    ASSERT_VALID(pDoc);    CRect rect;    GetClientRect(&rect);    pDC->SetMapMode(MM_ANISOTROPIC);    pDC->SetViewportOrg(rect.right/2,rect.bottom/2);    pDC->SetViewportExt(rect.right,rect.bottom);    pDC->SetWindowOrg(0,0);    pDC->SetWindowExt(1000,-1000);    pDC->MoveTo(150,150);    pDC->LineTo(-150,-200);    pDC->LineTo(150,-150);    pDC->LineTo(150,150);   }

Code Analysis:

1. Use PDC-> setviewportorg (rect. Right/2, rect. Bottom/2.

2. use PDC-> setviewportext (rect. right, rect. bottom); and PDC-> setw.wext (1000,-1000); To determine the unit correspondence between the device coordinate and the logical coordinate.

3. Because the symbols in the range of the logical window are inconsistent with those in the window, the vertical coordinates are reversed, so the Y axis is positive upward.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.