Skip to content

Instantly share code, notes, and snippets.

@JigarM
Created October 29, 2016 06:12
Show Gist options
  • Save JigarM/054e03650bc9a3574873161715100371 to your computer and use it in GitHub Desktop.
Save JigarM/054e03650bc9a3574873161715100371 to your computer and use it in GitHub Desktop.
iOS arrow drawing code, ported to MonoTouch / C# from https://gist.github.com/mayoff/4146780 and https://gist.github.com/pragmatrix/6406094
using System;
using System.Drawing;
using MonoTouch.CoreGraphics;
/// https://gist.github.com/mayoff/4146780
///https://gist.github.com/pragmatrix/6406094
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