Skip to content

Instantly share code, notes, and snippets.

@pragmatrix
Last active December 22, 2015 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pragmatrix/6406094 to your computer and use it in GitHub Desktop.
Save pragmatrix/6406094 to your computer and use it in GitHub Desktop.
iOS arrow drawing code, ported to MonoTouch / C# from https://gist.github.com/mayoff/4146780
using System;
using System.Drawing;
using MonoTouch.CoreGraphics;
/// https://gist.github.com/mayoff/4146780
static class ArrowPath
{
public static CGPath pathWithArrowFromPoint(
PointF startPoint,
PointF endPoint,
float tailWidth,
float headWidth,
float headLength)
{
var dx = endPoint.X - startPoint.X;
var dy = endPoint.Y - startPoint.Y;
var length = (float)Math.Sqrt(dx*dx + dy*dy);
var points = getAxisAlignedArrowPoints(length, tailWidth, headWidth, headLength);
var transform = transformForStartPoint(startPoint, endPoint, length);
var path = new CGPath();
path.AddLines(transform, points);
path.CloseSubpath();
return path;
}
static PointF[] getAxisAlignedArrowPoints(
float length,
float tailWidth,
float headWidth,
float headLength)
{
var tailLength = length - headLength;
var points = new PointF[7];
points[0] = new PointF(0, tailWidth / 2);
points[1] = new PointF(tailLength, tailWidth / 2);
points[2] = new PointF(tailLength, headWidth / 2);
points[3] = new PointF(length, 0);
points[4] = new PointF(tailLength, -headWidth / 2);
points[5] = new PointF(tailLength, -tailWidth / 2);
points[6] = new PointF(0, -tailWidth / 2);
return points;
}
static CGAffineTransform transformForStartPoint(PointF startPoint, PointF endPoint, float length)
{
var cosine = (endPoint.X - startPoint.X) / length;
var sine = (endPoint.Y - startPoint.Y) / length;
return new CGAffineTransform(cosine, sine, -sine, cosine, startPoint.X, startPoint.Y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment