2015-11-29 17 views
5

私の目標は、円の端を囲むランダムな端点と一定の開始点(円の中央)を使用して、パイゲームの線から円の形を作ることです。ですから、pygame.draw.line関数screen、aRandomColor、startingPosition、およびendingPositionを引数として渡すことにしました。終了位置はランダムに生成されたx値を含むタプルであり、ヘルパー関数は円の半径に基づいてy値を計算します。python距離式座標平面誤差

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 

と線引出し:

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) 

そして:

drawRandLine(screen,start=(200,200),lenght=100) 

(randPosように呼ばれるそれらのものが上がらない他の機能」私の第一の機能は、このようなYの値を算出します問題はありません)。これは何らかの理由でmath.sqrt()内の値が負の数であると診断されたエラーを生成しました。しかし、そこにあるすべての価値が2の累乗に引き上げられ、私が混乱していることが起こるので、それは起こり得ません。そこで、math.sqrt()内の値を絶対値に変更しました。これは、関数がエラーを上げていないが、描かれた円は、このように見えた製:

wacky inverted circloid

私はpygameの者が逆さまに飛行機のyの値を調整することを知っているが、それは違いを作る必要がありますか?角度のAA均一な分布が02 * math.pi間のランダムな角度thetaを生成し、ラインの終点の座標を見つけるために三角法を用いることであろう取得の

+1

これらの見た目は[hyperbolas](https://en.wikipedia.org/wiki/Hyperbola)のように見えます... –

+0

これはx軸を横切ってお互いの逆であり、そのため彼らは湾曲して見えます。 – Vityou

+3

'abs(d) Galax

答えて

3

一つの方法:

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)) 
    # ... 
0

私は何ができる:負の問題の平方根を修正、およびSを避ける​​ために、XまたはYを計算し、指定された長さに自分の限界を設定することにより、いずれかのラインの均一な分布を置き

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) 

いくつかの行の長さの長さ。

+0

私の提案は実際には1ライナーです。実装が簡単で、均一に分布したアングルを提供し、さらに優れたパフォーマンスを備えています。これらの乱数はそれぞれ、追加した各トリグ関数の約6倍の時間がかかります。 – Galax

+0

pythonのtrig関数はテイラー級数を使用していませんか? – Vityou

+0

私は確信していませんが、私はPythonのtrig関数が非常に高速であることを疑います。私はすぐに 'timeit'を使用して2つのバージョンのプロファイルを作成しました。私の2行関数は、あなたの時間の約1/10で均一な角度で' end_pos'を作成します。 – Galax

関連する問題