0% found this document useful (0 votes)
16 views4 pages

Computer Science Assgnment

The document provides a Python code that uses the turtle module to draw a circle based on specified parameters (X=25, Y=75, Radius=100). It defines a function 'drawCircle' to calculate the circumference and draw the circle by moving and turning the turtle object. The 'main' function sets up the drawing environment and calls 'drawCircle' to execute the drawing.

Uploaded by

peopleslively
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Computer Science Assgnment

The document provides a Python code that uses the turtle module to draw a circle based on specified parameters (X=25, Y=75, Radius=100). It defines a function 'drawCircle' to calculate the circumference and draw the circle by moving and turning the turtle object. The 'main' function sets up the drawing environment and calls 'drawCircle' to execute the drawing.

Uploaded by

peopleslively
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Q) Define a function drawCircle.

This function should expect a Turtle object, the


coordinates of the circle’s center point, and the circle’s radius as arguments. The function
should draw the specified circle. The algorithm should draw the circle’s circumference by
turning 3 degrees and moving a given distance 120 times. Calculate the distance moved
with the formula 2.0 × π × radius ÷ 120.0.
Define a function main that will draw a circle with the following parameters when the
program is run:

 X = 25
 Y = 75
 Radius = 100

Solution :

Subject: Computer Science

Sub Subject: Python Programming

Step 1 of 2:

Given:

X= 25

Y = 75

Radius = 100

Step 2 of 2:

The code for the above problem in Python 3, is:

import turtle

import math

# Define the function to draw a circle

def drawCircle(t, x, y, radius):

circumference_distance = 2.0 * math.pi * radius / 120.0

t.penup()

t.goto(x, y - radius)
t.pendown()

for _ in range(120):

t.forward(circumference_distance)

t.left(3)

# Define the main function

def main():

screen = turtle.Screen()

screen.title("Circle Drawing")

t = turtle.Turtle()

t.speed(0) # Set the drawing speed

# Circle parameters

X = 25

Y = 75

Radius = 100

# Draw the circle

drawCircle(t, X, Y, Radius)

turtle.done()

if __name__ == "__main__":

main()

Explanation:

The provided Python code uses the turtle module to draw a circle with specified parameters.

1: Importing Libraries

The code starts by importing two essential modules: turtle for graphics and math for mathematical
operations.

2: Drawing the Circle

Function: drawCircle
The drawCircle function takes four parameters: a Turtle object (turtle), x and y coordinates representing
the center of the circle, and the radius of the circle.

It calculates the distance to move in each step (circumference_distance) by dividing the circumference
of the circle into 120 segments.

It positions the turtle at the starting point on the circumference, moves forward a calculated distance,
and then turns slightly for 120 times to draw the circle by approximating its circumference.

Function: main

The main function sets up the drawing environment using the turtle module.

It sets the title of the drawing window to "Circle Drawing" and initializes a Turtle object t.

Defines the circle parameters: X, Y, and Radius.

Calls the drawCircle function with these parameters to draw the circle.

3. Overall Execution Flow

It initializes the drawing window and the turtle object.

Calculates the distance to move in each step around the circumference based on the number of
segments (120) and the circle's radius.

Moves the turtle to the starting point on the circumference and starts drawing by moving forward and
turning gradually for 120 steps to approximate the circle's shape.

The program ends after drawing the circle.

4. How to Execute:

You can run this code in a Python environment.

After execution, a window will appear displaying the drawn circle at the specified coordinates with the
given radius.

The drawing will consist of 120 line segments, approximating a circle based on the number of steps and
the calculated distance to move in each step.

Final Answer:

The above turtle code gives the following output:


Conclusion: This Python code utilizes the turtle module to draw a circle. The drawCircle function takes a
Turtle object, the coordinates of the circle's center, and its radius to draw the circle's circumference by
moving and turning. The main function sets up the drawing environment and executes the drawing
function with the provided parameters. Run this code in a Python environment supporting the turtle
module to draw the specified circle with the given parameters. After execution, take a screenshot of the
drawn circle for submission.

You might also like