View Full Version : algorithm/code for drawing an arrow head
RedWolf
2002.05.26, 01:27 AM
I am drawing an arrow and am trying to figure out a simple algorithm for drawing the arrow head as a triangle. I know I can draw it using a polygon once I can figure out the three vertices. The catch is that the arrow could be at any angle.
Anyone know where I can find an algorithm or code to accomplish this? Thanks.
hyperzoanoid
2002.05.29, 11:56 AM
do you mean it will point in any direction or that it will have vertices with different angles? Im doing 2d so forgive me, but you can use the tangent, cosine, and sine or something they will give angles based on side measures. I need more info
CMagicPoker
2002.05.29, 03:21 PM
As hyperzoanoid asked before....
2D or 3D?
In 2D, do as hyperzoanoid said, it's the way to go, coco ;-)
In 3D, well, and you want it to be drawer using a polygon (you said 3 vertices, so it's a triangle?), not simple, dude...
I dont know what effect you seek, but...
OK, get the point of the end of the line in which you want to draw your arrow, this will be the offset of where you will put your triangle. You need the angle of the amount of how much rotated your triangle polygon will be rotated.
There's also a billboard thingee related...
Sorry, cant help, much, so much heat where I am...I am in a lamented state...so lamented...AAAAAHHAHAHA TOO HHOOTTT!!!!
RedWolf
2002.05.29, 11:12 PM
Originally posted by hyperzoanoid
do you mean it will point in any direction or that it will have vertices with different angles? Im doing 2d so forgive me, but you can use the tangent, cosine, and sine or something they will give angles based on side measures. I need more info
Yes, the arrow could point in any direction in 2D. I'm of course going to be attaching the "tail" to it as just a straight line. This line is of course trivial to do. As I said, I will want to draw the arrow head as a triangle using the QD polygon functions. The trick is figuring out an algorithm that will provide me the points given the arrow direction and the angle of the line. Thanks.
Codemattic
2002.05.30, 07:20 AM
This is off the top of my head - so if anybody sees a mistake feel free to correct my math. Well - first we need to know what angle (lets call it theta) the line is being drawn at. So use atan2(endY-startY, endX-startX) to get theta.
You should of made three points that represent the arrowhead. The center of the arrowhead should be at the origin (0,0). Then to rotate each of the points around the origin you use (i think - again off the top of my head)
//do this for all three points
translatedX = (originalX * cos(theta)) + (originalY * sin(theta));
translatedY = (-originalX * sin(theta)) + (originalY * cos(theta));
Then we want to move the arrowhead to the end of the line.
//do this for all three points
translatedX += endX;
translatedY += endY;
you now have the three points rotated and translated to where you want.
<rant>
You are probably experienced in quickdraw so you figure this is faster than learning quartz or something. But this would be incredibly easy in Quartz (or Java2D - since you are a java programmer).
</rant>
hope this helps,
Codemattic
RedWolf
2002.06.02, 12:28 AM
Thanks codemattic, I'll give that a try.
I need to stick with QuickDraw since this is for an application written in C++/C that must run on System 7.5 to Mac OS 9.2, as well as a Carbon app on OS X.
Codemattic
2002.06.02, 09:49 AM
ok - Quickdraw it is.
Remember tho - zero radians is to the right (not up). So when you choose your three starting points they should form an arrowhead pointing to the right - centered at the origin (0,0).
good luck,
Codemattic
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.