Skip to content

Instantly share code, notes, and snippets.

@treytomes
Created February 7, 2024 14:53
Show Gist options
  • Save treytomes/548ee9e1c67121ca20fa58f72f6d389c to your computer and use it in GitHub Desktop.
Save treytomes/548ee9e1c67121ca20fa58f72f6d389c to your computer and use it in GitHub Desktop.
Several ways of graphing an equation.
MIN_X = -10
MAX_X = 10
MIN_Y = -10
MAX_Y = 10
RANGE_X = MAX_X - MIN_X + 1
RANGE_Y = MAX_Y - MIN_Y + 1
// Transform grid coordinates into screen coordinates.
transform = function(x, y)
y = (y + RANGE_Y / 2) * gfx.height / RANGE_Y
x = (x + RANGE_Y / 2) * gfx.width / RANGE_X
return {
"x": x,
"y": y,
}
end function
setPixel = function(x, y, c, size = 1)
pnt = transform(x, y)
if size == 1 then
gfx.setPixel pnt.x, pnt.y, c
else
gfx.fillEllipse pnt.x - size / 2, pnt.y - size / 2, size, size, c
end if
end function
line = function(x1, y1, x2, y2, lineColor, penSize)
start = transform(x1, y1)
finish = transform(x2, y2)
gfx.line(start.x, start.y, finish.x, finish.y, lineColor, penSize)
end function
lineColor = color.green
penSize = 2
text.clear
gfx.clear
// Draw the grid.
line MIN_X, 0, MAX_X, 0, color.gray, 1
line 0, MIN_Y, 0, MAX_Y, color.gray, 1
for x in range(MIN_X, MAX_X)
line x, -0.1, x, 0.1, color.gray, 1
end for
for y in range(MIN_Y, MAX_Y)
line -0.1, y, 0.1, y, color.gray, 1
end for
for y in range(MIN_Y, MAX_Y)
for x in range(MIN_X, MAX_X)
setPixel x, y, color.gray
end for
end for
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Graph a line with dots.
// m = 0.5
// b = 2
// for x in range(MIN_X, MAX_X)
// y = m * x + b
// setPixel x, y, lineColor, 3
// end for
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Graph a line with lines.
// for x0 in range(MIN_X, MAX_X)
// y0 = x0 ^ 2 - 2
// x1 = x0 - 1
// y1 = x1 ^ 2 - 2
// line x0, y0, x1, y1, lineColor, 3
// end for
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Graph a circle with dots.
// x^2+y^2=r^2
// --> if r=3, then y=+-sqrt(3^2-x^2)
// for x in range(MIN_X, MAX_X, 1)
// y = sqrt(3^2 - x^2)
// setPixel x, y, lineColor, 3
// y = -sqrt(3^2 - x^2)
// setPixel x, y, lineColor, 3
// end for
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Graph a circle with lots of dots.
// x^2+y^2=r^2
// --> if r=3, then y=+-sqrt(3^2-x^2)
// for x in range(MIN_X, MAX_X, 0.1)
// y = sqrt(3^2 - x^2)
// setPixel x, y, lineColor, 3
// y = -sqrt(3^2 - x^2)
// setPixel x, y, lineColor, 3
// end for
// You can never quite get enough dots to make the ends look complete.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Graph a circle with lines.
// Looks a little better, but goes nuts at the endpoints.
// Even so, how to programmatically solve x^2+y^2=r^2 for y?
// for x0 in range(MIN_X, MAX_X, 0.1)
// x1 = x0 - 0.1
// y0 = sqrt(3^2 - x0^2)
// y1 = sqrt(3^2 - x1^2)
// line x0, y0, x1, y1, lineColor, 3
// y0 = -sqrt(3^2 - x0^2)
// y1 = -sqrt(3^2 - x1^2)
// line x0, y0, x1, y1, lineColor, 3
// end for
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// And now, let's try marching squares on the little bugger.
GRID_RESOLUTION = 0.5
sample = function(x, y)
r = 5
dx = -3
dy = 2
return r^2 / ((x - dx)^2 + (y - dy)^2)
// return r^2 / (x^2 + cos(y))
end function
lerp = function(x, x0, x1, y0 = 0, y1 = 1)
return y0 + ((y1 - y0) * (x - x0)) / (x1 - x0)
end function
c = color.green
for y in range(MIN_Y, MAX_Y, GRID_RESOLUTION)
for x in range(MIN_Y, MAX_Y, GRID_RESOLUTION)
bl = sample(x, y)
br = sample(x + GRID_RESOLUTION, y)
tl = sample(x, y + GRID_RESOLUTION)
tr = sample(x + GRID_RESOLUTION, y + GRID_RESOLUTION)
a = [
x * GRID_RESOLUTION + GRID_RESOLUTION * lerp(1, tl, tr),
y * GRID_RESOLUTION + GRID_RESOLUTION,
]
b = [
x * GRID_RESOLUTION + GRID_RESOLUTION,
y * GRID_RESOLUTION + GRID_RESOLUTION * lerp(1, br, tr),
]
c = [
x * GRID_RESOLUTION + GRID_RESOLUTION * lerp(1, bl, br),
y * GRID_RESOLUTION,
]
d = [
x * GRID_RESOLUTION,
y * GRID_RESOLUTION + GRID_RESOLUTION * lerp(1, bl, tl),
]
bl = bl >= 1
br = br >= 1
tl = tl >= 1
tr = tr >= 1
case = bl + br * 2 + tr * 4 + tl * 8
if case == 0 or case == 15 then
continue
else if case == 1 or case == 14 then
line d[0], d[1], c[0], c[1], lineColor, penSize
else if case == 2 or case == 13 then
line b[0], b[1], c[0], c[1], lineColor, penSize
else if case == 3 or case == 12 then
line d[0], d[1], b[0], b[1], lineColor, penSize
else if case == 4 or case == 11 then
line a[0], a[1], b[0], b[1], lineColor, penSize
else if case == 5 then
line d[0], d[1], a[0], a[1], lineColor, penSize
line c[0], c[1], b[0], b[1], lineColor, penSize
else if case == 6 or case == 9 then
line c[0], c[1], a[0], a[1], lineColor, penSize
else if case == 7 or case == 8 then
line d[0], d[1], a[0], a[1], lineColor, penSize
else if case == 10 then
line a[0], a[1], b[0], b[1], lineColor, penSize
line c[0], c[1], d[0], d[1], lineColor, penSize
end if
end for
end for
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment