C++绘制箭头原理

原文(墙):http://kapo-cpp.blogspot.com/2008/10/drawing-arrows-with-cairo.html

 

Drawing arrows with Cairo

For my ggredit project I need to draw connectors between objects, connectors are simple lines that can have an arrow head at the begin and/or at the end.
But how to draw an arrowed line? We have to come back at school days, when we learn trigonometry.

Basics


With the function atan2 we can obtain the angle of the source line. Adding to this angle a defined sub-angle (arrow_degrees_) we can get the left and right angle of the arrow arms. With this two angles and using a defined lenght for the arrow arms (arrow_lenght_) we can finally achieve two points to use for perform drawing operations.

Now return to the future...we need to transform this on c++ code.

 
  1. void calcVertexes(double start_x, double start_y, double end_x, double end_y, double& x1, double& y1, double& x2, double& y2)

  2. {

  3. double angle = atan2 (end_y - start_y, end_x - start_x) + M_PI;

  4.  
  5. x1 = end_x + arrow_lenght_ * cos(angle - arrow_degrees_);

  6. y1 = end_y + arrow_lenght_ * sin(angle - arrow_degrees_);

  7. x2 = end_x + arrow_lenght_ * cos(angle + arrow_degrees_);

  8. y2 = end_y + arrow_lenght_ * sin(angle + arrow_degrees_);

  9. }

  10.  
  11.  

 

Effective arrow object


We want to offer many kinds of arrows to the user, like on the wikipedia page for thesymbol Arrow. I start to define this kind of arrows:

 
  1. enum ArrowStyle

  2. {

  3. ARROW_OPEN,

  4. ARROW_SOLID,

  5. ARROW_SOLID_FILLED,

  6. ARROW_DIAMOND,

  7. ARROW_DIAMOND_FILLED,

  8. ARROW_CIRCLE,

  9. ARROW_CIRCLE_FILLED

  10. };

  11.  


But we do not like to put an if-then-else evry time we need to draw the arrow. Is better to define an ArrowHead base class with an abstract method draw, and implements all different styles inheriting from the ArrowHead class.

The ArrowHead class is the base class.

 
  1. class ArrowHead

  2. {

  3. public:

  4. enum ArrowStyle

  5. {

  6. ARROW_OPEN,

  7. ARROW_SOLID,

  8. ARROW_SOLID_FILLED,

  9. ARROW_DIAMOND,

  10. ARROW_DIAMOND_FILLED,

  11. ARROW_CIRCLE,

  12. ARROW_CIRCLE_FILLED

  13. };

  14.  
  15. ArrowHead() :

  16. arrow_lenght_( 15 ),

  17. arrow_degrees_( 0.5 )

  18. {

  19. }

  20.  
  21. virtual ~ArrowHead()

  22. {

  23. }

  24.  
  25. void calcVertexes(double start_x, double start_y, double end_x, double end_y, double& x1, double& y1, double& x2, double& y2)

  26. {

  27. double angle = atan2 (end_y - start_y, end_x - start_x) + M_PI;

  28.  
  29. x1 = end_x + arrow_lenght_ * cos(angle - arrow_degrees_);

  30. y1 = end_y + arrow_lenght_ * sin(angle - arrow_degrees_);

  31. x2 = end_x + arrow_lenght_ * cos(angle + arrow_degrees_);

  32. y2 = end_y + arrow_lenght_ * sin(angle + arrow_degrees_);

  33. }

  34.  
  35. virtual void draw(Cairo::RefPtr< Cairo::Context > context_ref, double start_x, double start_y, double end_x, double end_y) = 0;

  36.  
  37. protected:

  38. double arrow_lenght_;

  39. double arrow_degrees_;

  40. };

  41.  


The Arrow object has two arrow head, for draw the begin and the end arrow.

 
  1. std::auto_ptr< ArrowHead > start_arrow_head_ptr_;

  2. std::auto_ptr< ArrowHead > end_arrow_head_ptr_;

  3.  


we can use this object for draw the arrow:

 
  1. start_arrow_head_ptr_->draw (context_ref, x2, y2, x1, y1);

  2. end_arrow_head_ptr_->draw (context_ref, x1, y1, x2, y2);

  3.  


Now we need to implements all classes for the effective renderering. We can start with the ARROW_OPEN, like this:

We simply need to draw two lines, the first line from end_x, end_y to x1, y1 the second line from end_x, end_y to x2, y2.

 
  1. class ArrowOpen : public ArrowHead

  2. {

  3. public:

  4. ArrowOpen() :

  5. ArrowHead(),

  6. line_color_( "black" )

  7. {

  8. }

  9.  
  10. virtual ~ArrowOpen()

  11. {

  12. }

  13.  
  14. void draw(Cairo::RefPtr< Cairo::Context > context_ref, double start_x, double start_y, double end_x, double end_y)

  15. {

  16. double x1;

  17. double y1;

  18. double x2;

  19. double y2;

  20.  
  21. calcVertexes (start_x, start_y, end_x, end_y, x1, y1, x2, y2);

  22.  
  23. context_ref->set_source_rgb (line_color_.get_red_p(), line_color_.get_blue_p(), line_color_.get_green_p());

  24.  
  25. context_ref->move_to (end_x, end_y);

  26. context_ref->line_to (x1, y1);

  27. context_ref->stroke();

  28.  
  29. context_ref->move_to (end_x, end_y);

  30. context_ref->line_to (x2, y2);

  31. context_ref->stroke();

  32. }

  33.  
  34. protected:

  35. Gdk::Color line_color_;

  36. };

  37.  


We can implement the ARROW_SOLID, like this:

In this case we need to draw a triangle using the three vertexes: end_x, end_y; x1, y1 and x2, y2.

 
  1. class ArrowSolid : public ArrowHead

  2. {

  3. public:

  4. ArrowSolid() :

  5. ArrowHead(),

  6. line_color_( "black" ),

  7. fill_color_( "white" )

  8. {

  9. }

  10.  
  11. virtual ~ArrowSolid()

  12. {

  13. }

  14.  
  15. void draw(Cairo::RefPtr< Cairo::Context > context_ref, double start_x, double start_y, double end_x, double end_y)

  16. {

  17. double x1;

  18. double y1;

  19. double x2;

  20. double y2;

  21.  
  22. calcVertexes (start_x, start_y, end_x, end_y, x1, y1, x2, y2);

  23.  
  24. context_ref->move_to (end_x, end_y);

  25. context_ref->line_to (x1, y1);

  26. context_ref->line_to (x2, y2);

  27. context_ref->close_path();

  28.  
  29. context_ref->set_source_rgb (line_color_.get_red_p(), line_color_.get_blue_p(), line_color_.get_green_p());

  30. context_ref->stroke_preserve();

  31.  
  32. context_ref->set_source_rgb (fill_color_.get_red_p(), fill_color_.get_blue_p(), fill_color_.get_green_p());

  33. context_ref->fill();

  34. }

  35.  
  36. protected:

  37. Gdk::Color line_color_;

  38. Gdk::Color fill_color_;

  39. };

  40.  


We can implement the ARROW_DIAMOND, like this:

In this case we need four vertexes, but for now we have only three vertexes. The fourth vertex is also a point on the source line (so for the same angle of the source line), I use this method:

 
  1. class ArrowDiamond : public ArrowHead

  2. {

  3. public:

  4. ArrowDiamond() :

  5. ArrowHead(),

  6. line_color_( "black" ),

  7. fill_color_( "white" )

  8. {

  9. arrow_lenght_ = 10.0;

  10. }

  11.  
  12. virtual ~ArrowDiamond()

  13. {

  14. }

  15.  
  16. void draw(Cairo::RefPtr< Cairo::Context > context_ref, double start_x, double start_y, double end_x, double end_y)

  17. {

  18. double angle = atan2 (end_y - start_y, end_x - start_x) + M_PI;

  19.  
  20. double x1 = end_x + arrow_lenght_ * cos(angle - arrow_degrees_);

  21. double y1 = end_y + arrow_lenght_ * sin(angle - arrow_degrees_);

  22. double x2 = end_x + arrow_lenght_ * cos(angle + arrow_degrees_);

  23. double y2 = end_y + arrow_lenght_ * sin(angle + arrow_degrees_);

  24. double x3 = end_x + arrow_lenght_ * 2 * cos(angle);

  25. double y3 = end_y + arrow_lenght_ * 2 * sin(angle);

  26.  
  27. context_ref->move_to (end_x, end_y);

  28. context_ref->line_to (x1, y1);

  29. context_ref->line_to (x3, y3);

  30. context_ref->line_to (x2, y2);

  31. context_ref->close_path();

  32.  
  33. context_ref->set_source_rgb (line_color_.get_red_p(), line_color_.get_blue_p(), line_color_.get_green_p());

  34. context_ref->stroke_preserve();

  35.  
  36. context_ref->set_source_rgb (fill_color_.get_red_p(), fill_color_.get_blue_p(), fill_color_.get_green_p());

  37. context_ref->fill();

  38. }

  39.  
  40. protected:

  41. Gdk::Color line_color_;

  42. Gdk::Color fill_color_;

  43. };

  44.  


The last implementation is the ARROW_CIRCLE, like this:

For draw a circle we need to have the center and the radius. We can define a radius (using arrow_lenght_) and then put the center on the source line.

 
  1. class ArrowCircle : public ArrowHead

  2. {

  3. public:

  4. ArrowCircle() :

  5. ArrowHead(),

  6. line_color_( "black" ),

  7. fill_color_( "white" )

  8. {

  9. arrow_lenght_ = 7.0;

  10. }

  11.  
  12. virtual ~ArrowCircle()

  13. {

  14. }

  15.  
  16. void draw(Cairo::RefPtr< Cairo::Context > context_ref, double start_x, double start_y, double end_x, double end_y)

  17. {

  18. double angle = atan2 (end_y - start_y, end_x - start_x) + M_PI;

  19.  
  20. double xc = end_x + arrow_lenght_ * cos(angle);

  21. double yc = end_y + arrow_lenght_ * sin(angle);

  22.  
  23.  
  24. context_ref->arc (xc, yc, arrow_lenght_, 0.0, 2 * M_PI);

  25.  
  26. context_ref->set_source_rgb (line_color_.get_red_p(), line_color_.get_blue_p(), line_color_.get_green_p());

  27. context_ref->stroke_preserve();

  28.  
  29. context_ref->set_source_rgb (fill_color_.get_red_p(), fill_color_.get_blue_p(), fill_color_.get_green_p());

  30. context_ref->fill();

  31. }

  32.  
  33. protected:

  34. Gdk::Color line_color_;

  35. Gdk::Color fill_color_;

  36. };

  37.  
  38.  


Now we need only two functions for set the begin and end arrow style:

 
  1. void CairoArea::setStartArrowStyle(ArrowHead::ArrowStyle style)

  2. {

  3. switch (style)

  4. {

  5. case ArrowHead::ARROW_OPEN:

  6. start_arrow_head_ptr_ = std::auto_ptr< ArrowHead >( new ArrowOpen() );

  7. break;

  8. case ArrowHead::ARROW_SOLID_FILLED:

  9. start_arrow_head_ptr_ = std::auto_ptr< ArrowHead >( new ArrowSolid() );

  10. break;

  11. case ArrowHead::ARROW_DIAMOND_FILLED:

  12. start_arrow_head_ptr_ = std::auto_ptr< ArrowHead >( new ArrowDiamond() );

  13. break;

  14. case ArrowHead::ARROW_CIRCLE_FILLED:

  15. start_arrow_head_ptr_ = std::auto_ptr< ArrowHead >( new ArrowCircle() );

  16. break;

  17. }

  18. }

  19.  
  20. void CairoArea::setEndArrowStyle(ArrowHead::ArrowStyle style)

  21. {

  22. switch (style)

  23. {

  24. case ArrowHead::ARROW_OPEN:

  25. end_arrow_head_ptr_ = std::auto_ptr< ArrowHead >( new ArrowOpen() );

  26. break;

  27. case ArrowHead::ARROW_SOLID_FILLED:

  28. end_arrow_head_ptr_ = std::auto_ptr< ArrowHead >( new ArrowSolid() );

  29. break;

  30. case ArrowHead::ARROW_DIAMOND_FILLED:

  31. end_arrow_head_ptr_ = std::auto_ptr< ArrowHead >( new ArrowDiamond() );

  32. break;

  33. case ArrowHead::ARROW_CIRCLE_FILLED:

  34. end_arrow_head_ptr_ = std::auto_ptr< ArrowHead >( new ArrowCircle() );

  35. break;

  36. }

  37.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值