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

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

#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
{

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
//   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
endPt.v - arrowLength,
endPt.h + arrowLength,
endPt.v + arrowLength);

// Draw arrow head (a reversed wedge)

} // end ArrowLineTo()
```

Community Search:
MacTech Search:

Duplicate Annihilator 5.7.5 - Find and d...
Duplicate Annihilator takes on the time-consuming task of comparing the images in your iPhoto library using effective algorithms to make sure that no duplicate escapes. Duplicate Annihilator... Read more
BusyContacts 1.0.2 - Fast, efficient con...
BusyContacts is a contact manager for OS X that makes creating, finding, and managing contacts faster and more efficient. It brings to contact management the same power, flexibility, and sharing... Read more
Capture One Pro 8.2.0.82 - RAW workflow...
Capture One Pro 8 is a professional RAW converter offering you ultimate image quality with accurate colors and incredible detail from more than 300 high-end cameras -- straight out of the box. It... Read more
Backblaze 4.0.0.872 - Online backup serv...
Backblaze is an online backup service designed from the ground-up for the Mac.With unlimited storage available for \$5 per month, as well as a free 15-day trial, peace of mind is within reach with... Read more
Little Snitch gives you control over your private outgoing data. Track background activity As soon as your computer connects to the Internet, applications often have permission to send any... Read more
Monolingual 1.6.4 - Remove unwanted OS X...
Monolingual is a program for removing unnecesary language resources from OS X, in order to reclaim several hundred megabytes of disk space. If you use your computer in only one (human) language, you... Read more
CleanApp 5.0 - Application deinstaller a...
CleanApp is an application deinstaller and archiver.... Your hard drive gets fuller day by day, but do you know why? CleanApp 5 provides you with insights how to reclaim disk space. There are... Read more
Fantastical 2.0 - Create calendar events...
Fantastical is the Mac calendar you'll actually enjoy using. Creating an event with Fantastical is quick, easy, and fun: Open Fantastical with a single click or keystroke Type in your event details... Read more
Cocktail 8.2 - General maintenance and o...
Cocktail is a general purpose utility for OS X that lets you clean, repair and optimize your Mac. It is a powerful digital toolset that helps hundreds of thousands of Mac users around the world get... Read more
Direct Mail 4.0.4 - Create and send grea...
Direct Mail is an easy-to-use, fully-featured email marketing app purpose-built for OS X. It lets you create and send great looking email campaigns. Start your newsletter by selecting from a gallery... Read more

## Latest Forum Discussions

These are All the Apple Watch Apps and G...
The Apple Watch is less than a month from hitting store shelves, and once you get your hands on it you're probably going to want some apps and games to install. Fear not! We've compiled a list of all the Apple Watch apps and games we've been able to... | Read more »
Appy to Have Known You - Lee Hamlet Look...
Being at 148Apps these past 2 years has been an awesome experience that has taught me a great deal, and working with such a great team has been a privilege. Thank you to Rob Rich, and to both Rob LeFebvre and Jeff Scott before him, for helping me... | Read more »
Hands-On With Allstar Heroes - A Promisi...
Let’s get this out of the way quickly. Allstar Heroes looks a lot like a certain other recent action RPG release, but it turns out that while it’s not yet available here, Allstar Heroes has been around for much longer than that other title. Now that... | Read more »
Macho Man and Steve Austin Join the Rank...
WWE Immortals, by Warner Bros. Interactive Entertainment and WWE, has gotten a superstar update. You'll now have access to Macho Man Randy Savage and Steve Austin. Both characters have two different versions: Macho Man Randy Savage Renegade or Macho... | Read more »
Fearless Fantasy is Fantastic for the iF...
I actually had my first look at Fearless Fantasy last year at E3, but it was on a PC so there wasn't much for me to talk about. But now that I've been able to play with a pre-release version of the iOS build, there's quite a bit for me to talk... | Read more »
MLB Manager 2015 (Games)
MLB Manager 2015 5.0.14 Device: iOS Universal Category: Games Price: \$4.99, Version: 5.0.14 (iTunes) Description: Guide your favorite MLB franchise to glory! MLB Manager 2015, officially licensed by MLB.com and based on the award-... | Read more »
Breath of Light (Games)
Breath of Light 1.0.1421 Device: iOS Universal Category: Games Price: \$2.99, Version: 1.0.1421 (iTunes) Description: Hold a quiet moment. Breath of Light is a meditative and beautiful puzzle game with a hypnotic soundtrack by... | Read more »
WWE WrestleMania Tags into the App Store
Are You ready to rumble? The official WWE WrestleMania app, by World Wrestling Entertainment, is now available. Now you can get all your WrestleMania info in one place before anyone else. The app offers details on superstar signings, interactive... | Read more »
Bio Inc's New Expansion is Infectin...
Bio Inc., by DryGin Studios, is the real time strategy game where you infect a human body with the worst virus your evil brain can design. Recently, the game was updated to add a whole lot of new features. Now you can play the new “Lethal”... | Read more »
The Monocular Minion is Here! Despicable...
Despicable Me: Minion Rush, by Gameloft, is introducing a new runner to the mix in their latest update. Now you can play as Carl, the prankster minion. Carl has a few new abilities to play with, including running at a higher speed from the start.... | Read more »

## Price Scanner via MacPrices.net

13-inch 2.5GHz MacBook Pro (refurbished) avai...
The Apple Store has Apple Certified Refurbished 13″ 2.5GHz MacBook Pros available for \$829, or \$270 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free: - 13″ 2.... Read more
Save up to \$80 on iPad Air 2s, NY tax only, f...
B&H Photo has iPad Air 2s on sale for \$80 off MSRP including free shipping plus NY sales tax only: - 16GB iPad Air 2 WiFi: \$469.99 \$30 off - 64GB iPad Air 2 WiFi: \$549.99 \$50 off - 128GB iPad... Read more
iMacs on sale for up to \$205 off MSRP
B&H Photo has 21″ and 27″ iMacs on sale for up to \$205 off MSRP including free shipping plus NY sales tax only: - 21″ 1.4GHz iMac: \$1019 \$80 off - 21″ 2.7GHz iMac: \$1189 \$110 off - 21″ 2.9GHz... Read more
Färbe Technik Offers iPhone Battery Charge LI...
Färbe Technik, which manufactures and markets of mobile accessories for Apple, Blackberry and Samsung mobile devices, is offering tips on how to keep your iPhone charged while in the field: •... Read more
Electronic Recyclers International CEO Urges...
Citing a recent story on CNBC about concerns some security professionals have about the forthcoming Apple Watch, John Shegerian, Chairman and CEO of Electronic Recyclers International (ERI), the... Read more
Save up to \$380 with Apple refurbished iMacs
The Apple Store has Apple Certified Refurbished iMacs available for up to \$380 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free: - 27″ 3.5GHz 5K iMac – \$2119 \$... Read more
Mac minis on sale for up to \$75 off, starting...
MacMall has Mac minis on sale for up to \$75 off MSRP including free shipping. Their prices are the lowest available for these models from any reseller: - 1.4GHz Mac mini: \$459.99 \$40 off - 2.6GHz Mac... Read more
College Student Deals: Additional \$50 off Mac...
Take an additional \$50 off all MacBooks and iMacs at Best Buy Online with their College Students Deals Savings, valid through April 11, 2015. Anyone with a valid .EDU email address can take advantage... Read more
Mac Pros on sale for up to \$260 off MSRP
B&H Photo has Mac Pros on sale for up to \$260 off MSRP. Shipping is free, and B&H charges sales tax in NY only: - 3.7GHz 4-core Mac Pro: \$2799, \$200 off MSRP - 3.5GHz 6-core Mac Pro: \$3719.99... Read more
13-inch 2.5GHz MacBook Pro on sale for \$100 o...
B&H Photo has the 13″ 2.5GHz MacBook Pro on sale for \$999 including free shipping plus NY sales tax only. Their price is \$100 off MSRP. Read more

## Jobs Board

DevOps Software Engineer - *Apple* Pay, iOS...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring 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
Sr. Technical Services Consultant, *Apple*...
**Job Summary** Apple Professional Services (APS) has an opening for a senior technical position that contributes to Apple 's efforts for strategic and transactional Read more
Lead *Apple* Solutions Consultant - Retail...
**Job Summary** Job Summary The Lead ASC is an Apple employee who serves as the Apple business manager and influencer in a hyper-business critical Reseller's store Read more
*Apple* Pay - Site Reliability Engineer - Ap...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more