Coder Social home page Coder Social logo

w8r / bresenham-zingl Goto Github PK

View Code? Open in Web Editor NEW
27.0 2.0 6.0 121 KB

Set of efficient Bresenham rasterisers ported from Alois Zingl' code

Home Page: https://w8r.github.io/bresenham-zingl/demo/

JavaScript 100.00%
bresenham bezier curve anti-aliasing rasterizer

bresenham-zingl's Issues

Cubic bezier infinite loop.

I encountered a bug when placing the handles of a cubic bezier very close together.

bresenham.cubicBezier(0,0, 10, 0, 0, 5, 10, 5, (x, y) => console.log(x, y));

This will result in an infitie loop with the output:

0 0
1 0
2 0
3 0
4 1
5 2
5 3
5 4
6 4
6 4
6 4
6 4
6 4
6 4
…

When moving the handles apart as in

bresenham.cubicBezier(0,0, 10, 0, 0, 6, 10, 6, (x, y) => console.log(x, y));

the algorithm succeeds.

Freature Request: Filled Circle

I went ahead and did a very raw version here (using jimp). Thanks for the port, it was a good starting point!

/* Extended from https://github.com/w8r/bresenham-zingl/blob/master/src/line.js */

module.exports.circle = (image, xm, ym, radius, outerColor, innerColor) => {
  const blendColor = (baseColor, addColor, x, y, alpha) => {
    /* eslint-disable no-bitwise */
    const baseB = (baseColor >> 24) & 0xff;
    const baseG = (baseColor >> 16) & 0xff;
    const baseR = (baseColor >> 8) & 0xff;
    const baseA = (baseColor & 0xff) / 255.0;

    const addB = (addColor >> 24) & 0xff;
    const addG = (addColor >> 16) & 0xff;
    const addR = (addColor >> 8) & 0xff;
    const addA = ((addColor & 0xff) / 255.0) * (1 - (alpha / 255.0));

    const a = 1 - ((1 - baseA) * (1 - addA));
    const r = Math.round(((addR * addA) / a) + ((baseR * baseA * (1 - addA)) / a));
    const g = Math.round(((addG * addA) / a) + ((baseG * baseA * (1 - addA)) / a));
    const b = Math.round(((addB * addA) / a) + ((baseB * baseA * (1 - addA)) / a));

    const color = Math.round(a * 255.0) | (r << 8) | (g << 16) | (b << 24);
    /* eslint-enable no-bitwise */
    image.setPixelColor(color, x, y);
  };
  let x = -radius;
  let y = 0;
  let i;
  let x2;
  let e2;
  let err = 2 - (2 * radius);
  const r = 1 - err;

  do {
    for (let j = xm + x; j <= xm - x; j += 1) {
      if (image.getPixelColor(j, ym - y) === 0) {
        image.setPixelColor(innerColor, j, ym - y);
      }
    }
    for (let j = xm - y; j <= xm + y; j += 1) {
      if (image.getPixelColor(j, ym - x) === 0) {
        image.setPixelColor(innerColor, j, ym - x);
      }
    }

    i = (255 * (err - (2 * (x + y)) - 2)) / r;
    blendColor(i > 0 ? 0x0 : innerColor, outerColor, xm - x, ym + y, Math.abs(i));
    blendColor(i > 0 ? 0x0 : innerColor, outerColor, xm - y, ym - x, Math.abs(i));
    blendColor(i > 0 ? 0x0 : innerColor, outerColor, xm + x, ym - y, Math.abs(i));
    blendColor(i > 0 ? 0x0 : innerColor, outerColor, xm + y, ym + x, Math.abs(i));

    e2 = err;
    x2 = x;
    if (err + y > 0) {
      i = (255 * (err - (2 * x) - 1)) / r;
      if (i < 256) {
        blendColor(0x0, outerColor, xm - x, ym + y + 1, i);
        blendColor(0x0, outerColor, xm - y - 1, ym - x, i);
        blendColor(0x0, outerColor, xm + x, ym - y - 1, i);
        blendColor(0x0, outerColor, xm + y + 1, ym + x, i);
      }
      x += 1;
      err += (x * 2) + 1;
    }
    if (e2 + x2 <= 0) {
      i = (255 * (((2 * y) + 3) - e2)) / r;
      if (i < 256) {
        blendColor(innerColor, outerColor, xm - x2 - 1, ym + y, i);
        blendColor(innerColor, outerColor, xm - y, ym - x2 - 1, i);
        blendColor(innerColor, outerColor, xm + x2 + 1, ym - y, i);
        blendColor(innerColor, outerColor, xm + y, ym + x2 + 1, i);
      }
      y += 1;
      err += (y * 2) + 1;
    }
  } while (x < 0);
};

Licensing.

While your library says MIT, I've searched all of zingl's pages and can't find any consistent licensing guidance anywhere. Do you have something to suggest this is an acceptable licensing?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.