[Python] Vertical line in my graphing rendering function

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a graphing calculator and when I graph certain functions (e.g y=abs(x)) there is a vertical line in the middle. This only happens on certain resolutions. It happens with other functions as well such as certain parabolas.

    from PIL import Image
    from numba import vectorize, float64, njit
    import numpy as np
    import numexpr as ne
    from time import time
    from timeit import timeit

    def generate_img(equation, resolution, xbound, ybound, color) -> Image.Image:
    equation_sides = equation.split("=")


    xbound_dist = abs(xbound[0]) + abs(xbound[1])
    ybound_dist = abs(ybound[0]) + abs(ybound[1])

    xstep = xbound_dist / resolution[0]
    ystep = ybound_dist / resolution[1]

    xstart = xbound[0] - xstep
    ystart = ybound[1] - ystep



    pixels = np.arange((resolution[0] + 2) * (resolution[1] + 2), dtype=np.int32)

    x = get_x(np.float64(xstart), np.float64(xstep), np.int32(resolution[0] + 2), pixels)
    y = get_y(np.float64(ystart), np.float64(ystep), np.int32(resolution[0] + 2), pixels)


    pixels_shape = (resolution[1] + 2, resolution[0] + 2)
    pixels = ne.evaluate(f"({equation_sides[0]})-({equation_sides[1]})", local_dict={"x": x, "y": y, "i": 1j}).reshape(pixels_shape)

    mask = get_imag(pixels[1:resolution[1] + 1, 1: resolution[0] + 1])
    pixels = abs(pixels)
    pixels = pixels.astype(np.float64)

    main_area = pixels[1:resolution[1]+1, 1:resolution[0]+1]
    north = pixels[0:resolution[1], 1:resolution[0]+1]
    south = pixels[2:resolution[1]+2, 1:resolution[0]+1]
    west = pixels[1:resolution[1]+1, 0:resolution[0]]
    east = pixels[1:resolution[1]+1, 2:resolution[0]+2]
    north_east = pixels[0:resolution[1], 2:resolution[0]+2]
    north_west = pixels[0:resolution[1], 0:resolution[0]]
    south_east = pixels[2:resolution[1]+2, 2:resolution[0]+2]
    south_west = pixels[2:resolution[1]+2, 0:resolution[0]]

    pixels = light_up_pixels(
    main_area,
    north,
    south,
    west,
    east,
    north_east,
    north_west,
    south_east,
    south_west
    )

    shape = Image.fromarray(pixels * mask)
    img = Image.new("RGB", resolution, color)
    img.putalpha(shape)
    return img

    @vectorize(['bool(float64)', 'bool(complex128)'])
    def get_imag(pixel):
    return abs(pixel.imag) < 0.01

    @vectorize(['float64(float64, float64, int32, int32)'])
    def get_x(xstart, xstep, xres, p):
    return xstart + (p % xres) * xstep

    @vectorize(['float64(float64, float64, int32, int32)'])
    def get_y(ystart, ystep, xres, p):
    return ystart - (p // xres) * ystep

    @vectorize(['boolean(float64, float64, float64, float64, float64, float64, float64, float64, float64)'])
    def light_up_pixels(main, north, south, west, east, north_east, north_west, south_east, south_west):
    count = 0
    count += main < north
    count += main < south
    count += main < west
    count += main < east
    count += main < north_east
    count += main < north_west
    count += main < south_east
    count += main < south_west
    return count >= 6


    equation = "y=abs(x)"
    generate_img(equation, (10, 20), (-10, 10), (-10, 10), (255, 0, 0)).show()


    the graph result using the current code I feel like I've tried everything but it's still not working

    I don't have the correct image as I my program does not currently work correctly, there should not be a vertical line under the corner of the abs function. it should look like an upside down '^' not a 'Y'

    Continue reading...

Compartilhe esta Página