Size of 1 pixel

This is a discussion on Size of 1 pixel within the C++ Programming forums, part of the General Programming Boards category; I want to ask about the dimension of 1 pixel in inch unit. My code is below. When I print ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    68

    Lightbulb Size of 1 pixel

    I want to ask about the dimension of 1 pixel in inch unit.

    My code is below. When I print the Rectangle. It's very small. So I wonder about unit of x and y in this below code.Are they in pixel unit or other unit?

    code:
    -------------------------------------------------------------
    int x = 0 ;
    int y = 0 ;

    CBrush brush;

    brush.CreateSolidBrush(RGB(0, 0, 0));

    CRect Rectangle(x, y, x + 2, y + 4) ;

    dc.FillRect(&Rectangle , &brush);
    -------------------------------------------------------------

  2. #2
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    to find that out you would have to take your screen's size horizontally in inches, and divide by the width of the resolution in pixels. then do the same for the height. Then multiply the two answers and you got the area in square inches.

  3. #3
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    Looks like MFC.
    I want to ask about the dimension of 1 pixel in inch unit.
    Doesn't matter. Windows can do it all for you.

    Windows deals with drawing in terms of logical coordinates and device coordinates. Device coordinates are physical pixels on your monitor. Logical coordinates are a 'mapping system' used to position things in a window. All of the CDC class functions that accept coordinates (like CDC::FillRect) use logical coordinates.

    By default, logical coordinates directly correspond to device coordinates (logical coordinate x,y corresponds to exactly one pixel). So, the code
    Code:
    brush.CreateSolidBrush(RGB(0, 0, 0)); 
    CRect Rectangle(0, 0, 100, 200) ;  //Simplified your coordinates for example clarity 
    dc.FillRect(&Rectangle , &brush);
    will fill a rectangle that is 100 logical units by 200 logical units. Since, by default, logical coordinates and device coordinates are equal, the rectangle is 100 pixels by 200 pixels. You can, however, change the relationship between logical coordinates and device coordinates by using CDC::SetMapMode(int MappingMode). The Mapping Modes are:
    Code:
    MappingMode One logical unit equals
    MM_TEXT                           1 pixel
    MM_LOMETRIC                       .1 mm
    MM_HIMETRIC                       .01 mm
    MM_LOENGLISH                      .01 in
    MM_HIENGLISH                      .001 in
    MM_TWIPS                          .0007 in
    MM_ISOTROPIC                      user defined
    MM_ANISTROPIC                     user defined
    The code
    Code:
    brush.CreateSolidBrush(RGB(0, 0, 0)); 
    CRect Rectangle(0, 0, 100, 200) ;  //Simplified your coordinates for example clarity
    dc.SetMapMode(MM_LOENGLISH); 
    dc.FillRect(&Rectangle , &brush);
    will still fill a rectangle that is 100 logical units by 200 logical units. But, since we changed the mapping mode to MM_LOENGLISH, that rectangle is now 1 inch by 2 inches. If you measure the rectangle on your monitor, it should actually come out to 1x2 inches, though sometimes it's not exact due to differences in monitors, resolution, etc. If the device context is refering to a printer, it will come out exact because a printer has a fixed resolution (dpi).

    Note that logical and device coordinates apply only to CDC class functions. The CRect class takes 4 integers as the corners for the rectangle, but CRect does not differentiate between logical or device coordinates; CDC::FillRect applies the 4 integers as logical coordinates.

    fletch
    "Logic is the art of going wrong with confidence."
    Morris Kline

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    Thank you for your answer.

  5. #5
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    Sure, no problem. I forgot one thing though...

    For all of the mapping modes except MM_TEXT, the window is in the bottom right quadrant of the x,y axis...your 'y' values should be negative.
    Code:
          top left corner ----------------------> +x
                          |
                          |      Window space
                          |
                          |
                          |
                          \/
                         -y
    The MM_TEXT mode is the conventional +x,+y that you're used to.
    Correcting the previous code...
    Code:
    brush.CreateSolidBrush(RGB(0, 0, 0)); 
    CRect Rectangle(0, 0, 100, -200) ;  //Simplified your coordinates for example clarity. -200 vice +200
    dc.SetMapMode(MM_LOENGLISH); 
    dc.FillRect(&Rectangle , &brush);
    fletch
    "Logic is the art of going wrong with confidence."
    Morris Kline

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heapsort
    By xENGINEERx in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 07:17 PM
  2. Generic heapsort
    By Sephiroth1109 in forum C Programming
    Replies: 15
    Last Post: 12-07-2007, 05:14 PM
  3. How do you use variable to initialize array size?
    By yougene in forum C Programming
    Replies: 11
    Last Post: 09-04-2007, 02:50 PM
  4. Comment this Program
    By kishorepalle in forum C Programming
    Replies: 11
    Last Post: 10-05-2004, 06:41 AM
  5. Determining Data Size For Network Send/Rec :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 6
    Last Post: 05-07-2002, 09:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21