Chinaunix首页 | 论坛 | 博客
  • 博客访问: 348761
  • 博文数量: 72
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 632
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-08 16:54
文章分类

全部博文(72)

文章存档

2007年(54)

2006年(18)

我的朋友

分类:

2007-01-29 12:37:43

 #define   PI   3.14159265  
   
  void   DrawLineArrow(TCanvas*   Canvas,   TPoint   p0,   TPoint   p1)  
  {  
  //对于点(x0,y0)指向(x1,y1)的箭头线,以(x1,y1)为原点,先计算出(x1,y1)  
  //指向(x0,y0)的单位向量,箭头线与直线的角度为d,那么就是将这个向量  
  //旋转角度d和-d,向量旋转的公式为:  
  //(a+b*i)*(cos(d)+i*sin(d))   =   a*cos(d)-b*sin(d)   +   i*(a*sin(d)+b*cos(d))  
  //因为上面的向量以(x1,y1)为原点的单位向量,所以还需要乘以一个长度(  
  //箭头线的长度),然后加上偏移就可以了。也就是画的箭头线的顶点的坐标为  
  //x=x1+L*(a*cos(d)-b*sin(d)),   y=y1+L*(a*sin(d)+b*cos(d))。  
  //另一个顶点的坐标,将上面的sin(d)换成   -sin(d)   就可以了。  
  //上面公式中的a为(x0-x1)/L0,   b为(y0-y1)/L0,L0是(x0,y0)与(x1,y1)的长度,  
  //L是箭头线的长度(自己设定)。  
          float   a,   b,   L0,   L,   d,   x,   y;  
   
          Canvas->MoveTo(p0.x,   p0.y);  
          Canvas->LineTo(p1.x,   p1.y);   //画p0   到   p1   的直线  
   
          L0   =   sqrt(pow(p0.x-p1.x,2)+pow(p0.y-p1.y,2));   //直线的长度  
          a     =   (p0.x-p1.x)/L0;     b   =   (p0.y-p1.y)/L0;   //单位向量的两个分量  
          L   =   10.0;   //箭头线长度,这儿我写成固定的,你可以自己修改或者计算  
          d   =   30.0/180.0*PI;     //箭头线与直线的角度30度,你可以自己调整  
          for(int   n=0;   n<2;   n++)   {  
                  x   =   p1.x+L*(a*cos(d)-b*sin(n==0?d:-d));  
                  y   =   p1.y+L*(a*sin(n==0?d:-d)+b*cos(d));  
                  Canvas->MoveTo(p1.x,   p1.y);  
                  Canvas->LineTo(x,   y);   //画箭头线  
          }  
  }
 
 
void   DrawArrow(CDC   *pdc,CPoint   m_One,   CPoint   m_Two)  
  {  
      double   slopy   ,   cosy   ,   siny;  
      double   Par   =   10.0;     //length   of   Arrow   (>)  
      slopy   =   atan2(   (   m_One.y   -   m_Two.y   ),  
          (   m_One.x   -   m_Two.x   )   );  
      cosy   =   cos(   slopy   );  
      siny   =   sin(   slopy   );   //need   math.h   for   these   functions  
   
      //draw   a   line   between   the   2   endpoint  
      pdc->MoveTo(   m_One   );  
      pdc->LineTo(   m_Two   );  
       
      //here   is   the   tough   part   -   actually   drawing   the   arrows  
      //a   total   of   6   lines   drawn   to   make   the   arrow   shape  
      pdc->MoveTo(   m_One);  
      pdc->LineTo(   m_One.x   +   int(   -   Par   *   cosy   -   (   Par   /   2.0   *   siny   )   ),  
          m_One.y   +   int(   -   Par   *   siny   +   (   Par   /   2.0   *   cosy   )   )   );  
      pdc->LineTo(   m_One.x   +   int(   -   Par   *   cosy   +   (   Par   /   2.0   *   siny   )   ),  
          m_One.y   -   int(   Par   /   2.0   *   cosy   +   Par   *   siny   )   );  
      pdc->LineTo(   m_One   );  
      /*/-------------similarly   the   the   other   end-------------/*/  
      pdc->MoveTo(   m_Two   );  
      pdc->LineTo(   m_Two.x   +   int(   Par   *   cosy   -   (   Par   /   2.0   *   siny   )   ),  
          m_Two.y   +   int(   Par   *   siny   +   (   Par   /   2.0   *   cosy   )   )   );  
      pdc->LineTo(   m_Two.x   +   int(   Par   *   cosy   +   Par   /   2.0   *   siny   ),  
          m_Two.y   -   int(   Par   /   2.0   *   cosy   -   Par   *   siny   )   );  
      pdc->LineTo(   m_Two   );  
       
  }  
阅读(4409) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~