0

Please Please Please help :) The objective of this program is to randomly generate three side lengths of a triangle, check if those side lengths can make a triangle, calculate the angles of the triangle, and then draw the triangle with turtle.

My code runs! But I am having an issue with my logic haha. Because when I draw my triangle, the sides do not connect. I am having trouble pin pointing my problem though. Any help would be really appreciated!!!

Also- Because im randomly generating from a wide range of variables and using the law of cosines to find the angles...I am often getting a math domain error. Would absolute value solve this problem for me? I am a little fuzzy on the math on why im getting this error (a negative value I presume?). I would also like guidance on how and where in my code to fix this. (Im just learning and I dont have a teacher.)

This is my code:

# import statements
import random
import math
import turtle

# set turtle
window = turtle.Screen()
writer = turtle.Turtle()
writer.pensize(3)
writer.pencolor("pink")
writer.pendown

def checkTriangle():
  side1 = random.randint(5,100)
  side2 = random.randint(5,100)
  side3 = random.randint(5,100)

  sum1and2 = side1 + side2
  sum1and3 = side1 + side3
  sum2and3 = side2 + side3

  angle1 = angle(side1, side2, side3)
  angle2 = angle(side2, side3, side1)
  angle3 = angle(side3, side1, side2)

  if side3 <= sum1and2 and side2 <= sum1and3 and side1 <= sum2and3:
    print ("true")
    print(side1, side2, side3)
    print(angle1, angle2, angle3)
    drawTriangle(side1, side2, side3, angle1, angle2, angle3)
    
  else:
    print ("false")

def angle(side1, side2, side3):
  return math.degrees(math.acos((side3**2 - side2**2 - side1**2) /(-2.0 * side1 * side2)))

def drawTriangle(side1, side2, side3, angle1, angle2, angle3):
  writer.forward(side1)
  writer.left(angle3)
  writer.forward(side2)
  writer.left(angle1)
  writer.forward(side3)
  writer.left(angle2)
  
checkTriangle()

Thanks again.

  • In ```acos(x)```, it should be that -1 <= x <= 1. That means there are cases where you can't define a triangle with some values of lengths of sides - (5, 5, 100) for example. And you should do like ```writer.left(180 - angle3)``` in the ```drawTriangle()```. – relent95 Jan 11 '23 at 01:22
  • Hello, thank you so much for taking the time to respond. The (180 - angle) suggestion was definitely helpful I totally understand that. Could you please explain the acos(x) situation? I dont really follow. Do I need an if statement? :) – PersonAnon Jan 11 '23 at 02:14
  • Think that one vertex A is fixed and two sides(edges) of length 5 starts from that vertex. The other vertices B and C are two points in a circle of radius 5. You can't make their distance to be 100. So lengths of sides - (5, 5, 100) can't define a triangle. Just handle these cases, by either checking the input of the ```acos()``` or catching a ```ValueError``` exception. – relent95 Jan 11 '23 at 02:28
  • Okay I fixed it!!! I ended up moving my variables of angle into the if loop. I hadn't realized that it mattered that my angles were calculated outside of my if statement. Thanks again. – PersonAnon Jan 11 '23 at 03:21

0 Answers0