Click here to Skip to main content
15,851,335 members
Articles / Multimedia / GDI

Drawing Arrows

Rate me:
Please Sign up or sign in to vote.
4.56/5 (22 votes)
30 Nov 2002 226.2K   7.1K   51   44
How to draw arrows (with arrowheads) to an arbitrary DC

Sample Image - Arrows.jpg

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:

C++
// 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);

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:

C++
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

History

  • December 1, 2002 - Created

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUrgent Pin
Member 104154789-May-14 6:34
Member 104154789-May-14 6:34 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey22-Feb-12 1:02
professionalManoj Kumar Choubey22-Feb-12 1:02 
SuggestionSome suggestions Pin
herohuyongtao8-Oct-11 21:52
herohuyongtao8-Oct-11 21:52 
GeneralCorrecting the math for better arrow heads Pin
bstrack17-Feb-10 9:09
bstrack17-Feb-10 9:09 
Generalquestion Pin
Luo Rui4-Dec-06 3:24
Luo Rui4-Dec-06 3:24 
GeneralFreaks out in release mode Pin
mark-w24-Sep-06 9:30
mark-w24-Sep-06 9:30 
GeneralRe: Freaks out in release mode Pin
mark-w24-Sep-06 9:55
mark-w24-Sep-06 9:55 
GeneralRe: Freaks out in release mode Pin
Jamie Hale25-Sep-06 5:23
Jamie Hale25-Sep-06 5:23 
GeneralRe: Freaks out in release mode Pin
mark-w30-Sep-06 20:30
mark-w30-Sep-06 20:30 
AnswerRe: Freaks out in release mode Pin
ClemensR18-Sep-07 6:10
ClemensR18-Sep-07 6:10 
GeneralRe: Freaks out in release mode Pin
kkokkal14-Jan-08 21:23
kkokkal14-Jan-08 21:23 
GeneralJust another approach Pin
prcarp10-May-06 4:27
prcarp10-May-06 4:27 
GeneralRe: Just another approach Pin
rsantos0123-Nov-06 5:54
rsantos0123-Nov-06 5:54 
GeneralI am looking for a simple MFC Control with Draw Area Pin
p2002ad25-May-05 7:14
p2002ad25-May-05 7:14 
Hello,

I am looking for a simple MFC Control with Draw Area.

I would like to:

0) place this Control in CDialog, CFormView

1) draw CDC Objects on it (e.g. CDC::Polyline, CDC::Rectangle)

2) Copy Drawing to Clipboard

3) Save Drawing to EMF

Many Thanks,
Jessica
GeneralLength of arrow Pin
Hemant kulkarni2-Dec-02 21:11
Hemant kulkarni2-Dec-02 21:11 
GeneralRe: Length of arrow Pin
Philippe Lhoste3-Dec-02 0:07
Philippe Lhoste3-Dec-02 0:07 
GeneralRe: Length of arrow Pin
Philippe Lhoste3-Dec-02 2:53
Philippe Lhoste3-Dec-02 2:53 
GeneralRe: Length of arrow Pin
Jamie Hale3-Dec-02 5:50
Jamie Hale3-Dec-02 5:50 
GeneralRe: Length of arrow Pin
Jamie Hale3-Dec-02 5:51
Jamie Hale3-Dec-02 5:51 
GeneralHeight of the arrowhead Pin
MetalRob15-Nov-10 0:48
MetalRob15-Nov-10 0:48 
GeneralRe: Height of the arrowhead Pin
Jamie Hale15-Nov-10 1:00
Jamie Hale15-Nov-10 1:00 
GeneralRe: Height of the arrowhead Pin
MetalRob15-Nov-10 1:17
MetalRob15-Nov-10 1:17 
GeneralRe: Height of the arrowhead Pin
MetalRob15-Nov-10 1:18
MetalRob15-Nov-10 1:18 
GeneralAnother way: matrix transformation (don't be scared it's easy!) :) Pin
Damir Valiulin2-Dec-02 14:05
Damir Valiulin2-Dec-02 14:05 
GeneralRe: Another way: matrix transformation (don't be scared it's easy!) :) Pin
Jamie Hale3-Dec-02 5:48
Jamie Hale3-Dec-02 5:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.