{"id":19029,"date":"2017-03-01T06:57:17","date_gmt":"2017-03-01T06:57:17","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=19029"},"modified":"2021-03-20T14:54:42","modified_gmt":"2021-03-20T14:54:42","slug":"recursive-turtle-graphics-program-to-draw-nested-squares","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/recursive-turtle-graphics-program-to-draw-nested-squares\/","title":{"rendered":"Recursive Turtle graphics program to draw nested squares"},"content":{"rendered":"<h3>Overview<\/h3>\n<p>In this tutorial, we will learn how to draw nested squares using a recursive turtle graphics program. We will use Eclipse IDE to run the python program.<\/p>\n<p>Create graphics window and turtle handle.<\/p>\n<pre>WindowTurtleScreen=turtle.Screen()\r\ntDocsTurtle = turtle.Turtle()<\/pre>\n<p>The recursive function is <strong>NestedSquare.<\/strong> This function calls itself. Initially, we will call the function from the main<\/p>\n<p>NestedSquare(tDocsTurtle,600,60)<\/p>\n<h3>Program<\/h3>\n<p>&nbsp;<\/p>\n<pre>#Import turtle module \r\nimport turtle\r\n\r\n#############################################\r\n# Recursive Function to draw square         #  \r\n# using Turtle graphics in Python           #\r\n#############################################\r\ndef NestedSquare(tDocsTurtle,side, delta):\r\n    if side &lt; delta: \r\n        return \r\n    \r\n    tDocsTurtle.penup()\r\n    tDocsTurtle.goto(-(side-delta)\/2, -(side-delta)\/2)\r\n    tDocsTurtle.pendown()\r\n    \r\n    for i in range(4):\r\n        tDocsTurtle.forward(side)\r\n        tDocsTurtle.left(90) \r\n\r\n    #Recursive call to draw inner square\r\n    NestedSquare(tDocsTurtle,side-delta,delta)\r\n    \r\n    \r\n########################################\r\n# Main function                        #\r\n########################################\r\ndef main():\r\n    WindowTurtleScreen=turtle.Screen()\r\n    tDocsTurtle = turtle.Turtle()\r\n    WindowTurtleScreen.title(\"NestedSquares - www.TestingDocs.com\")\r\n    tDocsTurtle.penup()\r\n    tDocsTurtle.goto(0, -300)\r\n    tDocsTurtle.pendown()\r\n    tDocsTurtle._write(\"www.TestingDocs.com\", \"right\",\"Arial\") \r\n    NestedSquare(tDocsTurtle,600,60) \r\n    WindowTurtleScreen.exitonclick()\r\n    \r\n\r\nmain()\r\n<\/pre>\n<h3><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-19042\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Turtle-Program-Nested-Squares.png\" alt=\"Turtle Program Nested Squares\" width=\"1660\" height=\"971\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Turtle-Program-Nested-Squares.png 1660w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Turtle-Program-Nested-Squares-300x175.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Turtle-Program-Nested-Squares-1024x599.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Turtle-Program-Nested-Squares-768x449.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Turtle-Program-Nested-Squares-1536x898.png 1536w\" sizes=\"auto, (max-width: 1660px) 100vw, 1660px\" \/><\/h3>\n<h3>Sample Output<\/h3>\n<p>Run the program.<\/p>\n<p>Run As &gt;&gt; Python Run<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-19040 size-full\" title=\"Nested Squares Turtle Graphics Output\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Nested-Squares-Turtle-Graphics-Output.png\" alt=\"Nested Squares Turtle Graphics Output\" width=\"1667\" height=\"1023\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Nested-Squares-Turtle-Graphics-Output.png 1667w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Nested-Squares-Turtle-Graphics-Output-300x184.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Nested-Squares-Turtle-Graphics-Output-1024x628.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Nested-Squares-Turtle-Graphics-Output-768x471.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Nested-Squares-Turtle-Graphics-Output-1536x943.png 1536w\" sizes=\"auto, (max-width: 1667px) 100vw, 1667px\" \/><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-19029\" data-postid=\"19029\" class=\"themify_builder_content themify_builder_content-19029 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Overview In this tutorial, we will learn how to draw nested squares using a recursive turtle graphics program. We will use Eclipse IDE to run the python program. Create graphics window and turtle handle. WindowTurtleScreen=turtle.Screen() tDocsTurtle = turtle.Turtle() The recursive function is NestedSquare. This function calls itself. Initially, we will call the function from the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[211],"tags":[],"class_list":["post-19029","post","type-post","status-publish","format-standard","hentry","category-python","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/19029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=19029"}],"version-history":[{"count":10,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/19029\/revisions"}],"predecessor-version":[{"id":19595,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/19029\/revisions\/19595"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=19029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=19029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=19029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}