# 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.  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](x, y)
renderPos = currentBand.bandDefinition[bandKey]
bandSol = currentBand.bandDefinition[bandKey]
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?

• 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
Apr 7, 2021 at 2:43