Exemple #1
0
        void DrawArc(SKCanvas canvas, SKPaint paint, double startAngleInDegrees, double endAngleInDegrees)
        {
            // find center and radius
            var centerx = (float)Bounds.Width / 2;
            var centery = (float)Bounds.Height / 2;

            var radius = Bounds.Width < Bounds.Height ? (float)Bounds.Width / 2 : (float)Bounds.Height / 2;

            var w = 0.558f;

            // convert degrees to radians
            var startAngleRadians = startAngleInDegrees * Math.PI / 180;
            var endAngleRadians   = endAngleInDegrees * Math.PI / 180;

            // find x,y coordinates for start and end points
            var startx     = centerx + radius * (float)Math.Cos(startAngleRadians);
            var starty     = centery + radius * (float)Math.Sin(startAngleRadians);
            var startPoint = new SKPoint(startx, starty);

            var endx     = centerx + radius * (float)Math.Cos(endAngleRadians);
            var endy     = centery + radius * (float)Math.Sin(endAngleRadians);
            var endPoint = new SKPoint(endx, endy);

            // find linear distance & midpoint between start and end points
            var linearDistance = Math.Sqrt(Math.Pow(endy - starty, 2) / Math.Pow(endx - startx, 2));
            var midx           = (startx + endx) / 2;
            var midy           = (starty + endy) / 2;
            var midPoint       = new SKPoint(midx, midy);

            // rotate end point 45 degrees counterclockwise around midpoint to find Conic function anchor
            var anchorPoint = RotatePoint(endPoint, midPoint, 45);

            // build path
            var path = new SKPath();

            //path.MoveTo (startx, starty);
            //path.ConicTo (anchorPoint.X, anchorPoint.Y, endx, endy, w);



            path.MoveTo(centerx, centery - radius);

            path.ConicTo(centerx + radius, centery - radius, centerx + radius, centery, w);
            path.ConicTo(centerx + radius, centery + radius, centerx, centery + radius, w);
            path.ConicTo(centerx - radius, centery + radius, centerx - radius, centery, w);
            //path.ConicTo (centerx - radius, centery - radius, centerx, centery - radius, w);

            //canvas.DrawPoints (SKPointMode.Points, new SKPoint [] { }, paint);

            canvas.DrawPath(path, paint);
        }
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            // Draw path with conic curve
            using (SKPath path = new SKPath())
            {
                path.MoveTo(touchPoints[0].Center);
                path.ConicTo(touchPoints[1].Center,
                             touchPoints[2].Center,
                             (float)weightSlider.Value);

                canvas.DrawPath(path, strokePaint);
            }

            // Draw tangent lines
            canvas.DrawLine(touchPoints[0].Center.X,
                            touchPoints[0].Center.Y,
                            touchPoints[1].Center.X,
                            touchPoints[1].Center.Y, dottedStrokePaint);

            canvas.DrawLine(touchPoints[1].Center.X,
                            touchPoints[1].Center.Y,
                            touchPoints[2].Center.X,
                            touchPoints[2].Center.Y, dottedStrokePaint);

            foreach (TouchPoint touchPoint in touchPoints)
            {
                touchPoint.Paint(canvas);
            }
        }
Exemple #3
0
 internal static void AddConic(IToolContext context, ConicShape conic, double dx, double dy, SKPath geometry)
 {
     geometry.MoveTo(ToSKPoint(conic.StartPoint, dx, dy));
     geometry.ConicTo(
         ToSKPoint(conic.Point1, dx, dy),
         ToSKPoint(conic.Point2, dx, dy),
         (float)conic.Weight);
 }
Exemple #4
0
 /// <summary>
 /// Draws layout entities.
 /// </summary>
 /// <param name="State">Current drawing state.</param>
 /// <param name="PathState">Current path state.</param>
 /// <param name="Path">Path being generated.</param>
 public override void Draw(DrawingState State, PathState PathState, SKPath Path)
 {
     if (this.defined)
     {
         this.P0 = Path.LastPoint;
         this.P1 = PathState.Add(this.xCoordinate, this.yCoordinate);
         this.P2 = PathState.Add(this.xCoordinate2, this.yCoordinate2);
         Path.ConicTo(this.P1, this.P2, this.weight);
     }
 }
Exemple #5
0
        /// <summary>
        /// Draws layout entities.
        /// </summary>
        /// <param name="State">Current drawing state.</param>
        /// <param name="PathState">Current path state.</param>
        /// <param name="Path">Path being generated.</param>
        public virtual void Draw(DrawingState State, PathState PathState, SKPath Path)
        {
            if (this.defined)
            {
                this.P0 = Path.LastPoint;
                this.P1 = new SKPoint(this.xCoordinate, this.yCoordinate);
                this.P2 = new SKPoint(this.xCoordinate2, this.yCoordinate2);

                PathState.Set(this.xCoordinate, this.yCoordinate);
                PathState.Set(this.xCoordinate2, this.yCoordinate2);
                Path.ConicTo(this.P1, this.P2, this.weight);
            }
        }
Exemple #6
0
        private static SKPath CreatePath(float curveStart, float curveEnd, float width)
        {
            SKPath  path   = new SKPath();
            SKPoint point0 = new SKPoint(0, curveStart);
            SKPoint point1 = new SKPoint(width / 2, curveEnd);
            SKPoint point2 = new SKPoint(width, curveStart);

            path.MoveTo(0, 0);
            path.LineTo(point0);
            path.ConicTo(point1, point2, (float).5);
            path.LineTo(width, 0);
            path.LineTo(0, 0);
            path.Close();
            return(path);
        }
Exemple #7
0
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            // Translate to center
            canvas.Translate(info.Width / 2, info.Height / 2);

            // Draw the circle
            float radius = Math.Min(info.Width, info.Height) / 4;

            canvas.DrawCircle(0, 0, radius, blackStroke);

            // Get the value of the Slider
            float angle = (float)angleSlider.Value;

            // Calculate sin and cosine for half that angle
            float sin = (float)Math.Sin(Math.PI * angle / 180 / 2);
            float cos = (float)Math.Cos(Math.PI * angle / 180 / 2);

            // Find the points and weight
            SKPoint point0 = new SKPoint(-radius * sin, radius * cos);
            SKPoint point1 = new SKPoint(0, radius / cos);
            SKPoint point2 = new SKPoint(radius * sin, radius * cos);
            float   weight = cos;

            // Draw the points
            canvas.DrawCircle(point0.X, point0.Y, 10, blackFill);
            canvas.DrawCircle(point1.X, point1.Y, 10, blackFill);
            canvas.DrawCircle(point2.X, point2.Y, 10, blackFill);

            // Draw the tangent lines
            canvas.DrawLine(point0.X, point0.Y, point1.X, point1.Y, dottedStroke);
            canvas.DrawLine(point2.X, point2.Y, point1.X, point1.Y, dottedStroke);

            // Draw the conic
            using (SKPath path = new SKPath())
            {
                path.MoveTo(point0);
                path.ConicTo(point1, point2, weight);
                canvas.DrawPath(path, redStroke);
            }
        }
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            float densityMultiplier = args.Info.Height / (float)SkiaCanvas.Height;
            float pixelHeight       = currentHeaderHeight * densityMultiplier;
            float pullPoint         = minHeaderHeight * densityMultiplier;

            canvas.Clear();

            SKPath  path   = new SKPath();
            SKPoint point0 = new SKPoint(0, pixelHeight);
            SKPoint point1 = new SKPoint(args.Info.Width / 2, pullPoint);
            SKPoint point2 = new SKPoint(args.Info.Width, pixelHeight);

            path.MoveTo(0, 0);
            path.LineTo(point0);
            path.ConicTo(point1, point2, (float)weightSlider.Value);
            path.LineTo(args.Info.Width, 0);
            path.LineTo(0, 0);
            path.Close();
            canvas.ClipPath(path, antialias: true);

            using (SKPaint paint = new SKPaint())
            {
                // Create bitmap tiling
                paint.Shader = SKShader.CreateBitmap(resourceBitmap,
                                                     SKShaderTileMode.Repeat,
                                                     SKShaderTileMode.Repeat);
                // Draw background
                canvas.DrawRect(info.Rect, paint);
            }


            //SKRect dest = new SKRect(0, 0, info.Width, info.Height);
            //canvas.DrawBitmap(resourceBitmap, dest, BitmapStretch.AspectFill, BitmapAlignment.Start, BitmapAlignment.Start);
        }
Exemple #9
0
        protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
        {
            base.OnPaintSurface(e);

            SKRect    skRect  = new SKRect(e.Info.Rect.Left, e.Info.Rect.Top, e.Info.Rect.Right, e.Info.Rect.Bottom);
            SKSurface surface = e.Surface;
            SKCanvas  canvas  = surface.Canvas;

            canvas.Clear(myBackgroundColor);

            SKPaint paint = new SKPaint
            {
                Style       = SKPaintStyle.Fill,
                TextSize    = 28,
                StrokeWidth = ChartWidth,
                IsAntialias = true
            };

            SKPaint paintAxis = new SKPaint
            {
                Style       = SKPaintStyle.Fill,
                TextSize    = 14,
                StrokeWidth = LineWidth,
                IsAntialias = true,
                Color       = myColorText
            };

            SKPaint paintLinesAxis = new SKPaint
            {
                Style       = SKPaintStyle.Fill,
                TextSize    = 14,
                StrokeWidth = LineWidth,
                IsAntialias = true,
                Color       = myColorAxis,
                StrokeCap   = SKStrokeCap.Round,
                PathEffect  = SKPathEffect.CreateDash(new[] { 2f, 6f }, 0)
            };

            //paint.Style = SKPaintStyle.Stroke;
            paint.Color = myColorAxis;

            // title
            //SKPoint ptTitle=new SKPoint(leftMargin,topMargin);
            //canvas.DrawText(myTitle, ptTitle, paint);
            using (var paintTitle = new SKPaint())
            {
                paintTitle.TextSize    = 24.0f;
                paintTitle.IsAntialias = true;
                paintTitle.Color       = myColorTitle;
                paintTitle.IsStroke    = true;
                paintTitle.StrokeWidth = 1;
                paintTitle.TextAlign   = SKTextAlign.Center;

                canvas.DrawText(myTitle, e.Info.Width / 2f, topMargin, paintTitle);
            }
            //Y Axis
            SKPoint ptAxis = new SKPoint(leftMargin + 2, topMargin);

            canvas.DrawText(myTitleY, ptAxis, paintAxis);

            // Y Axis
            ptAxis = new SKPoint(skRect.Right - rightMargin, skRect.Bottom - bottomMargin);
            canvas.DrawText(myTitleX, ptAxis, paintAxis);
            // draw axis
            SKPoint pt1 = new SKPoint(skRect.Left + leftMargin, skRect.Bottom - bottomMargin);
            SKPoint pt2 = new SKPoint(skRect.Right - rightMargin, skRect.Bottom - bottomMargin);

            canvas.DrawLine(pt1, pt2, paintAxis);

            pt1 = new SKPoint(skRect.Left + leftMargin, skRect.Bottom - bottomMargin);
            pt2 = new SKPoint(skRect.Left + leftMargin, skRect.Top - topMargin);
            canvas.DrawLine(pt1, pt2, paintAxis);

            List <SKPoint> pointList = new List <SKPoint>();

            if (timeSeries.Count > 0)
            {
                float onex = (skRect.Width - leftMargin - rightMargin) / timeSeries.Count;
                float oney = (skRect.Height - topMargin - bottomMargin) / timeSeries.Max;

                for (float st = 0; st < this.timeSeries.Max; st += stepY)
                {
                    ptAxis = new SKPoint(skRect.Left + leftMargin + 4, skRect.Bottom - bottomMargin - st * oney);
                    if (st > 0)
                    {
                        SKPoint ptLine = new SKPoint(skRect.Right - rightMargin, skRect.Bottom - bottomMargin - st * oney);
                        canvas.DrawLine(ptAxis, ptLine, paintLinesAxis);
                    }
                    canvas.DrawText(st.ToString("0"), ptAxis, paintAxis);
                }

                int index = 0;
                foreach (TimeValue val in timeSeries)
                {
                    float   x  = index * onex;
                    float   y  = oney * val.Value;
                    SKPoint pt = new SKPoint(leftMargin + x, skRect.Bottom - bottomMargin - y);
                    pointList.Add(pt);
                    index++;
                }

                SKPoint[] points = pointList.ToArray();
                paint.Style = SKPaintStyle.Stroke;
                paint.Color = myColorLine;
                if (pointList.Count > 0)
                {
                    SKPoint origin = new SKPoint(skRect.Left + leftMargin, skRect.Bottom - bottomMargin);
                    var     path   = new SKPath();

                    path.MoveTo(points.First().X, origin.Y);
                    path.LineTo(points.First());

                    var       last      = points.Length;
                    TimeValue lastMonth = null;
                    for (int i = 0; i < last; i++)
                    {
                        SKPoint   pt   = points[i];
                        TimeValue tval = timeSeries[i];
                        if (lastMonth == null || tval.DateTime.Month != lastMonth.DateTime.Month)
                        {
                            lastMonth = tval;
                            ptAxis    = new SKPoint(pt.X, skRect.Bottom - (bottomMargin / 2));
                            canvas.DrawText(lastMonth.DateTime.ToString("MM/yy"), ptAxis, paintAxis);
                        }
                        if (i == 0)
                        {
                            path.LineTo(pt);
                        }
                        else
                        {
                            SKPoint pt0 = points[i - 1];
                            path.ConicTo(pt0, pt, Math.Abs(pt.X - pt0.X));
                        }
                    }

                    canvas.DrawPath(path, paint);
                }
            }
        }
Exemple #10
0
 public void ConicTo(float controlX, float controlY, float x, float y, float w) =>
 _path.ConicTo(controlX, controlY, x, y, w);
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            canvas.Clear(SKColors.White);
            canvas.Scale(2);

            var points = new[]
            {
                new SKPoint(10, 10),
                new SKPoint(50, 20),
                new SKPoint(100, 150)
            };

            using (SKPaint paint = new SKPaint())
                using (SKPaint textPaint = new SKPaint())
                {
                    paint.Style       = SKPaintStyle.Stroke;
                    paint.StrokeWidth = 5;
                    paint.IsAntialias = true;
                    paint.StrokeCap   = SKStrokeCap.Round;

                    textPaint.IsAntialias = true;

                    using (SKPath path = new SKPath())
                    {
                        // create a conic path
                        path.MoveTo(points[0]);
                        path.ConicTo(points[1], points[2], 10);

                        // draw the conic-based path
                        paint.Color = SampleMedia.Colors.XamarinDarkBlue;
                        canvas.DrawPath(path, paint);

                        // get the quads from the conic points
                        SKPoint[] pts;
                        var       quads = SKPath.ConvertConicToQuads(points[0], points[1], points[2], 10, out pts, 2);

                        // move the points on a bit
                        for (int i = 0; i < pts.Length; i++)
                        {
                            pts[i].Offset(120, 0);
                        }
                        // draw the quad-based path
                        using (var quadsPath = new SKPath())
                        {
                            quadsPath.MoveTo(pts[0].X, pts[0].Y);
                            for (int i = 0; i < quads; i++)
                            {
                                var idx = i * 2;
                                quadsPath.CubicTo(
                                    pts[idx].X, pts[idx].Y,
                                    pts[idx + 1].X, pts[idx + 1].Y,
                                    pts[idx + 2].X, pts[idx + 2].Y);
                            }

                            paint.Color = SampleMedia.Colors.XamarinPurple;
                            canvas.DrawPath(quadsPath, paint);
                        }

                        // move the points on a bit
                        for (int i = 0; i < pts.Length; i++)
                        {
                            pts[i].Offset(120, 0);
                        }
                        // draw the dots
                        paint.Color = SampleMedia.Colors.XamarinGreen;
                        canvas.DrawPoints(SKPointMode.Points, pts, paint);
                    }
                }
        }
Exemple #12
0
        internal static void AddFigure(IToolContext context, IList <IBaseShape> shapes, bool isClosed, double dx, double dy, SKPath geometry)
        {
            bool isFirstShape = true;

            foreach (var shape in shapes)
            {
                switch (shape)
                {
                case LineShape line:
                {
                    if (isFirstShape)
                    {
                        geometry.MoveTo(ToSKPoint(line.StartPoint, dx, dy));
                        isFirstShape = false;
                    }
                    geometry.LineTo(ToSKPoint(line.Point, dx, dy));
                }
                break;

                case CubicBezierShape cubicBezier:
                {
                    if (isFirstShape)
                    {
                        geometry.MoveTo(ToSKPoint(cubicBezier.StartPoint, dx, dy));
                        isFirstShape = false;
                    }
                    geometry.CubicTo(
                        ToSKPoint(cubicBezier.Point1, dx, dy),
                        ToSKPoint(cubicBezier.Point2, dx, dy),
                        ToSKPoint(cubicBezier.Point3, dx, dy));
                }
                break;

                case QuadraticBezierShape quadraticBezier:
                {
                    if (isFirstShape)
                    {
                        geometry.MoveTo(ToSKPoint(quadraticBezier.StartPoint, dx, dy));
                        isFirstShape = false;
                    }
                    geometry.QuadTo(
                        ToSKPoint(quadraticBezier.Point1, dx, dy),
                        ToSKPoint(quadraticBezier.Point2, dx, dy));
                }
                break;

                case ConicShape conic:
                {
                    if (isFirstShape)
                    {
                        geometry.MoveTo(ToSKPoint(conic.StartPoint, dx, dy));
                        isFirstShape = false;
                    }
                    geometry.ConicTo(
                        ToSKPoint(conic.Point1, dx, dy),
                        ToSKPoint(conic.Point2, dx, dy),
                        (float)conic.Weight);
                }
                break;
                }
            }

            if (!isFirstShape && isClosed)
            {
                geometry.Close();
            }
        }