Click here to Skip to main content
Click here to Skip to main content

Drawing Arrows

By , 30 Nov 2002
 

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:

// 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:

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

About the Author

Jamie Hale
Web Developer
Canada Canada
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membermanoj kumar choubey22 Feb '12 - 0:02 
SuggestionSome suggestionsmemberherohuyongtao8 Oct '11 - 20:52 
GeneralCorrecting the math for better arrow headsmemberbstrack17 Feb '10 - 8:09 
GeneralquestionmemberLuo Rui4 Dec '06 - 2:24 
GeneralFreaks out in release modemembermark-w24 Sep '06 - 8:30 
GeneralRe: Freaks out in release modemembermark-w24 Sep '06 - 8:55 
GeneralRe: Freaks out in release modememberJamie Hale25 Sep '06 - 4:23 
GeneralRe: Freaks out in release modemembermark-w30 Sep '06 - 19:30 
AnswerRe: Freaks out in release modememberClemensR18 Sep '07 - 5:10 
GeneralRe: Freaks out in release modememberkkokkal14 Jan '08 - 20:23 
GeneralJust another approachmemberprcarp10 May '06 - 3:27 
GeneralRe: Just another approachmemberrsantos0123 Nov '06 - 4:54 
GeneralI am looking for a simple MFC Control with Draw Areamemberp2002ad25 May '05 - 6:14 
GeneralLength of arrowmemberHemant kulkarni2 Dec '02 - 20:11 
GeneralRe: Length of arrowmemberPhilippe Lhoste2 Dec '02 - 23:07 
GeneralRe: Length of arrowmemberPhilippe Lhoste3 Dec '02 - 1:53 
GeneralRe: Length of arrowmemberJamie Hale3 Dec '02 - 4:50 
GeneralRe: Length of arrowmemberJamie Hale3 Dec '02 - 4:51 
GeneralHeight of the arrowheadmemberMetalRob14 Nov '10 - 23:48 
GeneralRe: Height of the arrowheadmemberJamie Hale15 Nov '10 - 0:00 
GeneralRe: Height of the arrowheadmemberMetalRob15 Nov '10 - 0:17 
GeneralRe: Height of the arrowheadmemberMetalRob15 Nov '10 - 0:18 
GeneralAnother way: matrix transformation (don't be scared it's easy!) :)memberDamir Valiulin2 Dec '02 - 13:05 
GeneralRe: Another way: matrix transformation (don't be scared it's easy!) :)memberJamie Hale3 Dec '02 - 4:48 
Generalnice, but GDI+ can do it bettermemberafisser2 Dec '02 - 2:15 
GeneralRe: nice, but GDI+ can do it bettermemberJamie Hale2 Dec '02 - 5:27 
GeneralRe: nice, but GDI+ can do it bettermemberPhilippe Lhoste2 Dec '02 - 23:01 
GeneralFirst non-joke thread!editorNishant S1 Dec '02 - 14:11 
GeneralRe: First non-joke thread!memberShog91 Dec '02 - 15:07 
GeneralRe: First non-joke thread!memberJamie Hale1 Dec '02 - 17:02 
QuestionWhere are the other 7?editorNishant S1 Dec '02 - 14:09 
QuestionWhat????memberzenboy1 Dec '02 - 12:28 
AnswerRe: What????memberzarzor1 Dec '02 - 12:46 
GeneralRe: What????memberzenboy1 Dec '02 - 13:08 
GeneralRe: What????memberzarzor1 Dec '02 - 13:33 
GeneralIn what context !?!?!?!?!?memberzenboy1 Dec '02 - 19:01 
GeneralRe: In what context !?!?!?!?!?memberzarzor2 Dec '02 - 11:02 
GeneralRe: In what context !?!?!?!?!?memberTomáš Petříček5 May '03 - 7:59 
GeneralRe: In what context !?!?!?!?!?memberOldViking4 Jun '03 - 7:26 
AnswerRe: What????memberJamie Hale1 Dec '02 - 14:09 
GeneralRe: What????memberzenboy1 Dec '02 - 16:51 
GeneralRe: What????memberJamie Hale1 Dec '02 - 17:04 
GeneralJamie check your e-mail...memberzenboy1 Dec '02 - 17:05 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 1 Dec 2002
Article Copyright 2002 by Jamie Hale
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid