添加随机加权点 [英] Adding random weighted point

查看:69
本文介绍了添加随机加权点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一块空白的画布,上面有两个红色的点.

Let's say I have a blank canvas with 2 red points in it.

是否有一种算法可以在画布中随机添加一个点,但是会偏向于提供半径的红色点呢?

Is there an algorithm to randomly add a point in the canvas but in a way where it's more bias to the red points with a supplied radius?

以下是一个粗略的图像作为示例:

Here's a crude image as an example:

即使这个问题是针对Python的,它实际上也适用于任何语言.

Even though this question is for Python it really applies for any language.

推荐答案

好的.随机选择第一个点或第二个点,然后用极坐标中的单个比例参数生成一些分布,然后移动 中心点位置.选择一些合理的径向分布(在下面的代码中为高斯分布,指数或柯西也可能适用)

Sure. Select first point or second point randomly, then generate some distribution with single scale parameter in polar coordinates, then shift by center point position. Select some reasonable radial distribution (gaussian in the code below, exponential or Cauchy might work as well)

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()

图片

这篇关于添加随机加权点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆