-1

我正在尝试编写一个喷漆类型的程序,但我忘记了我所有的数学。

我需要某种概率方程来选择用户点击附近的像素。因此,中心附近的概率应该很高,然后随着与中心的距离变大而降低,一旦达到一定距离,概率应该为零。中心的概率也应该是一条平滑曲线(如果你知道我的意思)

4

1 回答 1

2

我不确定您使用哪种语言进行编码,所以这里有一些伪代码。我将假设您知道您正在编码的语言中的相应语法。

// Parameters:
// Radius is the radius of the brush in pixels
// Strength is a double ranging 0.0 to 1.0 and multiplies the probability of a pixel being painted
function spraypaint(int radius, double strength) {
    strength = (strength * 2) - 1; //Change strength from 0.0->1.0 to -1.0->1.0 for logical application

    // For each pixel within the square...
    for(int x = -radius; x < radius; x++) {
        for(int y = -radius; y < radius; y++) {
            double distance = sqrt(x*x + y*y); // Get distance from center pixel
            double angle = 90*(distance/radius); // Get angle of "bell curve"
            double probability = sine(angle); // Determine probability of filling in this pixel

            // Apply additional probability based on strength parameter
            if(strength >= 0.0)
                probability += ((1-probability) * strength);
            else
                probability += probability * strength;

            if(distance > radius) {
                continue;   // Skip this pixel if it's out of the circle's radius
            }

            if(random(0.0 to 1.0) < probability) { // If we random a decimal lower than our probability
                setPixel(mouse.x + x, mouse.y + y, "Black"); // Draw this pixel
            }
        }
    }
}

基本思想是:

Iterate through each pixel and...

1. Find its distance from the center pixel (The clicked pixel).
2. Get distance/radius (0.0 to 1.0) and find the corresponding sine, creating a
   smooth probability curve.
3. Apply the supplied strength to the probability.
4. Pull a random double 0.0 to 1.0 and draw the pixel if it's within our probability.
于 2012-10-02T05:49:02.563 回答