{"id":188753,"date":"2025-02-07T09:10:29","date_gmt":"2025-02-07T09:10:29","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=188753"},"modified":"2025-02-07T09:10:31","modified_gmt":"2025-02-07T09:10:31","slug":"python-turtle-code-triangle-with-color","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/02\/07\/python-turtle-code-triangle-with-color\/","title":{"rendered":"Python Turtle Code:: Triangle with color"},"content":{"rendered":"\n<p>Python Turtle Code:: Triangle with color<\/p>\n\n\n\n<p>Drawing a Sierpinski triangle using GUI\/Turtle.<\/p>\n\n\n\n<p>Write a program that draws Sierpinski triangle using recursive methods as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/02\/image-141.png\" alt=\"\" class=\"wp-image-188754\"\/><\/figure>\n\n\n\n<p>The three points of the triangle are passed to invoke displayTriangles. If order == 0, the displayTriangles (order, p1, p2, p3) function displays a triangle that connects three points p1, p2, and p3, as shown in Figure a. Otherwise, it performs the following tasks:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Obtains a midpoint between p1 and p2), a midpoint between p2 and p3, and a midpoint between p3 and p1, as shown in Figure b.<\/li>\n\n\n\n<li>Recursively invokes displayTriangles with a reduced order to display three smaller Sierpinski triangles. Note that each small Sierpinski triangle is structurally identical to the original big Sierpinski triangle except that the order of a small<\/li>\n<\/ol>\n\n\n\n<p>triangle is one less, as shown in Figure b.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/02\/image-142.png\" alt=\"\" class=\"wp-image-188755\"\/><\/figure>\n\n\n\n<p>Drawing a Sierpinski triangle spawns calls to draw three small Sierpinski triangles recursively.<\/p>\n\n\n\n<p>Once done, rewrite the programs for filled Sierpinski triangles.<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\"><strong>The Correct Answer and Explanation is :<\/strong><\/mark><\/p>\n\n\n\n<p>he Sierpinski Triangle is a fractal pattern formed by recursively subdividing an equilateral triangle into smaller equilateral triangles.his process results in a self-similar structure, where each smaller triangle mirrors the overall shape.mplementing this pattern in Python using the Turtle graphics library allows for a visual demonstration of recursion and fractal geometry.<br><strong>Understanding the Recursive Process:<\/strong><\/p>\n\n\n\n<p>. <strong>Base Case (Order 0):<\/strong> Draw a single equilateral triangle.. <strong>Recursive Case (Order &gt; 0):<\/strong> Divide the current triangle into three smaller triangles by connecting the midpoints of its sides. Recursively apply the same process to each of the three smaller triangles.<br><strong>Python Turtle Implementation:<\/strong><\/p>\n\n\n\n<p>&#8220;python<br>import turtle<\/p>\n\n\n\n<p>def draw_triangle(points, color, my_turtle):<br>&#8220;&#8221;&#8221;Draws a filled triangle with the given points and color.&#8221;&#8221;&#8221;<br>my_turtle.fillcolor(color)<br>my_turtle.up()<br>my_turtle.goto(points[0][0], points[0][1])<br>my_turtle.down()<br>my_turtle.begin_fill()<br>my_turtle.goto(points[1][0], points[1][1])<br>my_turtle.goto(points[2][0], points[2][1])<br>my_turtle.goto(points[0][0], points[0][1])<br>my_turtle.end_fill()<\/p>\n\n\n\n<p>def get_mid(p1, p2):<br>&#8220;&#8221;&#8221;Calculates the midpoint between two points.&#8221;&#8221;&#8221;<br>return ((p1[0] + p2[0]) \/ 2, (p1[1] + p2[1]) \/ 2)<\/p>\n\n\n\n<p>def sierpinski(points, degree, my_turtle):<br>&#8220;&#8221;&#8221;Recursively draws the Sierpinski triangle.&#8221;&#8221;&#8221;<br>colormap = [&#8216;blue&#8217;, &#8216;red&#8217;, &#8216;green&#8217;, &#8216;white&#8217;, &#8216;yellow&#8217;, &#8216;violet&#8217;, &#8216;orange&#8217;]<br>draw_triangle(points, colormap[degree], my_turtle)<br>if degree &gt; 0:<br>sierpinski([points[0],<br>get_mid(points[0], points[1]),<br>get_mid(points[0], points[2])],<br>degree-1, my_turtle)<br>sierpinski([points[1],<br>get_mid(points[0], points[1]),<br>get_mid(points[1], points[2])],<br>degree-1, my_turtle)<br>sierpinski([points[2],<br>get_mid(points[2], points[1]),<br>get_mid(points[0], points[2])],<br>degree-1, my_turtle)<\/p>\n\n\n\n<p>def main():<br>&#8220;&#8221;&#8221;Sets up the Turtle environment and starts the drawing.&#8221;&#8221;&#8221;<br>my_turtle = turtle.Turtle()<br>my_win = turtle.Screen()<br>my_points = [[-100, -50], [0, 100], [100, -50]]<br>sierpinski(my_points, 3, my_turtle)<br>my_win.exitonclick()<\/p>\n\n\n\n<p>if <strong>name<\/strong> == &#8216;<strong>main<\/strong>&#8216;:<br>main()<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>**Explanation of the Code:**\n\n. **`draw_triangle` Function:** This function takes three points and a color as input, moves the turtle to the first point, and draws a filled triangle connecting all three points.. **`get_mid` Function:** Given two points, this function calculates and returns their midpoint, which is essential for subdividing the triangle.. **`sierpinski` Function:** This is the core recursive function. It draws a triangle and, if the recursion depth (`degree`) is greater than zero, it calculates the midpoints of the triangle's sides and recursively draws three smaller triangles at each corner. The `colormap` list assigns different colors to triangles at different recursion levels, enhancing the visual appeal.. **`main` Function:** Sets up the Turtle graphics environment, defines the initial points of the large equilateral triangle, and starts the recursive drawing process.\n**Running the Program:**\n\no execute this program, ensure you have Python installed on your system. Save the code in a file named `sierpinski.py` and run it using the command:\n``bash\npython sierpinski.py<\/code><\/pre>\n\n\n\n<p>his will open a window displaying the Sierpinski Triangle with a depth of 3. You can adjust the depth by changing the second argument in the <code>sierpinski<\/code> function call within the <code>main<\/code> function.<br><strong>Visual Demonstration:<\/strong><\/p>\n\n\n\n<p>or a visual demonstration of drawing a Sierpinski Triangle using Python Turtle, you can refer to the following video:<br>\ue200video\ue202Drawing a Sierpinski Triangle with Python Turtle\ue202turn0search4\ue201<\/p>\n\n\n\n<p>his video provides a step-by-step guide to creating the Sierpinski Triangle, which can be helpful for understanding the process and visualizing the outcome.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Turtle Code:: Triangle with color Drawing a Sierpinski triangle using GUI\/Turtle. Write a program that draws Sierpinski triangle using recursive methods as shown below: The three points of the triangle are passed to invoke displayTriangles. If order == 0, the displayTriangles (order, p1, p2, p3) function displays a triangle that connects three points p1, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-188753","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/188753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=188753"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/188753\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=188753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=188753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=188753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}