Riki
Riki

Reputation: 341

Filling pixels under or above some function

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

sin

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?

Upvotes: 1

Views: 282

Answers (1)

Prefect
Prefect

Reputation: 1777

As far as I understood, you want to plot your functions and fill up above or under of these functions. You might easily do this by creating a grid (i.e. a 2D Cartesian coordinate system) in numpy, and define your functions on the grid.

import numpy as np
import matplotlib.pyplot as plt
max_ax = 100

resolution_x = max_ax/5
resolution_y = max_ax/20
y,x = np.ogrid[-max_ax:max_ax+1, -max_ax:max_ax+1]
y,x = y/resolution_y, x/resolution_x

func1 = x**2 + 2*x + 5  <= -y


resolution_x = max_ax
resolution_y = max_ax
y,x = np.ogrid[-max_ax:max_ax+1, -max_ax:max_ax+1]
y,x = y/resolution_y, x/resolution_x

func2 = np.sin(6*x) <= y
func3 = np.tan(x) <= -y



fig,ax = plt.subplots(1,3)
ax[0].set_title('f(x)=x**2 + 2*x + 5')
ax[0].imshow(func1,cmap='gray')
ax[1].set_title('f(x)=sin(6*x)')
ax[1].imshow(func2,cmap='gray')
ax[2].set_title('f(x)=tan(x)')
ax[2].imshow(func3,cmap='gray')
plt.show()

plots

Is this what you are looking for?

Edit: I adjusted the limits of x- and y-axes. Because, for example, sin(x) does not make much sense outside of the range [-1,1].

Upvotes: 1

Related Questions