TweetFollow Us on Twitter

Arrowheads
Volume Number:1
Issue Number:9
Column Tag:C WORKSHOP

How to Draw Arrowheaded Lines

How to Draw Arrowheaded Lines

While writing a new application, I discovered I needed to have lines with arrowheads (like the ones we all love in MacDraw). Not only that, I needed to have the line stop on the surface of an object (usually an oval or rectangle).

The first place to look, of course, is to see how MacDraw did it. After using MacDraw for some time in various applications, I was surprised to see that the arrowheads were not 3-pointed polygons but actually small wedges! This was discovered after placing an arrowhead on a thick line and noticing the curvature of the arrowhead.

Keeping with the Macintosh spirit, I decided to make the call similar to the ones used in QuickDraw (i.e. - Line and LineTo). Instead of using a LineTo call, this routine may be substituted and all of the LineTo's changed to ArrowLineTo's.

The parameter passed to it is the absolute horizontal and vertical coordinates of the end point of the line (as in LineTo). The end point is also where the arrowhead will be drawn.

The basic algorithm is simple -

1). Compute the slope of the line

2). Draw the line

3). Point the wedge in the opposite direction of the line (to create the effect of an arrowhead).

4). Draw the wedge

There is a fantastic Mac routine called PtToAngle that will compute the angle for you! All you need to do is to place a rectangle around the starting point, make the call, and the angle is returned to you (in degrees)!

The following code (written in Mac C) is pretty much self explanatory. You could experiment with the constants arrowWidth and arrowLength (as shown in Fig. 1) to vary the width and length of the arrowhead. Note - this routine could be changed so that these constants are passed as parameters, for applications that need varying sizes of arrowheads.

/************************************************************************

         ArrowLineTo.c

 Copyright 1985, FlottWare

 ************************************************************************/

#include "MacCDefs.h"// Mac ROM data structure definitions
#include "Events.h"
#include "Window.h"

extern struct P_Str *CtoPstr();
int strlen(str) char *str;
  {int i=0; while (str[i++]); return i-1;}
  
/*------------------------------------------------------------------------
    Global Data
   ------------------------------------------------------------------------*/
 
#define False 0
#define True 0xFF

Rect      windowRect = {40,4,336,508}; // Window Rectangle
WindowPtr windowPtr; // Window Pointer

EventRecord event; // Current event record

/*------------------------------------------------------------------------
    main()
   ------------------------------------------------------------------------*/
 
main()
  { 
  
  if (CatchSignal()) // Is the user quitting this application?
    ExitToShell(); // Go to Finder when done
    
  init(); // Init Mac 
 
  while (True)   // Poll events forever 
    { 
    SystemTask();// Perform System duties
    
    if (GetNextEvent(everyEvent, &event))    // Get the next event 
      {
      
      switch ( event.what ) // Which event is it? 
 {

 case autoKey:
 case keyDown:                         // Key was pressed 
 case mouseDown: // Mouse Button pressed 
   {
          Signal("All Done"); // Y - quit application
   break;
   } // case autoKey,keyDown,mouseDown:
   
 } // switch (event.what)      
      } // if (Getevent)
    } // while (true)
  } // main
  
/************************************************************************ 

   Windows
 ***********************************************************************/ 

 
/*---------------------------------------------------------------------- 
 
   init() -   
  ----------------------------------------------------------------------*/

init()
  {
   // Open a window
   
  windowPtr = NewWindow(0,&windowRect,CtoPstr("Hit Key or Mouse to Quit "),True,
                             documentProc,-1,True,0);
  SetPort(windowPtr);
  
  #define centerH 250
  #define centerV 150
  #define offset   50
   // Draw some lines with arrowheads
   
  MoveTo(centerH,centerV);
  ArrowLineTo(centerH-offset,centerV);
  
  MoveTo(centerH,centerV);
  ArrowLineTo(centerH-offset,centerV+offset);
  
  MoveTo(centerH,centerV);
  ArrowLineTo(centerH,centerV+offset);
  
  MoveTo(centerH,centerV);
  ArrowLineTo(centerH+offset,centerV+offset);
  
  MoveTo(centerH,centerV);
  ArrowLineTo(centerH+offset,centerV);
  
  MoveTo(centerH,centerV);
  ArrowLineTo(centerH+offset,centerV-offset);
  
  MoveTo(centerH,centerV);
  ArrowLineTo(centerH,centerV-offset);
  
  MoveTo(centerH,centerV);
  ArrowLineTo(centerH-offset,centerV-offset);
  
  } // end init()

/*----------------------------------------------------------------------
      ArrowLineTo(horiz,vert)
   
     This routine draws a line from the current pen location
 to point (h,v) with an arrowhead (wedge) on the end of it.
 
        Inputs : horiz       - horizontal coord of end point
                 vert        - vertical   coord of end point
  
 Written by: Rick Flott                           Mac C (Consulair) V 
1.0
 ----------------------------------------------------------------------*/

ArrowLineTo(horiz,vert)
  short horiz;
  short vert;
  {
   // Size of starting Rect used in PtToAngle
  #define rectOffset  30
   // Arrowhead (wedge) constants
 //   arrowWidth - width of 1/2 arrowhead (in degrees)
   //   arrowLength - length of arrowhead in pixels
  #define arrowWidth  25
  #define arrowLength 10
  
 // Rects used in angle calculations
  Rect  startRect; //   starting Rect used in PtToAngle
  Rect  arrowheadRect;    //   the Rect that the wedge is drawn in

  short arrowAngle;// Angle of arrowhead (in degrees)          
 
  
  Point startPt,endPt;    // Start,End points of line
  
  GetPen(&startPt);// Get the current pen location
 // Set up a rectangle around the starting point
   //    (this is needed for the PtToAngle routine)
  SetRect (&startRect,startPt.h - rectOffset,
                      startPt.v - rectOffset,
                      startPt.h + rectOffset,
       startPt.v + rectOffset);
       
  SetPt(&endPt,horiz,vert); // Set up the end point
    
   // Calculate the angle (in degrees) of the line segment
  PtToAngle(&startRect,&endPt,&arrowAngle);
   
  LineTo(horiz,vert);// Draw the line
  
  arrowAngle -= (180 + arrowWidth);// Create a arrowhead with a wedge 
facing the opposite direction

   // Set up a Rect for the wedge around the endpoint
  SetRect (&arrowheadRect,endPt.h - arrowLength,
                          endPt.v - arrowLength,
                          endPt.h + arrowLength,
           endPt.v + arrowLength);
     
        // Draw arrow head (a reversed wedge)
  PaintArc(&arrowheadRect,arrowAngle,2*arrowWidth);
  
  } // end ArrowLineTo()
 
AAPL
$118.93
Apple Inc.
-0.07
MSFT
$47.81
Microsoft Corpora
+0.06
GOOG
$541.83
Google Inc.
+1.46

MacTech Search:
Community Search:

Software Updates via MacUpdate

Adobe Photoshop Elements 13.0 - Consumer...
Adobe Photoshop Elements 12--the #1 selling consumer photo editing software--helps you edit pictures with powerful, easy-to-use options and share them via print, the web, Facebook, and more.Version... Read more
Skype 7.2.0.412 - Voice-over-internet ph...
Skype allows you to talk to friends, family and co-workers across the Internet without the inconvenience of long distance telephone charges. Using peer-to-peer data transmission technology, Skype... Read more
HoudahSpot 3.9.6 - Advanced file search...
HoudahSpot is a powerful file search tool built upon MacOS X Spotlight. Spotlight unleashed Create detailed queries to locate the exact file you need Narrow down searches. Zero in on files Save... Read more
RapidWeaver 6.0.3 - Create template-base...
RapidWeaver is a next-generation Web design application to help you easily create professional-looking Web sites in minutes. No knowledge of complex code is required, RapidWeaver will take care of... Read more
iPhoto Library Manager 4.1.10 - Manage m...
iPhoto Library Manager lets you organize your photos into multiple iPhoto libraries. Separate your high school and college photos from your latest summer vacation pictures. Or keep some photo... Read more
iExplorer 3.5.1.9 - View and transfer al...
iExplorer is an iPhone browser for Mac lets you view the files on your iOS device. By using a drag and drop interface, you can quickly copy files and folders between your Mac and your iPhone or... Read more
MacUpdate Desktop 6.0.3 - Discover and i...
MacUpdate Desktop 6 brings seamless 1-click installs and version updates to your Mac. With a free MacUpdate account and MacUpdate Desktop 6, Mac users can now install almost any Mac app on macupdate.... Read more
SteerMouse 4.2.2 - Powerful third-party...
SteerMouse is an advanced driver for USB and Bluetooth mice. It also supports Apple Mighty Mouse very well. SteerMouse can assign various functions to buttons that Apple's software does not allow,... Read more
iMazing 1.1 - Complete iOS device manage...
iMazing (was DiskAid) is the ultimate iOS device manager with capabilities far beyond what iTunes offers. With iMazing and your iOS device (iPhone, iPad, or iPod), you can: Copy music to and from... Read more
PopChar X 7.0 - Floating window shows av...
PopChar X helps you get the most out of your font collection. With its crystal-clear interface, PopChar X provides a frustration-free way to access any font's special characters. Expanded... Read more

Latest Forum Discussions

See All

Mystery Case Files: Dire Grove, Sacred G...
Mystery Case Files: Dire Grove, Sacred Grove HD Review By Jennifer Allen on November 28th, 2014 Our Rating: iPad Only App - Designed for the iPad A decent new installment for the popular Mystery Case Files series.   | Read more »
Castaway Paradise – Tips, Tricks, and St...
Ahoy there, castaways: Were you curious about our own thoughts regarding this pristine shipwreck? Check out our Castaway Paradise review! Castaway Paradise is out for iOS, finally giving mobile gamers the opportunity to enjoy the idyllic lifestyle... | Read more »
Castaway Paradise VIP Subs are on Sale f...
Castaway Paradise VIP Subs are on Sale for a Limited Time, and a Special Holiday Update is Coming Soon Posted by Rob Rich on November 28th, 2014 [ | Read more »
Primitive Review
Primitive Review By Jordan Minor on November 28th, 2014 Our Rating: :: FOLK ARTUniversal App - Designed for iPhone and iPad True to its name, Primitive is about as straightforward as runners get.   | Read more »
7 tips to get ahead of the competition i...
7 tips to get ahead of the competition in Dynasty of Dungeons Posted by Simon Reed on November 28th, 2014 [ permalink ] Playcrab has launched their action-packed new dungeon crawler, Dynasty of Dungeons, today. | Read more »
Master of Tea Kung Fu Review
Master of Tea Kung Fu Review By Jordan Minor on November 28th, 2014 Our Rating: :: ONE DROP RULESUniversal App - Designed for iPhone and iPad Master of Tea Kung Fu is a creative and complex caffeinated brawler.   | Read more »
Monster Strike Review
Monster Strike Review By Campbell Bird on November 28th, 2014 Our Rating: :: BILLIARD STRATEGYUniversal App - Designed for iPhone and iPad Collect monsters and battle by flinging them across the battlefield in this strangely... | Read more »
Proun+ Review
Proun+ Review By Jennifer Allen on November 28th, 2014 Our Rating: :: TWITCHY RACINGUniversal App - Designed for iPhone and iPad Twitchy racing aplenty in Proun+, an enjoyably tricky title.   | Read more »
Lucha Amigos (Games)
Lucha Amigos 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Forget Ninja Turtles, and meet Wrestlers Turtles! Crazier, Spicier and…Bouncier! Sling carapaces of 7 Luchadores to knock all... | Read more »
Record of Agarest War Zero (Games)
Record of Agarest War Zero 1.0 Device: iOS Universal Category: Games Price: $7.99, Version: 1.0 (iTunes) Description: HyperDevbox Holiday Turkey Black Friday Special Pricing! To celebrate the opening of the holiday season HyperDevbox... | Read more »

Price Scanner via MacPrices.net

Best Black Friday Deal: 15-inch Retina MacBoo...
 B&H Photo has the new 2014 15″ Retina MacBook Pros on sale for $300 off MSRP as part of their Black Friday sale. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.2GHz Retina... Read more
Up To 75% Off Infovole Text Apps Over Black F...
Infovole’s entire range of apps, including the Textkraft family of word processors for iPads and iPhones, is being offered at 50-75% off over the Black Friday and Cyber Monday weekend. The five-day... Read more
Black Friday: Up to $60 off Mac minis, NY tax...
 B&H Photo has new 2014 Mac minis on sale for up to $60 off MSRP as part of their Black Friday sale. Shipping is free, and B&H charges NY sales tax only: - 1.4GHz Mac mini: $449.99 $50 off... Read more
Black Friday: 27-inch 5K iMac for $2299, save...
 B&H Photo continues to offer Black Friday sale prices on the 27″ 3.5GHz 5K iMac, in stock today and on sale for $2299 including free shipping plus NY sales tax only. Their price is $200 off MSRP... Read more
Karalux Announces 24K Gold-Plated iPhone 6
Karalux, a Vietnam-based jewellery firm, has launched a unique 24 karat gold-plated iPhone 6 version with gold-cast monolithic dragon on its back panel. The real 24 karat gold plated enclosure doesn’... Read more
Black Friday: 13-inch 2.6GHz Retina MacBook P...
 B&H Photo has lowered their price for the 13″ 2.6GHz/128GB Retina MacBook Pro to $1159 for Black Friday. That’s $140 off MSRP, and it’s the lowest price for this model (except for Apple’s $1099... Read more
View all the Black Friday sales on our Mac Pr...
We’ve updated our Mac Price Trackers with the latest information on prices, bundles, and availability on systems from Apple’s authorized internet/catalog resellers. View Black Friday sale prices at a... Read more
Black Friday: 11-inch MacBook Air for $779, s...
 Best Buy has lowered their price for the 2014 11″ 1.4GHz/128GB MacBook Air to $779.99 for Black Friday. That’s $120 off MSRP. Choose free shipping or free local store pickup (if available). Sale... Read more
Apple Store Black Friday sale for 2014: $100...
BLACK FRIDAY The Apple Store has posted their Black Friday deals for 2014. Receive a $100 PRODUCT(RED) branded iTunes gift card with the purchase of select Macs, $50 with iPads, and $25 with iPods,... Read more
Black Friday: 15% off iTunes Gift Cards
Staples is offering 15% off $50 and $100 iTunes Gift Cards on their online store as part of their Black Friday sale. Click here for more information. Shipping is free. Best Buy is offering $100... Read more

Jobs Board

Position Opening at *Apple* - Apple (United...
…Summary** As a Specialist, you help create the energy and excitement around Apple products, providing the right solutions and getting products into customers' hands. You Read more
Position Opening at *Apple* - Apple (United...
**Job Summary** Being a Business Manager at an Apple Store means you're the catalyst for businesses to discover and leverage the power, ease, and flexibility of Apple Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Senior Event Manager, *Apple* Retail Market...
…This senior level position is responsible for leading and imagining the Apple Retail Team's global event strategy. Delivering an overarching brand story; in-store, Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.