Excuse
Basically, there was a request for some code to draw arrows and I had never seen any. Plus, I'm not really in the mood to hang drapes or vacuum.
Using the code
This is a simple API for drawing lines with arrowheads. It looks like this:
typedef struct tARROWSTRUCT {
int nWidth;
float fTheta;
bool bFill;
} ARROWSTRUCT;
void ArrowTo(HDC hDC, int x, int y, ARROWSTRUCT *pArrow);
void ArrowTo(HDC hDC, const POINT *lpTo, ARROWSTRUCT *pArrow);
Simply fill an ARROWSTRUCT
with the desired attributes, make sure the current DC position is correct (MoveTo()
, etc.), and call one of the two ArrowTo()
functions. The size parameters (nWidth
and fTheta
) are defined as follows:
Technique
This goes back to high-school algebra and trigonometry. The ArrowTo()
function first builds a vector of the full line. Then it calculates the points for the sides of the arrowhead based on the nWidth
and fTheta
attributes you pass. Badda-boom-badda-bing, you got your arrowhead.
Here's some pseudo-pseudocode:
lineVector = toPoint - fromPoint
lineLength = length of lineVector
tPointOnLine = nWidth / (2 * (tanf(fTheta) / 2) * lineLength);
pointOnLine = toPoint + -tPointOnLine * lineVector
normalVector = (-lineVector.y, lineVector.x)
tNormal = nWidth / (2 * lineLength)
leftPoint = pointOnLine + tNormal * normalVector
rightPoint = pointOnLine + -tNormal * normalVector
History
- December 1 2002 - Created.