diff options
Diffstat (limited to 'src/ExtensionsMethods.cs')
| -rw-r--r-- | src/ExtensionsMethods.cs | 104 |
1 files changed, 95 insertions, 9 deletions
diff --git a/src/ExtensionsMethods.cs b/src/ExtensionsMethods.cs index 6697be62..340a8ceb 100644 --- a/src/ExtensionsMethods.cs +++ b/src/ExtensionsMethods.cs @@ -58,17 +58,18 @@ namespace Crow public static Cairo.PointD Substract(this Cairo.PointD p1, Cairo.PointD p2){ return new Cairo.PointD(p1.X - p2.X, p1.Y - p2.Y); } + public static Cairo.PointD Divide(this Cairo.PointD p1, double d){ + return new Cairo.PointD(p1.X / d, p1.Y / d); + } public static Cairo.PointD Add(this Cairo.PointD p1, Cairo.PointD p2){ return new Cairo.PointD(p1.X + p2.X, p1.Y + p2.Y); } public static Cairo.PointD Multiply(this Cairo.PointD p1, double v){ return new Cairo.PointD(p1.X * v, p1.Y * v); } - public static void DrawCote(this Cairo.Context ctx, Cairo.PointD p1, Cairo.PointD p2, double stroke = 1.0) - { - const double arrowWidth = 4.0; - const double arrowLength = 10.0; - + public static void DrawCote(this Cairo.Context ctx, Cairo.PointD p1, Cairo.PointD p2, + double stroke = 1.0, bool fill = false, double arrowWidth = 3.0, double arrowLength = 7.0) + { Cairo.PointD vDir = p2.Substract(p1); vDir = vDir.GetNormalized (); Cairo.PointD vPerp = vDir.GetPerp (); @@ -80,21 +81,77 @@ namespace Crow ctx.MoveTo (p1); ctx.LineTo (pA0.Add (vA)); - ctx.LineTo (pA0.Substract (vA)); + if (fill) + ctx.LineTo (pA0.Substract (vA)); + else + ctx.MoveTo (pA0.Substract (vA)); + ctx.LineTo (p1); ctx.MoveTo (p2); ctx.LineTo (pA1.Add (vA)); - ctx.LineTo (pA1.Substract (vA)); + if (fill) + ctx.LineTo (pA1.Substract (vA)); + else + ctx.MoveTo (pA1.Substract (vA)); ctx.LineTo (p2); - ctx.Fill (); + if (fill) + ctx.Fill (); ctx.MoveTo (p1); ctx.LineTo (p2); ctx.LineWidth = stroke; ctx.Stroke (); + } + public static void DrawCoteInverse(this Cairo.Context ctx, Cairo.PointD p1, Cairo.PointD p2, + double stroke = 1.0, bool fill = false, double arrowWidth = 3.0, double arrowLength = 7.0) + { + Cairo.PointD vDir = p2.Substract(p1); + vDir = vDir.GetNormalized (); + Cairo.PointD vPerp = vDir.GetPerp (); + + Cairo.PointD pA0 = p1.Add(vDir.Multiply(arrowLength)); + Cairo.PointD pA1 = p2.Substract(vDir.Multiply(arrowLength)); + + Cairo.PointD vA = vPerp.Multiply (arrowWidth); + + ctx.MoveTo (p1.Add (vA)); + ctx.LineTo (pA0); + ctx.LineTo (p1.Substract (vA)); + if (fill) + ctx.LineTo (p1.Add (vA)); + + ctx.MoveTo (p2.Add (vA)); + ctx.LineTo (pA1); + ctx.LineTo (p2.Substract (vA)); + + if (fill) { + ctx.LineTo (p2.Add (vA)); + ctx.Fill (); + } + ctx.MoveTo (pA0); + ctx.LineTo (pA1); + ctx.LineWidth = stroke; + ctx.Stroke (); + } + public static void DrawCoteFixed(this Cairo.Context ctx, Cairo.PointD p1, Cairo.PointD p2, + double stroke = 1.0, double coteWidth = 3.0) + { + Cairo.PointD vDir = p2.Substract(p1); + vDir = vDir.GetNormalized (); + Cairo.PointD vPerp = vDir.GetPerp (); + Cairo.PointD vA = vPerp.Multiply (coteWidth); + + ctx.MoveTo (p1.Add (vA)); + ctx.LineTo (p1.Substract (vA)); + ctx.MoveTo (p2.Add (vA)); + ctx.LineTo (p2.Substract (vA)); + ctx.MoveTo (p1); + ctx.LineTo (p2); + ctx.LineWidth = stroke; + ctx.Stroke (); } public static void SetSourceColor(this Cairo.Context ctx, Color c) { @@ -106,6 +163,30 @@ namespace Crow } #endregion + public static Orientation GetOrientation(this Alignment a){ + return (a==Alignment.Left) ||(a==Alignment.Right) ? Orientation.Horizontal : Orientation.Vertical; + } + public static Alignment GetOpposite(this Alignment a){ + switch (a) { + case Alignment.Left: + return Alignment.Right; + case Alignment.Right: + return Alignment.Left; + case Alignment.Top: + return Alignment.Bottom; + case Alignment.Bottom: + return Alignment.Top; + case Alignment.TopLeft: + return Alignment.BottomRight; + case Alignment.TopRight: + return Alignment.BottomLeft; + case Alignment.BottomLeft: + return Alignment.TopRight; + case Alignment.BottomRight: + return Alignment.TopLeft; + } + return Alignment.Center; + } public static void Raise(this EventHandler handler, object sender, EventArgs e) { if(handler != null) @@ -120,7 +201,12 @@ namespace Crow handler(sender, e); } } - + public static byte[] GetBytes(this string str) + { + byte[] bytes = new byte[str.Length * sizeof(char)]; + System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); + return bytes; + } public static bool IsWhiteSpaceOrNewLine (this char c) { return c == '\t' || c == '\r' || c == '\n' || char.IsWhiteSpace (c); |