我用OpenGL画个箭头基本思路是这样的:
1、确定起点和终点;
2、确定方向;
3、确定箭翼的长度;
4、画出箭头两侧;
带箭头的线就画好了。
lineto(origin,dst)
//
float signX = 1.0;
float signY = 1.0;/// X,Y轴向的符号标记
float deltX = dst.X - origin.X;
float deltY = dst.Y-origin.Y;
float cosalph = deltX/sqrt(deltX*deltX+deltY*deltY);//算一算旋转的角度
float sinalph = deltY/sqrt(deltX*deltX+deltY*deltY);
float x = cosalph*(-箭翼长度*signX);
float y = sinalph*(-箭翼长度*signY);
if(deltX>=0)
{
signX =-1.0;
}
if(deltY>=0)
{
signY =-1.0;
}
/// 画带箭头的直线
glLineWidth(1.0);
glColor3f(1.0,0,0);
glEnable(GL_LINE_SMOOTH);
glBegin(GL_LINES);
glVertex2f(originX,originY);
glVertex2f(dstX,dstY);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex2f((x*cos(-0.5)- y*sin(0.5))+dstX,(x*sin(-0.5)+y*cos(-0.5))+dstY); /\
glVertex2f(dstX,dstY); / | \
glVertex2f((x*cos(0.5)- y*sin(0.5))+dstX,(x*sin(0.5)+y*cos(0.5))+dstY); |
glEnd(); |
glDisable(GL_LINE_SMOOTH);
张开的角度实际是箭头两边的线相对于中间的线旋转的角度,实例中0.5是通过旋转角度计算得到的。