Skip to content

Instantly share code, notes, and snippets.

@Oldes
Last active March 24, 2023 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oldes/ec8abdc2fcb42599e08402619123d79a to your computer and use it in GitHub Desktop.
Save Oldes/ec8abdc2fcb42599e08402619123d79a to your computer and use it in GitHub Desktop.
Graphing 2D Equations using Rebol/MathPresso extension
REBOL [
title: "Graphing 2D Equations"
file: %graph2d.r3
purpose: {
Demonstrate use of Rebol/MathPresso extension.
Using precompiled math expression.
https://github.com/Siskin-framework/Rebol-MathPresso
}
reference: https://p5js.org/examples/math-graphing-2d-equations.html
license: MIT
needs: [mathpresso]
usage: [save %result.jpg graph2d 640x480 4]
]
graph2d: function/with [
"Graphing 2D Equations"
img [image! pair!] "Output image"
num [number!] "Input number"
][
if pair? img [img: make image! img]
cols: img/size/x
rows: img/size/y
dx: space-wide / cols ;; Increment x this amount per pixel
dy: space-high / rows ;; Increment y this amount per pixel
data/1: num
data/2: space-wide / -2 ;; Start x at -1 * space-wide / 2
repeat i cols [
data/3: space-high / -2 ;; Start y at -1 * space-high / 2
repeat j rows [
math/eval :expr :data
clr/1: clr/2: clr/3: to integer! data/4
poke img as-pair i j clr
data/3: data/3 + dy
]
data/2: data/2 + dx
]
img
][
;; This code is evaluated only once when the function is initialized!
math: system/modules/mathpresso
clr: 0.0.0 ;= Color used to set the pixel
space-wide: 16.0 ;= 2D space width
space-high: 16.0 ;= 2D space height
;; buffer for its values
data: #[double! [0 0 0 0 0 0 0]]
;; create a context for the expression
cntx: math/context [n x y bw theta r val]
;; and compile the expression
expr: math/compile :cntx {
r = sqrt(x*x + y*y); // Convert cartesian to polar
theta = atan2(y, x); // Compute 2D polar coordinate function
val = sin(n * cos(r) + 5*theta); // Results in a value between -1 and 1
bw = ((val + 1) * 255) / 2; // Convert to value between 0 and 255
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment