0
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ 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) $\endgroup$
    – Wyck
    Apr 7 at 2:43
1
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.