Implicit functions

Implicit function visualizations using 2D grid systems are a powerful technique for representing and exploring complex mathematical relationships in two-dimensional space. These visualizations are particularly useful for understanding implicit functions, which are equations in which the dependent variable (usually denoted as "y") cannot be explicitly solved for as a function of the independent variable ("x"). Instead, implicit functions are defined by an equation that relates both or more variables.
In 2D grid systems, implicit function visualizations involve plotting points on a grid where each point represents a pair of x and y coordinates. The key idea is to evaluate the implicit function's equation at each point in the grid to determine whether the equation holds true (i.e., whether the function equals a constant C) at that location. The result is a visual representation of the implicit function's behavior over a specified region of the x-y plane.
In this tutorial we'll explore different implicit functions and see how we can incorporate them using our 2D grid system.



The parameters of the Algorithm

  • SCREEN: (int,int) - Dimensions of the screen given as a width and height in pixels
# MODULES
import pygame

# DD
SCREEN = (400,400)
display

The Algorithm

  • For each pixel
    • Is the result of a function that receives the pixel's x and y, close to a constant value C?
      • T: give pixel the color red

Let's implement this algorithm using Python!
# MODULES
import pygame

# DD
SCREEN = (400,400)
display = pygame.display.set_mode(SCREEN)


# CODE

# FD. remap()
# Signature: float, float, float, float, float -> float
# purp. rescale a given value
def remap(value, from1, to1, from2, to2):
    return (value - from1) / (to1 - from1) * (to2 - from2) + from2


def isHyperEllipse(x,y):
    value = x**4 + y**4
    if value < 80:
        return True
    return False

def isHeart(x,y):
    value = x**2 + (y-abs(x)*0.57)**2
    if value < 40:
        return True
    return False

def draw():
    display.fill("#1e1e1e")
    for r in range(SCREEN[1]):
        for c in range(SCREEN[0]):
            x = remap(c,0,SCREEN[0],-10,10)
            y = remap(r,0,SCREEN[0],-10,10)
            if isHeart(x,-y):
                display.set_at((c,r),(c%256,255-c%256,r%256))
    pygame.display.flip()

def update():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

while True:
    draw()
    update()