Python coordinate plane error

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:

wacky inverted circloid

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

+5
source share
2 answers

One way to get a uniform distribution of angles would be to create a random angle theta between 0 and 2 * math.pi and use trigonometry to find the coordinates of the end point of the line:

 def drawRandLineTrig(start_pos, length): theta = random.rand() * 2 * math.pi end_pos = (start_pos[0] + length*math.cos(theta), start_pos[1] + length*math.sin(theta)) # ... 
+3
source

I could do:

 def randPos(origin=(0,0), xdis=width, ydis=height): randx = random.randint(float(origin[0])-xdis,float(origin[0])+xdis) randy = random.randint(float(origin[1])-ydis,float(origin[1])+ydis) return (randx, randy) def drawRandLine(surface, color=random, start_pos=random, end_pos=random,l=random): if start_pos==random: start_pos = randPos() if end_pos==random: end_pos = randPos() if color==random: color = randColor() if l != random: b = random.randint(0,1) l=float(l) origin = start_pos dis = l if b==0: end_x = float(random.randint((origin[0]-dis),(origin[0]+dis))) 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) else: end_y = float(random.randint((origin[1]-dis),(origin[1]+dis))) end_pos = ("x", end_y) x2=findMissing(start_pos, end_pos,l,bothValues=True) a = random.randint(0,1) if a==0: x2 = float(x2[0]) else: x2 = float(x2[1]) lst = list(end_pos) lst[0] = x2 end_pos = tuple(lst) pygame.draw.line(surface, color, start_pos, end_pos) 

Which fixes the sqrt of the negative problem and puts the even distribution of the rows by calculating x OR y and setting their limits to the specified length to avoid the weird length of some rows.

0
source

Source: https://habr.com/ru/post/1237069/


All Articles