GDIObjective CVisual Studio 6Visual C++ 7.0Windows 2000Visual C++ 6.0Windows XP中级开发Visual StudioWindowsC++
绘制箭头






4.56/5 (20投票s)
2002年12月1日

232029

7171
如何向任意 DC 绘制箭头(
说明
基本上,有人请求了一些绘制箭头的代码,而我之前从未见过任何相关代码。另外,我实在没心情挂窗帘或吸尘。
Using the Code
这是一个用于绘制带箭头线段的简单 API。它看起来像这样
// ARROWSTRUCT
//
// Defines the attributes of an arrow.
typedef struct tARROWSTRUCT {
int nWidth; // width (in pixels) of the full base of the arrowhead
float fTheta; // angle (in radians) at the arrow tip between the two
// sides of the arrowhead
bool bFill; // flag indicating whether or not the arrowhead should be
// filled
} ARROWSTRUCT;
// ArrowTo()
//
// Draws an arrow, using the current pen and brush, from the current position
// to the passed point using the attributes defined in the ARROWSTRUCT.
void ArrowTo(HDC hDC, int x, int y, ARROWSTRUCT *pArrow);
void ArrowTo(HDC hDC, const POINT *lpTo, ARROWSTRUCT *pArrow);
只需用所需的属性填充一个 ARROWSTRUCT
,确保当前的 DC 位置正确(使用 MoveTo()
等),然后调用其中一个 ArrowTo()
函数。尺寸参数(nWidth
和 fTheta
)定义如下
技术
这要追溯到高中代数和三角学。ArrowTo()
函数首先构建一个完整的线段向量。然后,它根据你传递的 nWidth
和 fTheta
属性计算箭头两侧的点。搞定,你得到了箭头。
这里有一些伪代码
lineVector = toPoint - fromPoint
lineLength = length of lineVector
// calculate point at base of arrowhead
tPointOnLine = nWidth / (2 * (tanf(fTheta) / 2) * lineLength);
pointOnLine = toPoint + -tPointOnLine * lineVector
// calculate left and right points of arrowhead
normalVector = (-lineVector.y, lineVector.x)
tNormal = nWidth / (2 * lineLength)
leftPoint = pointOnLine + tNormal * normalVector
rightPoint = pointOnLine + -tNormal * normalVector
历史
- 2002年12月1日 - 创建
许可证
本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。
作者可能使用的许可证列表可以在此处找到。