22 #define _USE_MATH_DEFINES 25 #include <cairo/cairo.h> 47 void findArrowHeadPoints(
float xBase,
float yBase,
float xPoint,
float yPoint,
float headToLengthRatio,
float width){
48 vecX=xPoint-xBase, vecY=yPoint-yBase;
49 float shaftLength=sqrt(powf(vecX,2.)+powf(vecY,2.));
50 float theta=2*M_PI*.25;
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;
56 xSide2=xPoint-vecX*headToLengthRatio-orthX;
57 ySide1=yPoint-vecY*headToLengthRatio+orthY;
58 ySide2=yPoint-vecY*headToLengthRatio-orthY;
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);
105 void draw(cairo_t *cr,
float xBase,
float yBase,
float xPoint,
float yPoint,
float headToLengthRatio,
float width,
bool closed,
bool filled) {
107 cairo_move_to (cr, xBase, yBase);
110 scale=headToLengthRatio;
111 cairo_line_to (cr, xBase+vecX*(1-scale), yBase+vecY*(1-scale));
114 if (filled || closed)
115 cairo_new_sub_path (cr);
116 cairo_move_to (cr, xSide1, ySide1);
117 cairo_line_to (cr, xPoint, yPoint);
118 cairo_line_to (cr, xSide2, ySide2);
119 if (filled || closed)
120 cairo_close_path(cr);
131 #endif // CAIROARROW_H_
float ySide2
The arrow head side 2.
float vecY
The shaft vector transposed to the origin.
void draw(cairo_t *cr, float xBase, float yBase, float xPoint, float yPoint, float headToLengthRatio, float width, bool closed, bool filled)
void findArrowHeadPoints(float xBase, float yBase, float xPoint, float yPoint, float headToLengthRatio, float width)
float xSide2
The arrow head side 1.
CairoArrow(cairo_t *cr, float xBase, float yBase, float xPoint, float yPoint, float headToLengthRatio, float width, bool closed, bool filled)