c# - Transcribing a polygon on a circle -
i try inscribe diagonals of decagon inside circle
like
in c# approach creating circle
e.graphics.drawellipse(mypen, 0, 0, 100, 100);
and draw lines inside using
e.graphics.drawline(mypen, 20, 5, 50, 50);
after draw decagon polygon.
currently im stuck @ how divide circle 10 parts/ finding correct coordiantes of points on circumference of circles because im not in math, want know how know next point in circumference of circle size of circle indicated above.
, want ask better approach problem.
thank :)
just grits , shins, here's generic implementation inscribe x-sided polygon rectangle pass it. note in approach i'm not calculating absolute points. instead, translating origin, rotating surface, , drawing lines respect origin using fixed length , angle. repeated in loop achieve end result below, , similar commanding turtle in logo:
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 , 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 greater or equal 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); (int = 1; <= 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 formula length of side here @ regular polygon calculator.
Comments
Post a Comment