public void SamplePointsAndVectors() { var point1 = new SKPoint(50, 50); var point2 = new SKPoint(110, 200); var point3 = new SKPoint(80, 80); var vector = point2 - point1; var point4 = SKPoint.Add(point3, vector); var p1 = new SKPath(); p1.MoveTo(point1); p1.LineTo(point2); var p2 = new SKPath(); p2.MoveTo(point3); p2.LineTo(point4); SkiaSharpHelper.DrawLines(new[] { p1, p2 }); }
/// <summary> /// Applies the move and scale amount to the data point that make up the shape. /// </summary> private void ApplyMoveAndScale() { // Calculate origin offest var originOffest = new SKSize(PreviousRect.Left - Rect.Left, PreviousRect.Top - Rect.Top); // Get new center var center = Center; // Calculate offsets var offest = new SKSize(Rect.Left - PreviousRect.Left, Rect.Top - PreviousRect.Top); // Apply offest to all points for (int n = 0; n < Points.Count; ++n) { Points[n].ControlPoint = SKPoint.Add(Points[n].ControlPoint, offest); Points[n].EndPoint = SKPoint.Add(Points[n].EndPoint, offest); } // Calculate Size change var offsetX = 0f; var shrinkX = true; var offsetY = 0f; var shrinkY = true; if (Rect.Width < PreviousRect.Width) { offsetX = Rect.Width / PreviousRect.Width; shrinkX = true; } else { offsetX = PreviousRect.Width / Rect.Width; shrinkX = false; } if (Rect.Height < PreviousRect.Height) { offsetY = Rect.Height / PreviousRect.Height; shrinkY = true; } else { offsetY = PreviousRect.Height / Rect.Height; shrinkY = false; } if (!((int)offsetX == 1 && (int)offsetY == 1)) { // Apply offest to all points for (int n = 0; n < Points.Count; ++n) { // Get adjusted x,y var cx = Points[n].ControlPoint.X - PreviousCenter.X + originOffest.Width; var cy = Points[n].ControlPoint.Y - PreviousCenter.Y + originOffest.Height; var ex = Points[n].EndPoint.X - PreviousCenter.X + originOffest.Width; var ey = Points[n].EndPoint.Y - PreviousCenter.Y + originOffest.Height; // Calculate resized positions if (shrinkX) { cx = (cx * offsetX) + center.X; ex = (ex * offsetX) + center.X; } else { cx = (cx / offsetX) + center.X; ex = (ex / offsetX) + center.X; } if (shrinkY) { cy = (cy * offsetY) + center.Y; ey = (ey * offsetY) + center.Y; } else { cy = (cy / offsetY) + center.Y; ey = (ey / offsetY) + center.Y; } // Apply new points Points[n].ControlPoint = new SKPoint(cx, cy); Points[n].EndPoint = new SKPoint(ex, ey); } } }
public string DrawRegularPolygonSK(int vertexCount, float radius, SocketCommandContext context, [Optional] int rndNum) { string imagesPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data", "images"); if (!Directory.Exists(imagesPath)) { Directory.CreateDirectory(imagesPath); } using (SKBitmap bmp = new SKBitmap(1024, 1024)) using (SKPaint outerPaint = new SKPaint()) using (SKPaint innerPaint = new SKPaint()) using (SKPaint textPaint = new SKPaint()) using (SKCanvas canvas = new SKCanvas(bmp)) { SKPoint center = new SKPoint(bmp.Width / 2, bmp.Height / 2); var angle = Math.PI * 2 / vertexCount; var points = Enumerable.Range(0, vertexCount).Select(i => SKPoint.Add(center, new SKSize((float)Math.Sin(i * angle) * radius, (float)Math.Cos(i * angle) * radius))); var color = GetGuildUserRoleColor(context.User as SocketGuildUser).Result; canvas.Clear(); // this is for the filling of the polygon. innerPaint.Color = new SKColor(color.R, color.G, color.B); innerPaint.Style = SKPaintStyle.Fill; innerPaint.StrokeWidth = 5f; SKPath innerPath = new SKPath(); foreach (var point in points) { if (point == points.First()) { innerPath.MoveTo(point); } else { innerPath.LineTo(point); } } innerPath.Close(); canvas.DrawPath(innerPath, innerPaint); // this is for the lines of the polygon. outerPaint.Color = SKColors.Black; outerPaint.StrokeWidth = 10f; outerPaint.StrokeCap = SKStrokeCap.Round; outerPaint.Style = SKPaintStyle.Stroke; SKPath outerPath = new SKPath(); foreach (var point in points) { if (point == points.First()) { outerPath.MoveTo(point); } else { outerPath.LineTo(point); } } outerPath.Close(); canvas.DrawPath(outerPath, outerPaint); // this is for the text. textPaint.TextSize = 240f; textPaint.IsAntialias = true; if (color.R * 0.299f + color.G * 0.587f + color.B * 0.114f > 150) // decides the color of text based on bg color { textPaint.Color = SKColors.Black; } else { textPaint.Color = SKColors.White; } textPaint.Style = SKPaintStyle.StrokeAndFill; textPaint.StrokeWidth = 15f; textPaint.TextAlign = SKTextAlign.Center; textPaint.GetFontMetrics(out var skFontMetrics); canvas.DrawText(rndNum.ToString(), center.X, center.Y + skFontMetrics.CapHeight / 2, textPaint); // text's height is 188 pixels at size 240f and stroke 15f using (var stream = File.Create(Path.Combine(imagesPath, context.Message.Id.ToString() + ".png"))) { SKData data = SKImage.FromBitmap(bmp).Encode(SKEncodedImageFormat.Png, 100); data.SaveTo(stream); } return(Path.Combine(imagesPath, context.Message.Id.ToString() + ".png").ToString()); } }