using System; using CPF.Drawing; using CPF.ReoGrid.Drawing; namespace CPF.ReoGrid.Common { internal static class GraphicsToolkit { public static bool PointInRect(Rect rect, Point p) { return rect.Left <= p.X && rect.Top <= p.Y && rect.Right >= p.X && rect.Bottom >= p.Y; } public static double DistancePointToLine(Point startPoint, Point endPoint, Point target) { return GraphicsToolkit.DistancePointToLine(startPoint.X, startPoint.Y, endPoint.X, endPoint.Y, target); } public static double DistancePointToLine(float x1, float y1, float x2, float y2, Point target) { double num = (double)(y2 - y1); double num2 = (double)(x1 - x2); double num3 = (double)(x2 * y1 - x1 * y2); return Math.Abs(num * (double)target.X + num2 * (double)target.Y + num3) / Math.Sqrt(num * num + num2 * num2); } public static double DistancePointToPolygonBound(Point[] points, Point p, double min = 9999999.0, bool firstReturn = false) { double num = min + 1.0; Point startPoint = points[points.Length - 1]; foreach (Point point in points) { double num2 = GraphicsToolkit.DistancePointToLine(startPoint, point, p); bool flag = num > num2; if (flag) { num = num2; } startPoint = point; } return num; } public static double DistancePointToRectBounds(Rect rect, Point p, double min = 9999999.0, bool firstReturn = false) { Point[] array = new Point[4]; int num = 0; float left = rect.Left; float top = rect.Top; array[num] = new Point(ref left, ref top); int num2 = 1; float right = rect.Right; float top2 = rect.Top; array[num2] = new Point(ref right, ref top2); int num3 = 2; float right2 = rect.Right; float bottom = rect.Bottom; array[num3] = new Point(ref right2, ref bottom); int num4 = 3; float left2 = rect.Left; float bottom2 = rect.Bottom; array[num4] = new Point(ref left2, ref bottom2); return GraphicsToolkit.DistancePointToPolygonBound(array, p, min, firstReturn); } public static bool PointOnRectangleBounds(Rect rect, Point p, float borderWidth = 1f, double min = 9999999.0) { rect.Inflate(borderWidth, borderWidth); return rect.Contains(p) && GraphicsToolkit.DistancePointToRectBounds(rect, p, min, false) <= (double)borderWidth; } public static float AngleToArc(float width, float height, float angle) { return (float)(57.29577951308232 * Math.Atan2(Math.Sin((double)angle * 0.017453292519943295) * (double)height / (double)width, Math.Cos((double)angle * 0.017453292519943295))); } public static Point PointAtArc(Rect rect, float angle) { float num = (float)((double)GraphicsToolkit.AngleToArc(rect.Width, rect.Height, angle) * 0.017453292519943295); float num2 = rect.Width / 2f; float num3 = rect.Height / 2f; float num4 = (float)Math.Sin((double)num) * num2; float num5 = (float)Math.Cos((double)num) * num3; float num6 = rect.X + num2 + num4; float num7 = rect.Y + num3 - num5; return new Point(ref num6, ref num7); } public static void FillTriangle(DrawingContext g, float size, Point loc, GraphicsToolkit.TriangleDirection dir = GraphicsToolkit.TriangleDirection.Down) { CPFPen p = new CPFPen(Color.Black, 1f); GraphicsToolkit.FillTriangle(g, size, loc, dir, p); } public static void FillTriangle(DrawingContext g, float size, Point loc, GraphicsToolkit.TriangleDirection dir, CPFPen p) { float num = loc.X; float num2 = loc.Y; switch (dir) { case GraphicsToolkit.TriangleDirection.Left: loc.Y -= size / 2f; for (num2 = 0f; num2 < size / 2f; num2 += 1f) { Stroke stroke = p.Stroke; Brush brush = p.Brush; float num3 = loc.Y + num2; Point point = new Point(ref num, ref num3); float num4 = loc.Y + size - num2 - 1f; Point point2 = new Point(ref num, ref num4); g.DrawLine(stroke, brush, point, point2); num -= 1f; } break; case GraphicsToolkit.TriangleDirection.Up: loc.X -= size / 2f; for (num = 0f; num < size / 2f; num += 1f) { Stroke stroke = p.Stroke; Brush brush2 = p.Brush; float num3 = loc.X + num; Point point = new Point(ref num3, ref num2); float num4 = loc.X + size - num - 1f; Point point2 = new Point(ref num4, ref num2); g.DrawLine(stroke, brush2, point, point2); num2 -= 1f; } break; case GraphicsToolkit.TriangleDirection.Right: loc.Y -= size / 2f; for (num2 = 0f; num2 < size / 2f; num2 += 1f) { Stroke stroke = p.Stroke; Brush brush3 = p.Brush; float num3 = loc.Y + num2; Point point = new Point(ref num, ref num3); float num4 = loc.Y + size - num2 - 1f; Point point2 = new Point(ref num, ref num4); g.DrawLine(stroke, brush3, point, point2); num += 1f; } break; case GraphicsToolkit.TriangleDirection.Down: loc.X -= size / 2f - 1f; num2 -= 1f; for (num = 0f; num < size / 2f; num += 1f) { Stroke stroke = p.Stroke; Brush brush4 = p.Brush; float num3 = loc.X + num; Point point = new Point(ref num3, ref num2); float num4 = loc.X + size - num; Point point2 = new Point(ref num4, ref num2); g.DrawLine(stroke, brush4, point, point2); num2 += 1f; } break; } } private const double PIAngleDelta = 0.017453292519943295; public enum TriangleDirection { Left, Up, Right, Down } } }