Coder Social home page Coder Social logo

w8r / bresenham-zingl Goto Github PK

View Code? Open in Web Editor NEW
26.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 Introduction

Bresenham rasterisation functions by Alois Zingl

Screenshot

Port of C code by Alois Zingl from this paper

ALOIS ZINGL: "A Rasterizing Algorithm for Drawing Curves", 8 November 2012 (2012-11-08), pages 1 - 81

Install

npm i -S bresenham-zingl
import { line, circle, quadBezier } from 'bresenham-zingl';

quadBezier(0,0, 10, 10, 0, 10, (x, y) => console.log(x, y)); // 0,0, ...

API

User-defined callbacks

setPixel(x, y)

Use that callback to fill the pixel on canvas.

Parameters

parameter type description
x number
y number

setPixelAlpha(x, y, alpha)

Callback that would also receive the alpha value for the pixel

Parameters

parameter type description
x number
y number
alpha number

line(x0, y0, x1, y1, setPixel)

Line segment rasterisation

Parameters

parameter type description
x0 number
y0 number
x1 number
y1 number
setPixel setPixel

lineAA(x0, y0, x1, y1, setPixelAA)

Draw a black (0) anti-aliased line on white (255) background

Parameters

parameter type description
x0 number
y0 number
x1 number
y1 number
setPixelAA setPixelAlpha

Returns number,

lineWidth(x0, y0, x1, y1, wd, setPixel)

Plot an anti-aliased line of width wd

Parameters

parameter type description
x0 number
y0 number
x1 number
y1 number
wd number
setPixel setPixel

quadRationalBezierSegment(x0, y0, x1, y1, x2, y2, w, setPixel)

plot a limited rational Bezier segment, squared weight

Parameters

parameter type description
x0 number
y0 number
x1 number
y1 number
x2 number
y2 number
w number
setPixel setPixel

quadRationalBezierSegmentAA(x0, y0, x1, y1, x2, y2, w, setPixelAA)

draw an anti-aliased rational quadratic Bezier segment, squared weight

Parameters

parameter type description
x0 number
y0 number
x1 number
y1 number
x2 number
y2 number
w number
setPixelAA setPixelAlpha

rotatedEllipse(x, y, a, b, angle, setPixel)

Plot ellipse rotated by angle (radian)

Parameters

parameter type description
x number
y number
a number
b number
angle number
setPixel setPixel

rotatedEllipseRect(x0, y0, x1, y1, zd, setPixel)

Rectangle encloMath.sing the ellipse, integer rotation angle

Parameters

parameter type description
x0 number
y0 number
x1 number
y1 number
zd number
setPixel setPixel

ellipseRect(x0, y0, x1, y1, setPixel)

Rectangular parameter encloMath.sing the ellipse

Parameters

parameter type description
x0 number
y0 number
x1 number
y1 number
setPixel setPixel

circle(xm, ym, r, setPixel)

Circle rasterisation

Parameters

parameter type description
xm number
ym number
r number
setPixel setPixel

circleAA(xm, ym, r, setPixelAA)

Draw a black anti-aliased circle on white background

Parameters

parameter type description
xm number
ym number
r number
setPixelAA setPixelAlpha

quadBezierSegment(x0, y0, x1, y1, x2, y2, setPixel)

plot a limited quadratic Bezier segment

Parameters

parameter type description
x0 number
y0 number
x1 number
y1 number
x2 number
y2 number
setPixel setPixel

quadBezierAA(x0, y0, x1, y1, x2, y2, setPixelAA)

Plot any quadratic Bezier curve with anti-alias

Parameters

parameter type description
x0 number
y0 number
x1 number
y1 number
x2 number
y2 number
setPixelAA setPixelAlpha

quadBezierSegmentAA(x0, y0, x1, y1, x2, y2, setPixelAA)

Draw an limited anti-aliased quadratic Bezier segment

Parameters

parameter type description
x0 number
y0 number
x1 number
y1 number
x2 number
y2 number
setPixelAA setPixelAlpha

cubicBezier(x0, y0, x1, y1, x2, y2, x3, y3, setPixel)

plot any cubic Bezier curve

Parameters

parameter type description
x0 number
y0 number
x1 number
y1 number
x2 number
y2 number
x3 number
y3 number
setPixel setPixel

cubicBezierAA(x0, y0, x1, y1, x2, y2, x3, y3, setPixelAA)

plot any cubic Bezier curve

Parameters

parameter type description
x0 number
y0 number
x1 number
y1 number
x2 number
y2 number
x3 number
y3 number
setPixel setPixelAlpha

License

(The MIT License)

Copyright (c) 2012 Alois Zingl

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

bresenham-zingl's People

Contributors

w8r avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.

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?

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);
};

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.