Search code examples
pythongeometrytriangle

How can I draw a circumcircle of a triangle in Python Turtle?


Please, could you help me with this? I can draw a triangle and also calculate the center of the circle (I don't know if it is even helpful) and I have calculated the radius. But I don't know how to draw a circle itself. The way I have it doesn't connect all the vertexes. Thank you for any ideas.

import turtle
import math
from math import pi
alfa=60 # in degrees
beta=40
gama=80
c = 100
sin_alfa = math.sin(pi/180*alfa)  # in radians
sin_beta = math.sin(pi/180*beta)
sin_gama = math.sin(pi/180*gama)
a = c/sin_gama *sin_alfa # a,b calculated
b = c/sin_gama *sin_beta
board = turtle.Turtle()
board.forward(100) # base c 
board.left(180-beta)
board.forward(a)
board.left(180-gama)
board.forward(b)
board.left(180-alfa)
rad = rad = a/2*sin_alfa # radius of a circumcircle
board.circle(rad)

Solution

  • This modified code would draw a circle that touches all corners.

    import turtle
    import math
    from math import pi
    alfa=60 # in degrees
    beta=40
    gama=80
    c = 100
    sin_alfa = math.sin(pi/180*alfa)  # in radians
    sin_beta = math.sin(pi/180*beta)
    sin_gama = math.sin(pi/180*gama)
    a = c/sin_gama *sin_alfa # a,b calculated
    b = c/sin_gama *sin_beta
    board = turtle.Turtle()
    board.forward(100) # base c 
    board.left(180-beta)
    board.forward(a)
    board.left(180-gama)
    board.forward(b)
    board.left(180-alfa)
    rad = a/(2*sin_alfa)   # radius of a circumcircle
    
    
    # shift pen to bottom-most point of the circle
    board.up()
    board.forward(c/2)
    board.right(90)
    board.forward(rad*(1-(1-sin_gama**2)**0.5))
    board.down()
    board.left(90)
    
    # the circle
    board.circle(rad)
    
    turtle.done()
    

    Here is a graphical explanation of how the cursor was shifted: