<p>Compute the tangent to the line at that point, Use it to make a tangent vector, then use the point on the line, the point being tested and the new point to create two vectors. Take the dot product of the vectors. Positive values are on one side of the line, negative are on the other side. (above and below).</p>

CC BY-SA 4.0
2021-04-04T10:00:50.397Z
2021-04-04T10:00:50.397Z
pmw1234
<p>Seems like a simple problem, but I just cant wrap my head around it.</p>
<p>I have a config file in which I declare a few functions. It looks like this:</p>
<pre><code>&quot;bandDefinitions&quot; : [
    {
        &quot;0&quot;: [&quot;x^2 + 2*x + 5 - y&quot;, &quot;ABOVE&quot;]
    },
    {
        &quot;0&quot;: [&quot;sin(6*x) - y&quot;, &quot;UNDER&quot;]
    },
    {
        &quot;0&quot;: [&quot;tan(x) - y&quot;, &quot;ABOVE&quot;]
    }
]
</code></pre>
<p>These functions should generate 3 images. Every image should be filled depending on solution of equations, and provided position (Under or Above). I need to move the coordinate system to the center of the image, so I'm adding <code>-y</code> into the equation. Part of image which should be filled should be colored white, and the other part should be colored black.</p>
<p>To explain what I mean, I'm providing images for quadratic and sin functions.</p>
<p><a href="https://i.sstatic.net/17nU8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/17nU8.png" alt="enter image description here" /></a></p>
<p><a href="https://i.sstatic.net/aMeOh.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/aMeOh.png" alt="sin" /></a></p>
<p>What I'm doing is solve the equation for <code>x in [-W/2, W/2]</code> and store the solutions into the array, like this:</p>
<pre><code>#Generates X axis dots and solves an expression which defines a band
#Coordinate system is moved to the center of the image
def __solveKernelDefinition(self, f):
    xAxis = range(-kernelSize, kernelSize)
    dots = []

    for x in xAxis:
        sol = f(x, kernelSize/2)
        dots.append(sol)

    print(dots)
    return dots
</code></pre>
<p>I'm testing if some pixel should be colored white like this:</p>
<pre><code>def shouldPixelGetNoise(y, x, i, currentBand):
    shouldGetNoise = True

    for bandKey in currentBand.bandDefinition.keys():
        if shouldGetNoise:
            pixelSol = currentBand.bandDefinition[bandKey][2](x, y)
            renderPos = currentBand.bandDefinition[bandKey][1]
            bandSol = currentBand.bandDefinition[bandKey][0]
            shouldGetNoise = shouldGetNoise and pixelSol &lt;= bandSol[i] if renderPos == Position.UNDER else pixelSol &gt;= bandSol[i]
        else:
            break

    return shouldGetNoise

def kernelNoise(kernelSize, num_octaves, persistence, currentBand, dimensions=2):
    simplex = SimplexNoise(num_octaves, persistence, dimensions)
    data = []

    for i in range(kernelSize):
        data.append([])
        i1 = i - int(kernelSize / 2)

        for j in range(kernelSize):
            j1 = j - int(kernelSize / 2)
            if(shouldPixelGetNoise(i1, j1, i, currentBand)):
                noise = normalize(simplex.fractal(i, j, hgrid=kernelSize))
                data[i].append(noise * 255)
            else:
                data[i].append(0)
</code></pre>
<p>I'm only getting good output for convex quadratic functions. If I try to combine them, I get a black image. <code>Sin</code> just doesn't work at all. I see that this bruteforce approach won't lead me anywhere, so I was wondering what algorithm should I use to generate these kinds of images?</p>

CC BY-SA 4.0
2021-04-07T02:43:46.440Z
Do you know how to compute y as a function of x?  Do you know how to draw a line from the top to y?   i.e.: from (x,0) to (x,y)
Wyck
<image-processing><procedural-generation>
Filling pixels under or above some function
Riki
# Filling pixels under or above some function

(Asked by: username_0 on 2021-04-03)

Seems like a simple problem, but I just cant wrap my head around it.

I have a config file in which I declare a few functions. It looks like this:

```
"bandDefinitions" : [
    {
        "0": ["x^2 + 2*x + 5 - y", "ABOVE"]
    },
    {
        "0": ["sin(6*x) - y", "UNDER"]
    },
    {
        "0": ["tan(x) - y", "ABOVE"]
    }
]

```

These functions should generate 3 images. Every image should be filled depending on solution of equations, and provided position (Under or Above). I need to move the coordinate system to the center of the image, so I'm adding `-y` into the equation. Part of image which should be filled should be colored white, and the other part should be colored black.

To explain what I mean, I'm providing images for quadratic and sin functions.

[![enter image description here](https://i.sstatic.net/17nU8.png)](https://i.sstatic.net/17nU8.png)

[![sin](https://i.sstatic.net/aMeOh.png)](https://i.sstatic.net/aMeOh.png)

What I'm doing is solve the equation for `x in [-W/2, W/2]` and store the solutions into the array, like this:

```
#Generates X axis dots and solves an expression which defines a band
#Coordinate system is moved to the center of the image
def __solveKernelDefinition(self, f):
    xAxis = range(-kernelSize, kernelSize)
    dots = []

    for x in xAxis:
        sol = f(x, kernelSize/2)
        dots.append(sol)

    print(dots)
    return dots

```

I'm testing if some pixel should be colored white like this:

```
def shouldPixelGetNoise(y, x, i, currentBand):
    shouldGetNoise = True

    for bandKey in currentBand.bandDefinition.keys():
        if shouldGetNoise:
            pixelSol = currentBand.bandDefinition[bandKey][2](x, y)
            renderPos = currentBand.bandDefinition[bandKey][1]
            bandSol = currentBand.bandDefinition[bandKey][0]
            shouldGetNoise = shouldGetNoise and pixelSol <= bandSol[i] if renderPos == Position.UNDER else pixelSol >= bandSol[i]
        else:
            break

    return shouldGetNoise

def kernelNoise(kernelSize, num_octaves, persistence, currentBand, dimensions=2):
    simplex = SimplexNoise(num_octaves, persistence, dimensions)
    data = []

    for i in range(kernelSize):
        data.append([])
        i1 = i - int(kernelSize / 2)

        for j in range(kernelSize):
            j1 = j - int(kernelSize / 2)
            if(shouldPixelGetNoise(i1, j1, i, currentBand)):
                noise = normalize(simplex.fractal(i, j, hgrid=kernelSize))
                data[i].append(noise * 255)
            else:
                data[i].append(0)

```

I'm only getting good output for convex quadratic functions. If I try to combine them, I get a black image. `Sin` just doesn't work at all. I see that this bruteforce approach won't lead me anywhere, so I was wondering what algorithm should I use to generate these kinds of images?

**Tags:** image-processing, procedural-generation

> **Comments:**
> **username_1**: Do you know how to compute y as a function of x?  Do you know how to draw a line from the top to y?   i.e.: from (x,0) to (x,y)  

---

## 1 answers

### Answer by username_2 on 2021-04-04. Score: 1

Compute the tangent to the line at that point, Use it to make a tangent vector, then use the point on the line, the point being tested and the new point to create two vectors. Take the dot product of the vectors. Positive values are on one side of the line, negative are on the other side. (above and below).


---

