{"id":3684,"date":"2020-12-19T12:42:17","date_gmt":"2020-12-19T17:42:17","guid":{"rendered":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/?p=3684"},"modified":"2020-12-19T12:46:20","modified_gmt":"2020-12-19T17:46:20","slug":"turtle-graphics-spiral-in-c","status":"publish","type":"post","link":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/?p=3684","title":{"rendered":"Turtle Graphics Spiral In C#"},"content":{"rendered":"<p>As another example of what you can do with graphics in C#, I decided to recreate a famous Turtle Graphics spiral design. I can&#8217;t find an official name for this classic design. I decided to call it Spiral of Circles. It has a nautilus shape but you won&#8217;t find any sample code by doing a search on &#8220;Turtle Graphics nautilus&#8221;. There is a version of this design using squares. I did have the code for creating this design using Python or Processing. It took a little experimentation to reproduce it in C#. Part of the solution was to realize that radians needed to be converted to degrees which is why I included the conversion functions in the code. This design requires a rotate and a translate transformation within a loop. I&#8217;m not sure you really need to use a GraphicsPath object because I tried that before I solved the other problems.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.ComponentModel;\r\nusing System.Data;\r\nusing System.Drawing;\r\nusing System.Drawing.Drawing2D;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Windows.Forms;\r\n\r\nnamespace SpiralOfCircles\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n\r\n        System.Drawing.Graphics graphicsObj;\r\n\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n        }\r\n\r\n        private void Form1_Paint(object sender, PaintEventArgs e)\r\n        {\r\n            graphicsObj = this.CreateGraphics();\r\n\r\n            \/\/ Set the SmoothingMode property to smooth the lines.\r\n            graphicsObj.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;\r\n\r\n            \/\/ This centers what is drawn. Adjusted by 50 pixels.\r\n            graphicsObj.TranslateTransform((graphicsObj.VisibleClipBounds.Width \/ 2) + 50, (graphicsObj.VisibleClipBounds.Height \/ 2) - 50, MatrixOrder.Append);\r\n            \/\/ This scales everything up\r\n            graphicsObj.ScaleTransform(2, 2);\r\n\r\n            \/\/ Create pen.\r\n            Pen blackPen = new Pen(Color.Black, 1);\r\n\r\n            for (int i = 0; i &lt; 50; i++)\r\n            {\r\n                graphicsObj.RotateTransform((float)degrees((-Math.PI \/ 18)));\r\n\r\n                \/\/ Create a GraphicsPath object\r\n                GraphicsPath path = new GraphicsPath();\r\n\r\n                \/\/ Draw a circle\r\n                path.AddEllipse(0, 0, -i * 3, -i * 3);\r\n\r\n                \/\/ Create a Matrix object  \r\n                Matrix X = new Matrix();\r\n                X.Translate(i, 0);\r\n\r\n                \/\/ Apply transformation  \r\n                path.Transform(X);\r\n                graphicsObj.DrawPath(blackPen, path);\r\n            }\r\n        }\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ Function to convert degrees to radians\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"degrees\"&gt;degree value&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;equivalent radian value&lt;\/returns&gt;\r\n        public static double radians(double degrees)\r\n        {\r\n            double radians = (Math.PI \/ 180) * degrees;\r\n            return (radians);\r\n        }\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ Function to convert radians to degree \r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"radians\"&gt;radian value&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;equivalent degree value&lt;\/returns&gt;\r\n        public static double degrees(double radians)\r\n        {\r\n            double degrees = (180 \/ Math.PI) * radians;\r\n            return (degrees);\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<\/div>\n<div id=\"attachment_3685\" style=\"width: 410px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3685\" src=\"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/wp-content\/uploads\/2020\/12\/CSharp-Turtle-Graphics-Spiral-Of-Circles-400x400.jpg\" alt=\"Spiral Of Circles\" class=\"size-medium wp-image-3685\" width=\"400\" height=\"400\" srcset=\"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/wp-content\/uploads\/2020\/12\/CSharp-Turtle-Graphics-Spiral-Of-Circles-400x400.jpg 400w, http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/wp-content\/uploads\/2020\/12\/CSharp-Turtle-Graphics-Spiral-Of-Circles-150x150.jpg 150w, http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/wp-content\/uploads\/2020\/12\/CSharp-Turtle-Graphics-Spiral-Of-Circles-144x144.jpg 144w, http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/wp-content\/uploads\/2020\/12\/CSharp-Turtle-Graphics-Spiral-Of-Circles.jpg 600w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><p id=\"caption-attachment-3685\" class=\"wp-caption-text\">Spiral Of Circles<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>As another example of what you can do with graphics in C#, I decided to recreate a famous Turtle Graphics spiral design. I can&#8217;t find an official name for this classic design. I decided to call it Spiral of Circles. &hellip; <a href=\"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/?p=3684\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,12],"tags":[359,1884,1886,1885],"_links":{"self":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3684"}],"collection":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3684"}],"version-history":[{"count":3,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3684\/revisions"}],"predecessor-version":[{"id":3688,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3684\/revisions\/3688"}],"wp:attachment":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3684"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}