Last active
December 13, 2024 15:37
-
-
Save aspose-com-kb/244687329a6784db20ca8b0bb176ce7f to your computer and use it in GitHub Desktop.
Draw Pentagon using C#. For more details: https://kb.aspose.com/drawing/net/draw-pentagon-using-csharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Aspose.Drawing; | |
class PentagonDrawer | |
{ | |
static void Main() | |
{ | |
var drawingLicense = new License(); | |
drawingLicense.SetLicense("license.lic"); // Set the Aspose license | |
// Define the pentagon's parameters | |
int len = 100; | |
double circumcircleRadius = len / (2 * Math.Sin(Math.PI / 5)); | |
int canvasCenterX = 100; | |
int canvasCenterY = 100; | |
// Calculate the vertices of the pentagon | |
PointF[] vertices = new PointF[5]; | |
for (int vertexIndex = 0; vertexIndex < 5; vertexIndex++) | |
{ | |
double angleRadians = 2 * Math.PI * vertexIndex / 5 - Math.PI / 2; // Rotate to start from top | |
float xCoordinate = (float)(canvasCenterX + circumcircleRadius * Math.Cos(angleRadians)); | |
float yCoordinate = (float)(canvasCenterY + circumcircleRadius * Math.Sin(angleRadians)); | |
vertices[vertexIndex] = new PointF(xCoordinate, yCoordinate); | |
} | |
// Create and save the pentagon image | |
using (Bitmap canvas = new Bitmap(200, 200)) | |
using (Graphics graphicsContext = Graphics.FromImage(canvas)) | |
{ | |
graphicsContext.Clear(Color.Blue); // Set background color | |
graphicsContext.FillPolygon(Brushes.Cyan, vertices); // Fill pentagon with color | |
// graphicsContext.DrawPolygon(Pens.Black, vertices); // Optional outline | |
canvas.Save("pentagon.png"); // Save the image to file | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment