Exemplo n.º 1
0
        public static SKPath AsRotatedAndroidPath(this PathF target, PointF center, float ppu, float zoom, float angle)
        {
            ppu = zoom * ppu;

            var path = new SKPath();

            var pointIndex        = 0;
            var arcAngleIndex     = 0;
            var arcClockwiseIndex = 0;

            foreach (var type in target.SegmentTypes)
            {
                if (type == PathOperation.Move)
                {
                    var point = target.GetRotatedPoint(pointIndex++, center, angle);
                    path.MoveTo(point.X * ppu, point.Y * ppu);
                }
                else if (type == PathOperation.Line)
                {
                    var endPoint = target.GetRotatedPoint(pointIndex++, center, angle);
                    path.LineTo(endPoint.X * ppu, endPoint.Y * ppu);
                }
                else if (type == PathOperation.Quad)
                {
                    var controlPoint1 = target.GetRotatedPoint(pointIndex++, center, angle);
                    var endPoint      = target.GetRotatedPoint(pointIndex++, center, angle);
                    path.QuadTo(
                        controlPoint1.X * ppu,
                        controlPoint1.Y * ppu,
                        endPoint.X * ppu,
                        endPoint.Y * ppu);
                }
                else if (type == PathOperation.Cubic)
                {
                    var controlPoint1 = target.GetRotatedPoint(pointIndex++, center, angle);
                    var controlPoint2 = target.GetRotatedPoint(pointIndex++, center, angle);
                    var endPoint      = target.GetRotatedPoint(pointIndex++, center, angle);
                    path.CubicTo(
                        controlPoint1.X * ppu,
                        controlPoint1.Y * ppu,
                        controlPoint2.X * ppu,
                        controlPoint2.Y * ppu,
                        endPoint.X * ppu,
                        endPoint.Y * ppu);
                }
                else if (type == PathOperation.Arc)
                {
                    var topLeft     = target[pointIndex++];
                    var bottomRight = target[pointIndex++];
                    var startAngle  = target.GetArcAngle(arcAngleIndex++);
                    var endAngle    = target.GetArcAngle(arcAngleIndex++);
                    var clockwise   = target.GetArcClockwise(arcClockwiseIndex++);

                    while (startAngle < 0)
                    {
                        startAngle += 360;
                    }

                    while (endAngle < 0)
                    {
                        endAngle += 360;
                    }

                    var rect  = new SKRect(topLeft.X, topLeft.Y, bottomRight.X, bottomRight.Y);
                    var sweep = Geometry.GetSweep(startAngle, endAngle, clockwise);

                    startAngle *= -1;
                    if (!clockwise)
                    {
                        sweep *= -1;
                    }

                    path.AddArc(rect, startAngle, sweep);
                }
                else if (type == PathOperation.Close)
                {
                    path.Close();
                }
            }

            return(path);
        }
Exemplo n.º 2
0
        public static CGPath AsRotatedCGPath(this PathF target, PointF center, float ppu, float zoom, float angle)
        {
            ppu = ppu * zoom;
            var path = new CGPath();

            int pointIndex        = 0;
            int arcAngleIndex     = 0;
            int arcClockwiseIndex = 0;

            foreach (var type in target.SegmentTypes)
            {
                if (type == PathOperation.Move)
                {
                    var point = target.GetRotatedPoint(pointIndex++, center, angle);
                    path.MoveToPoint(point.X * ppu, point.Y * ppu);
                }
                else if (type == PathOperation.Line)
                {
                    var endPoint = target.GetRotatedPoint(pointIndex++, center, angle);
                    path.AddLineToPoint(endPoint.X * ppu, endPoint.Y * ppu);
                }
                else if (type == PathOperation.Quad)
                {
                    var controlPoint = target.GetRotatedPoint(pointIndex++, center, angle);
                    var endPoint     = target.GetRotatedPoint(pointIndex++, center, angle);
                    path.AddQuadCurveToPoint(
                        controlPoint.X * ppu,
                        controlPoint.Y * ppu,
                        endPoint.X * ppu,
                        endPoint.Y * ppu);
                }
                else if (type == PathOperation.Cubic)
                {
                    var controlPoint1 = target.GetRotatedPoint(pointIndex++, center, angle);
                    var controlPoint2 = target.GetRotatedPoint(pointIndex++, center, angle);
                    var endPoint      = target.GetRotatedPoint(pointIndex++, center, angle);
                    path.AddCurveToPoint(
                        controlPoint1.X * ppu,
                        controlPoint1.Y * ppu,
                        controlPoint2.X * ppu,
                        controlPoint2.Y * ppu,
                        endPoint.X * ppu,
                        endPoint.Y * ppu);
                }
                else if (type == PathOperation.Arc)
                {
                    var   topLeft     = target[pointIndex++];
                    var   bottomRight = target[pointIndex++];
                    float startAngle  = target.GetArcAngle(arcAngleIndex++);
                    float endAngle    = target.GetArcAngle(arcAngleIndex++);
                    var   clockwise   = target.GetArcClockwise(arcClockwiseIndex++);

                    var startAngleInRadians = Geometry.DegreesToRadians(-startAngle);
                    var endAngleInRadians   = Geometry.DegreesToRadians(-endAngle);

                    while (startAngleInRadians < 0)
                    {
                        startAngleInRadians += (float)Math.PI * 2;
                    }

                    while (endAngleInRadians < 0)
                    {
                        endAngleInRadians += (float)Math.PI * 2;
                    }

                    var cx     = (bottomRight.X + topLeft.X) / 2;
                    var cy     = (bottomRight.Y + topLeft.Y) / 2;
                    var width  = bottomRight.X - topLeft.X;
                    var height = bottomRight.Y - topLeft.Y;
                    var r      = width / 2;

                    var rotatedCenter = Geometry.RotatePoint(center, new PointF(cx, cy), angle);

                    var transform = CGAffineTransform.MakeTranslation(rotatedCenter.X * ppu, rotatedCenter.Y * ppu);
                    transform = CGAffineTransform.Multiply(CGAffineTransform.MakeScale(1, height / width), transform);

                    path.AddArc(transform, 0, 0, r * ppu, startAngleInRadians, endAngleInRadians, !clockwise);
                }
                else if (type == PathOperation.Close)
                {
                    path.CloseSubpath();
                }
            }

            return(path);
        }