My goal is to create a circle shape from the lines in pygame using random endpoints around the edge of the circle and a constant starting point (midpoint of the circle). So I decided that I would give the function pygame.draw.line: screen, aRandomColor, startPosition and endPosition as arguments. The end position is a tuple containing a randomly generated value of x, and the helper function will calculate the value of y based on the radius of the circle. My first function calculates the y value as follows:
import math import random def findY(pos1, pos2, distance, bothValues=False): p1 =pos1 p2 = pos2 x1 = float(p1[0]) y1 = float(p1[1]) x2 = float(p2[0]) d = float(distance) y2 = y1 - math.sqrt(d**2 - (x1-x2)**2) _y2 = y1 + math.sqrt(d**2 - (x1-x2)**2) if bothValues==True: return y2, _y2 else: return y2
and line box:
width = 500 height = 500 def randLine(surface, color=rand, start=rand, end=rand,length=rand): if start==rand: start = randPos() if end==rand: end = randPos() if color==rand: color = randColor() if length != rand: end_x = float(random.randint(0,width)) end_pos = (end_x, "y") y2=findMissing(start_pos, end_pos,l,bothValues=True) a = random.randint(0,1) if a==0: y2 = float(y2[0]) else: y2 = float(y2[1]) lst = list(end_pos) lst[1] = y2 end_pos = tuple(lst) pygame.draw.line(surface, color, start_pos, end_pos)
Then:
drawRandLine(screen,start=(200,200),lenght=100)
(other functions called randPos are not a problem). For some reason, this generated an error, which I diagnosed as the value inside math.sqrt () was a negative number. But this cannot be, since every value there is raised to power 2, and this is what I got confused about. So I changed the value inside math.sqrt () to its absolute value. This caused the function to not create any errors, but the circle was like this:

I know that the coordinate of the pygame y plane is upside down, but should it matter?