Skip to content

Instantly share code, notes, and snippets.

@ryotako
Last active February 2, 2022 13:19
Show Gist options
  • Save ryotako/76bdb50a327c0f004dda87718d3ed45e to your computer and use it in GitHub Desktop.
Save ryotako/76bdb50a327c0f004dda87718d3ed45e to your computer and use it in GitHub Desktop.
Julia set with Processing.py
julia = (-0.754567,-0.209658)
# julia = (-0.3, -0.63)
# julia = (0.285, 0.01)
def setup():
size(400,400, P2D)
pixelDensity(displayDensity())
frameRate(50)
print julia
def draw():
clear()
movable()
sh = loadShader("shader.frag")
if __movable_matrix:
x0 = __movable_matrix.m00 * 200 + __movable_matrix.m03
y0 = __movable_matrix.m11 * 200 + __movable_matrix.m13
sh.set("julia", *julia)
sh.set("origin", x0, height-y0)
sh.set("scale", __movable_matrix.m00, __movable_matrix.m11)
filter(sh)
def keyPressed():
global julia
if key == "s":
saveFrame("images/%f_%f.png" % julia)
print frameCount, "saved"
elif key == "j":
julia = (random(-1,1), random(-1,1))
print julia
__movable_matrix = None
def movable():
global __movable_matrix
if keyPressed and key == "h":
__movable_matrix = None
return
cx, cy = width / 2, height / 2
# ドラッグによる移動
if keyPressed and key == CODED and mousePressed:
if keyCode == CONTROL:
dx = mouseX - pmouseX
dy = mouseY - pmouseY
translate(dx, dy)
elif keyCode == SHIFT:
translate(cx, cy)
scale(1.01 ** (mouseY - pmouseY))
translate(-cx, -cy)
elif keyCode == ALT:
translate(cx, cy)
q1 = atan2(pmouseY - cy, pmouseX - cx)
q2 = atan2(mouseY - cy, mouseX - cx)
rotate(q2 - q1)
translate(-cx, -cy)
# 上記移動の適用後、以前の状態を(存在するならば)復元
if __movable_matrix:
m = __movable_matrix
p = this.g.pixelDensity
if this.g.__class__.__name__ == "PGraphicsJava2D":
this.g.applyMatrix(m.m00 / p, m.m01 / p, m.m02 / p,
m.m10 / p, m.m11 / p, m.m12 / p)
elif this.g.__class__.__name__ == "PGraphics2D":
this.g.applyMatrix(m.m00, m.m01, m.m03,
m.m10, m.m11, m.m13)
elif this.g.__class__.__name__ == "PGraphics3D":
this.g.applyMatrix(m.m00, m.m01, m.m02, m.m03 + this.g.cameraX,
m.m10, m.m11, m.m12, m.m13 + this.g.cameraY,
m.m20, m.m21, m.m22, m.m23 + this.g.cameraZ,
m.m30, m.m31, m.m32, m.m33)
# 現在の状態を記録
__movable_matrix = this.g.getMatrix().get()
uniform vec2 resolution;
uniform vec2 origin;
uniform vec2 scale;
uniform vec2 julia;
int N = 10000;
vec3 bg = vec3(0);
vec3 fg = vec3(1);
float PI = acos(-1);
vec2 cpow(vec2 c, int n){
vec2 z = c;
for (int i=1; i<n; i++){
z = vec2(z.x*c.x - z.y*c.y, 2*z.x*c.y);
}
return z;
}
vec2 cmul(vec2 c1, vec2 c2){
vec2 z;
z.x = c1.x*c2.x - c1.y*c2.y;
z.y = c1.x*c2.y + c1.y*c2.x;
return z;
}
void main(){
vec2 p = (gl_FragCoord.xy/2. - origin) / scale;
gl_FragColor = vec4(fg, 1);
vec2 c = julia;
vec2 z = p/200.;
int i;
for (i=0; i<N; i++){
z = cmul(z,z) + c;
if (length(z)>=2.){
gl_FragColor = vec4(mix(bg, fg, float(i)/N*180./sqrt(sqrt(sqrt(sqrt(scale.x))))) ,1.);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment