gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
CairoArrow.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 
18 #ifndef CAIROARROW_H_
19 #define CAIROARROW_H_
20 
21 #ifdef _MSC_VER
22 #define _USE_MATH_DEFINES
23 #endif
24 #include <math.h>
25 #include <cairo/cairo.h>
26 
32 class CairoArrow {
33  float xSide1, xSide2;
34  float ySide1, ySide2;
35  float vecX, vecY;
36 
47  void findArrowHeadPoints(float xBase, float yBase, float xPoint, float yPoint, float headToLengthRatio, float width){
48  vecX=xPoint-xBase, vecY=yPoint-yBase; // the vector form the base to the point translated to the origin.
49  float shaftLength=sqrt(powf(vecX,2.)+powf(vecY,2.));
50  float theta=2*M_PI*.25; // Find the orthogonal unit vector
51  float cosTheta=cos(theta), sinTheta=sin(theta);
52  float unitX=vecX/shaftLength, unitY=vecY/shaftLength;
53  float rotX=unitX*cosTheta-unitY*sinTheta, rotY=unitX*sinTheta+unitY*cosTheta;
54  float orthX=rotX*width, orthY=rotY*width;
55  xSide1=xPoint-vecX*headToLengthRatio+orthX; // find the arrow head side points based on the orthogonal unit vector to the shaft
56  xSide2=xPoint-vecX*headToLengthRatio-orthX;
57  ySide1=yPoint-vecY*headToLengthRatio+orthY;
58  ySide2=yPoint-vecY*headToLengthRatio-orthY;
59  }
60 
61 public:
70  CairoArrow(void) {
71  }
72 
89  CairoArrow(cairo_t *cr, float xBase, float yBase, float xPoint, float yPoint, float headToLengthRatio, float width, bool closed, bool filled) {
90  draw(cr, xBase, yBase, xPoint, yPoint, headToLengthRatio, width, closed, filled);
91  }
92 
105  void draw(cairo_t *cr, float xBase, float yBase, float xPoint, float yPoint, float headToLengthRatio, float width, bool closed, bool filled) {
106  findArrowHeadPoints(xBase, yBase, xPoint, yPoint, headToLengthRatio, width);
107  cairo_move_to (cr, xBase, yBase); // start at the base and draw the shaft
108  float scale=0.;
109  if (closed|filled) // if closed, then just draw up to the arrow head
110  scale=headToLengthRatio;
111  cairo_line_to (cr, xBase+vecX*(1-scale), yBase+vecY*(1-scale));
112  cairo_stroke (cr);
113  // now draw the arrow head
114  if (filled || closed) // close the path if filled or closed
115  cairo_new_sub_path (cr);
116  cairo_move_to (cr, xSide1, ySide1); // start at side1 and draw to the point, then side2
117  cairo_line_to (cr, xPoint, yPoint);
118  cairo_line_to (cr, xSide2, ySide2);
119  if (filled || closed) // close the path if filled or closed
120  cairo_close_path(cr);
121  if (!filled)
122  cairo_stroke (cr);
123  else
124  cairo_fill (cr);
125  }
126 
127  virtual ~CairoArrow() {
128  }
129 };
130 
131 #endif // CAIROARROW_H_
CairoArrow(void)
Definition: CairoArrow.H:70
float ySide2
The arrow head side 2.
Definition: CairoArrow.H:34
float vecY
The shaft vector transposed to the origin.
Definition: CairoArrow.H:35
void draw(cairo_t *cr, float xBase, float yBase, float xPoint, float yPoint, float headToLengthRatio, float width, bool closed, bool filled)
Definition: CairoArrow.H:105
void findArrowHeadPoints(float xBase, float yBase, float xPoint, float yPoint, float headToLengthRatio, float width)
Definition: CairoArrow.H:47
float vecX
Definition: CairoArrow.H:35
float xSide1
Definition: CairoArrow.H:33
float ySide1
Definition: CairoArrow.H:34
float xSide2
The arrow head side 1.
Definition: CairoArrow.H:33
virtual ~CairoArrow()
Definition: CairoArrow.H:127
CairoArrow(cairo_t *cr, float xBase, float yBase, float xPoint, float yPoint, float headToLengthRatio, float width, bool closed, bool filled)
Definition: CairoArrow.H:89
gtkIOStream: /tmp/gtkiostream/include/CairoArrow.H Source File
GTK+ IOStream  Beta