diff options
author | Tiago Conceição <Tiago_caza@hotmail.com> | 2021-06-12 05:56:20 +0300 |
---|---|---|
committer | Tiago Conceição <Tiago_caza@hotmail.com> | 2021-06-12 05:56:20 +0300 |
commit | 5d61e95331329a842c8547c04f40c68bcc922693 (patch) | |
tree | b1f23594e678af6ec4c379d85282c5c374d306cf /UVtools.Core/Extensions/DrawingExtensions.cs | |
parent | 470f5ec1cf87fff04ed88044e3b3beeeabd55609 (diff) |
v2.13.3v2.13.3
- **File formats:**
- (Add) CXDLP v2
- (Improved) GR1, MDLP, CXDLP decode and encode performance and memory optimization
- (Remove) CXDLP v1 from available formats
- (Add) Pixel editor - Drawing: New brushes of shapes/polygons
- (Upgrade) .NET from 5.0.6 to 5.0.7
- (Fix) When there are issues on the list, executing any operation will navigate to the last layer
- (Fix) PrusaSlicer printer: Rename "Creality HALOT-SKY CL-60" to "Creality HALOT-ONE CL-60"
Diffstat (limited to 'UVtools.Core/Extensions/DrawingExtensions.cs')
-rw-r--r-- | UVtools.Core/Extensions/DrawingExtensions.cs | 126 |
1 files changed, 43 insertions, 83 deletions
diff --git a/UVtools.Core/Extensions/DrawingExtensions.cs b/UVtools.Core/Extensions/DrawingExtensions.cs index db5ec69..d8f4385 100644 --- a/UVtools.Core/Extensions/DrawingExtensions.cs +++ b/UVtools.Core/Extensions/DrawingExtensions.cs @@ -21,88 +21,23 @@ namespace UVtools.Core.Extensions return Color.FromArgb(r, g, b); } - public static double CalculateSideLength(int sides, int radius) + public static double CalculatePolygonSideLengthFromRadius(double radius, int sides) { return 2 * radius * Math.Sin(Math.PI / sides); } - /*public static Point[] GetPolygonVertices(int sides, int radius, Point center, double startingAngle = 0) + public static double CalculatePolygonVerticalLengthFromRadius(double radius, int sides) { - if (sides < 3) - throw new ArgumentException("Polygons can't have less than 3 sides...", nameof(sides)); - - - // Fix rotation - switch (sides) - { - case 3: - startingAngle += 90; - break; - case 4: - startingAngle += 45; - break; - case 5: - startingAngle += 22.5; - break; - } - - var points = new Point[sides]; - var step = 360.0 / sides; - int i = 0; - for (var angle = startingAngle; angle < startingAngle + 360.0; angle += step) //go in a circle - { - if (i == sides) break; // Fix floating problem - double radians = angle * Math.PI / 180.0; - points[i++] = new( - (int) Math.Round(Math.Cos(radians) * radius + center.X), - (int) Math.Round(Math.Sin(-radians) * radius + center.Y) - ); - } - - return points; - }*/ + return radius * Math.Cos(Math.PI / sides); + } - /*public static Point[] GetPolygonVertices(int sides, int radius, Point center, double startingAngle = 0) + public static double CalculatePolygonRadiusFromSideLength(double length, int sides) { - startingAngle = -45; - if (sides < 3) - throw new ArgumentException("Polygons can't have less than 3 sides...", nameof(sides)); - - var vertex = new Point[sides]; - //var deg = (180.0 * (sides - 2)) / sides + startingAngle; - var deg = ((180.0 * (sides - 2) / sides) - 180) / 2 + startingAngle; - var step = 360.0 / sides; - var rad = deg * (Math.PI / 180); - - double nSinDeg = Math.Sin(rad); - double nCosDeg = Math.Cos(rad); - - vertex[0] = center; - //vertex[0].X += radius; - vertex[0].Y -= radius; - //vertex[0].X += (int)Math.Cos(deg) / 2 *radius; - //vertex[0].Y -= (int)Math.Sin(deg) / 2 *radius; - int length = (int)Math.Round(CalculateSideLength(sides, radius)); - - for (int i = 1; i < vertex.Length; i++) - { - vertex[i] = new( - (int)Math.Round(vertex[i - 1].X - nCosDeg * length), - (int)Math.Round(vertex[i - 1].Y - nSinDeg * length)); - - - //recalculate the degree for the next vertex - deg -= step; - rad = deg * (Math.PI / 180); - - nSinDeg = Math.Sin(rad); - nCosDeg = Math.Cos(rad); - - } - return vertex; - }*/ + var theta = 360.0 / sides; + return length / (2 * Math.Cos((90 - theta / 2) * Math.PI / 180.0)); + } - public static Point[] GetPolygonVertices(int sides, int radius, Point center, double startingAngle = 0) + public static Point[] GetPolygonVertices(int sides, int radius, Point center, double startingAngle = 0, bool flipHorizontally = false, bool flipVertically = false) { if (sides < 3) throw new ArgumentException("Polygons can't have less than 3 sides...", nameof(sides)); @@ -110,18 +45,23 @@ namespace UVtools.Core.Extensions var vertices = new Point[sides]; double deg = 360.0 / sides;//calculate the rotation angle - //double a = radius * Math.Cos(Math.PI / sides);//calculate vertical length - //double s = CalculateSideLength(sides, radius);//calculate the side length var rad = Math.PI / 180.0; + var x0 = center.X + radius * Math.Cos(-(((180 - deg) / 2) + startingAngle) * rad); + var y0 = center.Y - radius * Math.Sin(-(((180 - deg) / 2) + startingAngle) * rad); + + var x1 = center.X + radius * Math.Cos(-(((180 - deg) / 2) + deg + startingAngle) * rad); + var y1 = center.Y - radius * Math.Sin(-(((180 - deg) / 2) + deg + startingAngle) * rad); + vertices[0] = new( - (int) Math.Round(center.X + radius * Math.Cos(-(((180 - deg) / 2) + startingAngle) * rad)), - (int) Math.Round(center.Y - radius * Math.Sin(-(((180 - deg) / 2) + startingAngle) * rad))); + (int) Math.Round(x0), + (int) Math.Round(y0) + ); vertices[1] = new( - (int) Math.Round(center.X + radius * Math.Cos(-(((180 - deg) / 2) + deg + startingAngle) * rad)), - (int) Math.Round(center.Y - radius * Math.Sin(-(((180 - deg) / 2) + deg + startingAngle) * rad) - )); + (int) Math.Round(x1), + (int) Math.Round(y1) + ); for (int i = 0; i < sides - 2; i++) { @@ -129,11 +69,31 @@ namespace UVtools.Core.Extensions double dcosrot = Math.Cos((deg * (i + 1)) * rad); vertices[i + 2] = new( - (int)Math.Round(center.X + dcosrot * (vertices[1].X - center.X) - dsinrot * (vertices[1].Y - center.Y)), - (int)Math.Round(center.Y + dsinrot * (vertices[1].X - center.X) + dcosrot * (vertices[1].Y - center.Y)) + (int)Math.Round(center.X + dcosrot * (x1 - center.X) - dsinrot * (y1 - center.Y)), + (int)Math.Round(center.Y + dsinrot * (x1 - center.X) + dcosrot * (y1 - center.Y)) ); } + if (flipHorizontally) + { + var startX = center.X - radius; + var endX = center.X + radius; + for (int i = 0; i < sides; i++) + { + vertices[i].X = endX - (vertices[i].X - startX); + } + } + + if (flipVertically) + { + var startY = center.Y - radius; + var endY = center.Y + radius; + for (int i = 0; i < sides; i++) + { + vertices[i].Y = endY - (vertices[i].Y - startY); + } + } + return vertices; } } |