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

  1. #pragma once
  2.  
  3. //-----------------------------------------------------------------------------------//
  4. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  5. //                             ISBN  0-13-086985-6                                   //
  6. //                                                                                   //
  7. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  8. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  9. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  10. //                                                                                   //
  11. //  FileName   : curve.h                                                             //
  12. //  Description: Path data, curve transformation, styled curve                       //
  13. //  Version    : 1.00.000, May 31, 2000                                              //
  14. //-----------------------------------------------------------------------------------//
  15.  
  16. class KDash
  17. {
  18. public:
  19.     virtual double GetLength(int step)
  20.     {
  21.         return 10;
  22.     }
  23.  
  24.     virtual BOOL DrawDash(double x1, double y1, double x2, double y2, int step)
  25.     {
  26.         return FALSE;
  27.     }
  28. };
  29.  
  30.  
  31.  
  32. class K2DMap
  33. {
  34. public:
  35.     virtual Map(long & px, long & py) = 0;
  36. };
  37.  
  38.  
  39. class KBiLinearMap : public K2DMap
  40. {
  41.     double x00, y00, x01, y01, x10, y10, x11, y11;
  42.     double orgx, orgy, width, height;
  43.  
  44. public:
  45.     void SetWindow(int x, int y, int w, int h)
  46.     {
  47.         orgx   = x;
  48.         orgy   = y;
  49.         width  = w;
  50.         height = h;
  51.     }
  52.  
  53.     void SetDestination(POINT P[])
  54.     {
  55.         x00 = P[0].x; y00 = P[0].y;
  56.         x01 = P[1].x; y01 = P[1].y;
  57.         x10 = P[2].x; y10 = P[2].y;
  58.         x11 = P[3].x; y11 = P[3].y;
  59.     }
  60.  
  61.     virtual Map(long & px, long & py)
  62.     {
  63.         double x = (px - orgx ) / width;
  64.         double y = (py - orgy ) / height;
  65.  
  66.         px = (long) ( (1-x) * ( x00 * (1-y) + x01 * y ) +
  67.                           x * ( x10 * (1-y) + x11 * y ) );
  68.  
  69.         py = (long) ( (1-x) * ( y00 * (1-y) + y01 * y ) +
  70.                           x * ( y10 * (1-y) + y11 * y ) );
  71.     }
  72. };
  73.  
  74.  
  75. class KTransCurve
  76. {
  77.     int   m_orgx; // first point in a figure
  78.     int   m_orgy;
  79.  
  80.     float x0;      // current last point    
  81.     float y0;
  82.     float m_dstx;
  83.     float m_dsty;
  84.     int   m_seglen; // segment length to break
  85.  
  86.     virtual Map(float x, float y, float & rx, float & ry)
  87.     {
  88.         rx = x;
  89.         ry = y;
  90.     }
  91.  
  92.     virtual BOOL DrvLineTo(HDC hDC, int x, int y)
  93.     {
  94.         return ::LineTo(hDC, x, y);
  95.     }
  96.  
  97.     virtual BOOL DrvMoveTo(HDC hDC, int x, int y)
  98.     {
  99.         return ::MoveToEx(hDC, x, y, NULL);
  100.     }
  101.  
  102.     virtual BOOL DrvBezierTo(HDC hDC, POINT p[])
  103.     {
  104.         return ::PolyBezierTo(hDC, p, 3);
  105.     }    
  106.  
  107.     BOOL BezierTo(HDC hDC, float x1, float y1, float x2, float y2, float x3, float y3);
  108.     
  109. public:
  110.  
  111.     KTransCurve(int seglen)
  112.     {
  113.         m_seglen = seglen;
  114.     }
  115.  
  116.     BOOL   MoveTo(HDC hDC, int x, int y);
  117.  
  118.     BOOL BezierTo(HDC hDC, int x1, int y1, int x2, int y2, int x3, int y3)
  119.     {
  120.         return BezierTo(hDC, (float) x1, (float) y1, (float) x2, (float) y2, (float) x3, (float) y3);
  121.     }
  122.     
  123.     BOOL CloseFigure(HDC hDC)
  124.     {
  125.         LineTo(hDC, m_orgx, m_orgy );
  126.  
  127.         return ::CloseFigure(hDC);
  128.     }
  129.  
  130.     BOOL LineTo(HDC hDC, int x3, int y3)
  131.     {
  132.         return BezierTo(hDC, (x0*2+x3)/3, (y0*2+y3)/3, (x0+x3*2)/3, (y0+y3*2)/3, (float) x3, (float) y3);
  133.     }
  134. };
  135.  
  136.  
  137. class KPathData
  138. {
  139. public:
  140.     POINT * m_pPoint;
  141.     BYTE  * m_pFlag;
  142.     int     m_nCount;
  143.  
  144.     KPathData()
  145.     {
  146.         m_pPoint = NULL;
  147.         m_pFlag  = NULL;
  148.         m_nCount = 0;
  149.     }
  150.  
  151.     ~KPathData()
  152.     {
  153.         if ( m_pPoint ) delete m_pPoint;
  154.         if ( m_pFlag  ) delete m_pFlag;
  155.     }
  156.  
  157.     int  GetPathData(HDC hDC);
  158.     void MarkPoints(HDC hDC, bool bShowLine=true);
  159.     void MapPoints(K2DMap & map);
  160.  
  161.     BOOL Draw(HDC hDC, KTransCurve & transformer, bool bPath);
  162. };
  163.  
  164.  
  165. class KStyleCurve
  166. {
  167.     int         m_step;
  168.     double     m_x1, m_y1;
  169.     KDash  * m_pDash;
  170.  
  171. public:
  172.  
  173.     KStyleCurve(KDash * pDash)
  174.     {
  175.         m_x1   = 0;
  176.         m_y1   = 0;
  177.         m_step = 0;
  178.         m_pDash= pDash;
  179.     }
  180.  
  181.     BOOL MoveTo(double x, double y);
  182.     BOOL LineTo(double x, double y);
  183.     BOOL PolyDraw(const POINT *ppt, const BYTE *pbTypes, int nCount);
  184. };
  185.  
  186.  
  187. class KDashes : public KDash
  188. {
  189.     HDC    m_hDC;
  190.     int    m_test;
  191.  
  192.     virtual double GetLength(int step)
  193.     {
  194.         if ( step & 1)
  195.             return 5+m_test;
  196.         else
  197.             return 5+m_test*2;
  198.     }
  199.  
  200.     virtual BOOL DrawDash(double x1, double y1, double x2, double y2, int step);
  201.     
  202. public:
  203.     
  204.     KDashes(HDC hDC, int test)
  205.     {
  206.         m_hDC   = hDC;
  207.         m_test  = test;
  208.     }
  209. };
  210.  
  211.  
  212.