- UID
- 81263
- 帖子
- 53
- 精华
- 2
- 积分
- 167
|
因为本帖主要是展示简单例子,所以就不设置复杂的上色函数了。
- class Complex {
- // By chatGPT
- constructor(real, imaginary) {
- this.real = real;
- this.imaginary = imaginary || 0;
- }
- add(other) {
- return new Complex(
- this.real + other.real,
- this.imaginary + other.imaginary
- );
- }
- sub(other) {
- return new Complex(
- this.real - other.real,
- this.imaginary - other.imaginary
- );
- }
- mul(other) {
- const real = this.real * other.real - this.imaginary * other.imaginary;
- const imaginary = this.real * other.imaginary + this.imaginary * other.real;
- return new Complex(real, imaginary);
- }
- div(other) {
- const denominator = pow(other.real, 2) + pow(other.imaginary, 2);
- const real =
- (this.real * other.real + this.imaginary * other.imaginary) / denominator;
- const imaginary =
- (this.imaginary * other.real - this.real * other.imaginary) / denominator;
- return new Complex(real, imaginary);
- }
- eq(other) {
- return this.imaginary === other.imaginary && this.real === other.real;
- }
- norm() {
- return sqrt(pow(this.real, 2) + pow(this.imaginary, 2));
- }
- }
- function cpx(x, y) {
- return new Complex(x, y);
- }
- let img;
- const q0 = 1; // ,诱捕半径为 q0
- function setup() {
- createCanvas(windowHeight, windowHeight);
- img = createImage(windowHeight, windowHeight);
- img.loadPixels();
- noLoop();
- }
- /**
- * @param {p5.Vector} z
- * s
- * @param {Vector} first 点#为诱捕点(或点陷阱中心),
- */
- function getLastColor(c, s, first) {
- // <-- c -> z
- let z = cpx(0, 0);
- for (let index = 0; index < 30; index++) {
- const newz = c.add(z.mul(z));
- if (newz.eq(z) || newz.norm() > 2) {
- return s;
- }
- z = newz;
- s = min(s, abs(1 - z.norm()));
- }
- return s;
- }
- function plot() {
- for (let i = 0; i < img.width; i++) {
- for (let j = 0; j < img.height; j++) {
- const px = map(i, 0, img.width, -2, 2);
- const py = map(j, 0, img.height, -2, 2);
- const c = getLastColor(cpx(px, py), q0, cpx(0));
- if(c < q0){
- img.set(i, j, color(255 - map(c, 0, 1, 0, 255)));
- }
- }
- }
- }
- function draw() {
- background(0);
- plot();
- img.updatePixels();
- image(img, 0, 0);
- }
复制代码 |
|