Circle polygon

I'm currently trying to fit the diagonals of the decagon inside the circle

like this enter image description here

in C # my approach will create a circle

e.Graphics.DrawEllipse(myPen, 0, 0, 100, 100); 

and draw the lines inside using

  e.Graphics.DrawLine(myPen, 20, 5, 50, 50); 

after that I will draw a polygon-decagon.

I'm currently stuck on how to divide a circle into 10 parts / find the correct coordinates of points on a circle circumference because they are not good at math, I want to know how I recognize the next point in a circle circumference, the size of which is indicated above.
and I also want to ask the best approach to my problem.

Thanks:)

+7
math c # algorithm winforms
source share
3 answers

Just for grits and shins, here is a common implementation that will fit the X-sided polygon into the Rectangle that you pass. Note that in this approach, I actually do not calculate any absolute points. Instead, I translate the origin by rotating the surface and drawing lines only relative to the origin using a fixed length and angle. This is repeated in a loop to achieve the final result below and is very similar to the Turtle command in Logo :

Inscribed Polygon

 public partial class Form1 : Form { PictureBox pb = new PictureBox(); NumericUpDown nud = new NumericUpDown(); public Form1() { InitializeComponent(); this.Text = "Inscribed Polygon Demo"; TableLayoutPanel tlp = new TableLayoutPanel(); tlp.RowCount = 2; tlp.RowStyles.Clear(); tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize)); tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 100)); tlp.ColumnCount = 2; tlp.ColumnStyles.Clear(); tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize)); tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize)); tlp.Dock = DockStyle.Fill; this.Controls.Add(tlp); Label lbl = new Label(); lbl.Text = "Number of Sides:"; lbl.TextAlign = ContentAlignment.MiddleRight; tlp.Controls.Add(lbl, 0, 0); nud.Minimum = 3; nud.Maximum = 20; nud.AutoSize = true; nud.ValueChanged += new EventHandler(nud_ValueChanged); tlp.Controls.Add(nud, 1, 0); pb.Dock = DockStyle.Fill; pb.Paint += new PaintEventHandler(pb_Paint); pb.SizeChanged += new EventHandler(pb_SizeChanged); tlp.SetColumnSpan(pb, 2); tlp.Controls.Add(pb, 0, 1); } void nud_ValueChanged(object sender, EventArgs e) { pb.Refresh(); } void pb_SizeChanged(object sender, EventArgs e) { pb.Refresh(); } void pb_Paint(object sender, PaintEventArgs e) { // make circle centered and 90% of PictureBox size: int Radius = (int)((double)Math.Min(pb.ClientRectangle.Width, pb.ClientRectangle.Height) / (double)2.0 * (double).9); Point Center = new Point((int)((double)pb.ClientRectangle.Width / (double)2.0), (int)((double)pb.ClientRectangle.Height / (double)2.0)); Rectangle rc = new Rectangle(Center, new Size(1, 1)); rc.Inflate(Radius, Radius); InscribePolygon(e.Graphics, rc, (int)nud.Value); } private void InscribePolygon(Graphics G, Rectangle rc, int numSides) { if (numSides < 3) throw new Exception("Number of sides must be greater than or equal to 3!"); float Radius = (float)((double)Math.Min(rc.Width, rc.Height) / 2.0); PointF Center = new PointF((float)(rc.Location.X + rc.Width / 2.0), (float)(rc.Location.Y + rc.Height / 2.0)); RectangleF rcF = new RectangleF(Center, new SizeF(1, 1)); rcF.Inflate(Radius, Radius); G.DrawEllipse(Pens.Black, rcF); float Sides = (float)numSides; float ExteriorAngle = (float)360 / Sides; float InteriorAngle = (Sides - (float)2) / Sides * (float)180; float SideLength = (float)2 * Radius * (float)Math.Sin(Math.PI / (double)Sides); for (int i = 1; i <= Sides; i++) { G.ResetTransform(); G.TranslateTransform(Center.X, Center.Y); G.RotateTransform((i - 1) * ExteriorAngle); G.DrawLine(Pens.Black, new PointF(0, 0), new PointF(0, -Radius)); G.TranslateTransform(0, -Radius); G.RotateTransform(180 - InteriorAngle / 2); G.DrawLine(Pens.Black, new PointF(0, 0), new PointF(0, -SideLength)); } } } 

I got the formula for the side length here in the Regular Polygonal Calculator .

+5
source share

One way to deal with this is to use the trigonometric functions sin and cos . Give them the desired angle in radians in the loop (you need a multiple of 2*Ο€/10 , i.e. a = i*Ο€/5 for i between 0 and 9 inclusive). R*sin(a) will give you a vertical offset from the origin; R*cos(a) will give you horizontal displacement.

Please note that sin and cos are in the range of -1 to 1 , so you will see both positive and negative results. You will need to add an offset to the center of your circle so that the points appear in the correct places.

Once you have created a list of points, connect point i with point i+1 . When you reach the ninth point, connect it to the starting point to complete the polygon.

+6
source share

I do not test it, but I think that everything is in order.

 #define DegreeToRadian(d) d * (Pi / 180) float r = 1; // radius float cX = 0; // centerX float cY = 0; // centerY int numSegment = 10; float angleOffset = 360.0 / numSegment; float currentAngle = 0; for (int i = 0; i < numSegment; i++) { float startAngle = DegreeToRadian(currentAngle); float endAngle = DegreeToRadian(fmod(currentAngle + angleOffset, 360)); float x1 = r * cos(startAngle) + cX; float y1 = r * sin(startAngle) + cY; float x2 = r * cos(endAngle) + cX; float y2 = r * sin(endAngle) + cY; currentAngle += angleOffset; // [cX, cY][x1, y1][x2, y2] } 

(fmod - C ++ function equals floatNumber% floatNumber)

+2
source share

All Articles