2017-11-13 18 views
1

두 개의 빨간 점이있는 빈 캔버스가 있다고 가정 해 봅니다.임의 가중치 포인트 추가

랜덤으로 주어진 지점을 캔버스에 추가하는 알고리즘이 있지만 반경이 지정된 빨간색 점에 편향되는 방식이 있습니까?

여기에 예로서 원유 이미지입니다 :이 질문은 정말 모든 언어에 적용 파이썬입니다 enter image description here

에도 불구하고.

답변

2

확실히. 첫 번째 점 또는 두 번째 점을 무작위로 선택한 다음 극좌표에서 단일 축척 매개 변수로 일부 분포를 생성 한 다음 중심점 위치만큼 이동합니다. 일부 합리적인 반경 방향 분포를 선택

enter image description here

import math 
import random 

import matplotlib.pyplot as plt 

def select_point(): 
    p = random.random() 
    if p < 0.5: 
     return 0 
    return 1 

def sample_point(R): 
    """ 
    Sample point inpolar coordinates 
    """ 
    phi = 2.0 * math.pi * random.random() # angle 
    r = R * random.gauss(0.0, 1.0)  # might try different radial distribution, R*random.expovariate(1.0) 

    return (r * math.cos(phi), r * math.sin(phi)) 

def sample(R, points): 
    idx = select_point() 

    x, y = sample_point(R) 

    return (x + points[idx][0], y + points[idx][1]) 

R = 1.0 
points = [(7.1, 3.3), (4.8, -1.4)] 

random.seed(12345) 

xx = [] 
yy = [] 
cc = [] 

xx.append(points[0][0]) 
xx.append(points[1][0]) 

yy.append(points[0][1]) 
yy.append(points[1][1]) 

cc.append(0.8) 
cc.append(0.8) 

for k in range(0, 50): 
    x, y = sample(R, points) 
    xx.append(x) 
    yy.append(y) 
    cc.append(0.3) 

plt.scatter(xx, yy, c=cc) 
plt.show() 

사진 (아래 코드에서 가우스를, 지수 또는 코시도 작동 할 수 있습니다)