home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / axis.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  3.0 KB  |  101 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : axis.cpp                                                             //
  10. //  Description: Arrow and axis drawing                                              //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include <math.h>
  19.  
  20. #include "axis.h"
  21.  
  22.  
  23. // Draw a line with an arrow    
  24. void Arrow(HDC hDC, int x0, int y0, int x1, int y1, int width, int height)
  25. {
  26.     MoveToEx(hDC, x0, y0, NULL);
  27.     LineTo(hDC, x1, y1);
  28.  
  29.     KRotate rotate(x0, y0, x1, y1);
  30.  
  31.     POINT arrow[4] = 
  32.     { 
  33.         x1, y1, 
  34.         
  35.         rotate.RotateX(rotate.m_ds-width,  height) + x0, 
  36.         rotate.RotateY(rotate.m_ds-width,  height) + y0,
  37.         
  38.         rotate.RotateX(rotate.m_ds-width, -height) + x0, 
  39.         rotate.RotateY(rotate.m_ds-width, -height) + y0,
  40.         
  41.         x1, y1
  42.     };
  43.  
  44.     HGDIOBJ hOld = SelectObject(hDC, GetStockObject(BLACK_BRUSH));
  45.     Polygon(hDC, arrow, 4);
  46.     SelectObject(hDC, hOld);
  47. }
  48.  
  49.  
  50. // Convert size in points to device coordinate
  51. void PointToDevice(HDC hDC, SIZE & size)
  52. {
  53.     int dpix = GetDeviceCaps(hDC, LOGPIXELSX);
  54.     int dpiy = GetDeviceCaps(hDC, LOGPIXELSY);
  55.  
  56.     size.cx = size.cx * dpix / 72;
  57.     size.cy = size.cy * dpiy / 72;
  58. }
  59.  
  60.  
  61. // Convert size in device coordinate to logical coordinate
  62. void DeviceToLogical(HDC hDC, SIZE & size)
  63. {
  64.     POINT p[2] = { 0, 0, size.cx, size.cy };
  65.  
  66.     DPtoLP(hDC, p, 2);
  67.  
  68.     size.cx = abs(p[1].x - p[0].x);
  69.     size.cy = abs(p[1].y - p[0].y);
  70. }
  71.  
  72.  
  73. // Generic algorithm for axes display: width height in device coordinate
  74. void ShowAxes(HDC hDC, int width, int height)
  75. {
  76.     POINT corner[2];
  77.  
  78.     // display axes for the area [10, 10, width-10, height-10]
  79.     corner[0].x = 10;
  80.     corner[0].y = 10;
  81.     corner[1].x = width - 10;
  82.     corner[1].y = height - 10;
  83.  
  84.     // covert to logical coordinate
  85.     DPtoLP(hDC, corner, 2);
  86.  
  87.     // we need to display a 10x2 point arrow, convert to device coordinate, then to logical
  88.     SIZE s = { 10, 2 };
  89.     PointToDevice(hDC, s);
  90.     DeviceToLogical(hDC, s);
  91.  
  92.     // X-axis
  93.     Arrow(hDC, min(corner[0].x, corner[1].x), 0, 
  94.                max(corner[0].x, corner[1].x), 0, s.cx, s.cy);
  95.  
  96.     // Y-axis
  97.     Arrow(hDC, 0, min(corner[0].y, corner[1].y), 
  98.                0, max(corner[0].y, corner[1].y), s.cx, s.cy);
  99. }
  100.  
  101.