Python What is the Purpose of the Arc Function in Python's Turtle Graphics?

Click For Summary
The discussion revolves around understanding the arc function in a Python exercise related to drawing with a turtle graphics library. The arc function calculates the length of the arc based on the radius and angle, dividing the arc into smaller segments for drawing. The variable 'n' is determined by dividing the arc length by 4, which sets the number of segments to be drawn. This division aims to create manageable straight line segments that approximate the curve of the arc. If the arc length is less than 4, the integer division results in zero, necessitating the addition of one segment to ensure at least one line is drawn. The step angle is calculated by dividing the total angle by 'n', allowing for a gradual carving of the arc. The units of 'r' (radius) are consistent with the units used in the division, which is set to create segments of approximately 4 units in length. The discussion concludes with a confirmation that this method effectively approximates the arc using small straight lines.
mrcleanhands
Hi,
I'm working through thinkpython and there is an exercise which requires drawing flowers and arcs. I'm having some trouble understanding the arc function.

Code:
def arc(t, r, angle):
    """Draws an arc with the given radius and angle.

    t: Turtle
    r: radius
    angle: angle subtended by the arc, in degrees
    """
    arc_length = 2 * math.pi * r * abs(angle) / 360
    n = int(arc_length / 4) + 1 
    step_length = arc_length / n
    step_angle = float(angle) / n

    # making a slight left turn before starting reduces
    # the error caused by the linear approximation of the arc
    lt(t, step_angle/2)
    polyline(t, n, step_length, step_angle)
    rt(t, step_angle/2)

Here's the polyline code called from the above function:
Code:
def polyline(t, n, length, angle):
    """Draws n line segments.

    t: Turtle object
    n: number of line segments
    length: length of each segment
    angle: degrees between segments
    """
    for i in range(n):
        fd(t, length)
        lt(t, angle)
I'm unsure about this part:
Code:
 n = int(arc_length / 4) + 1
I'm guessing the purpose of n is to divide up the movements into separate iterations. How come it's set to arc_length / 4 + 1
Code:
step_angle = float(angle) / n
I'm guessing this is dividing up the total angle subtended by the arc into little bits which it gradually carves out...
 
Technology news on Phys.org
It looks like the value for n (number of arc segments) is fairly arbitrary, and you're right, the step angle is what an arc segment subtends for a given segment of the total arc.
 
What are the units of 'r' ?

I think those are the same units of the dividing 4

So, it seems to me that they are choosing to draw an arc as small straight lines, except when the arc is smaller than 4, in which the division yields a number smaller than 1 and the int() operation yields zero, which leaves you back with the + 1

In other words, they are choosing the "small segments" to be approx 4 units long, or so, every time.

make sense? or am I missing something?
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
17K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 41 ·
2
Replies
41
Views
6K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K