# 4.6: Interface design

The next step is to write circle, which takes a radius, r, as a parameter. Here is a simple solution that uses polygon to draw a 50-sided polygon:

import math

def circle(t, r):
circumference = 2 * math.pi * r
n = 50
length = circumference / n
polygon(t, n, length)


The first line computes the circumference of a circle with radius r using the formula $$2 \pi r$$. Since we use math.pi, we have to import math. By convention, import statements are usually at the beginning of the script.

n is the number of line segments in our approximation of a circle, so length is the length of each segment. Thus, polygon draws a 50-sided polygon that approximates a circle with radius r.

One limitation of this solution is that n is a constant, which means that for very big circles, the line segments are too long, and for small circles, we waste time drawing very small segments. One solution would be to generalize the function by taking n as a parameter. This would give the user (whoever calls circle) more control, but the interface would be less clean.

The interface of a function is a summary of how it is used: what are the parameters? What does the function do? And what is the return value? An interface is “clean” if it allows the caller to do what they want without dealing with unnecessary details.

In this example, r belongs in the interface because it specifies the circle to be drawn. n is less appropriate because it pertains to the details of how the circle should be rendered.

Rather than clutter up the interface, it is better to choose an appropriate value of n depending on circumference:

def circle(t, r):
circumference = 2 * math.pi * r
n = int(circumference / 3) + 3
length = circumference / n
polygon(t, n, length)


Now the number of segments is an integer near circumference/3, so the length of each segment is approximately 3, which is small enough that the circles look good, but big enough to be efficient, and acceptable for any size circle.

Adding 3 to n guarantees that the polygon has at least 3 sides.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8934452,"math_prob":0.9929295,"size":2073,"snap":"2021-43-2021-49","text_gpt3_token_len":488,"char_repetition_ratio":0.1416143,"word_repetition_ratio":0.06842105,"special_character_ratio":0.23830198,"punctuation_ratio":0.12085308,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9990337,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-26T09:24:00Z\",\"WARC-Record-ID\":\"<urn:uuid:75074b76-c02e-413a-ae4e-e3a1f97736ba>\",\"Content-Length\":\"95151\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9e0d05e3-8462-458d-88ce-852e3865a2c5>\",\"WARC-Concurrent-To\":\"<urn:uuid:03d39f44-9541-4a27-929f-8991e4a35ea6>\",\"WARC-IP-Address\":\"13.249.38.111\",\"WARC-Target-URI\":\"https://eng.libretexts.org/Bookshelves/Computer_Science/Programming_Languages/Book%3A_Think_Python_2ed._(Downey)/04%3A_Case_study_-_interface_design/4.06%3A_Interface_design\",\"WARC-Payload-Digest\":\"sha1:IQPEFG5ZRZMJARCBQF2XVX5ASORJHSGZ\",\"WARC-Block-Digest\":\"sha1:FTU54QENVYCJYTTE74KEVKPSQ2EPP6KM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323587854.13_warc_CC-MAIN-20211026072759-20211026102759-00153.warc.gz\"}"}