UNPKG

bresenham-zingl

Version:

Bresenham rasterisation algorithms by Alois Zingl

2 lines (1 loc) 11.9 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function N(t,a,o,f,h){const e=Math.abs(o-t),i=t<o?1:-1,l=-Math.abs(f-a),n=a<f?1:-1;let r=e+l,M;for(;;){if(h(t,a),M=2*r,M>=l){if(t===o)break;r+=l,t+=i}if(M<=e){if(a===f)break;r+=e,a+=n}}}function Q(t,a,o,f,h){const e=t<o?1:-1,i=a<f?1:-1;let l,n=Math.abs(o-t),r=Math.abs(f-a),M=n*n+r*r,c=M==0?1:16777087/Math.sqrt(M);for(n*=c,r*=c,M=n-r;;){if(h(t,a,Math.abs(M-n+r)>>16),c=M,l=t,2*c>=-n){if(t==o)break;c+r<16711680&&h(t,a+i,c+r>>16),M-=r,t+=e}if(2*c<=r){if(a==f)break;n-c<16711680&&h(l+e,a,n-c>>16),M+=n,a+=i}}}function A(t,a,o,f,h,e){let i=Math.abs(o-t),l=t<o?1:-1,n=Math.abs(f-a),r=a<f?1:-1,M=i-n,c,q,u,b=i+n==0?1:Math.sqrt(i*i+n*n);for(h=(h+1)/2;;){if(e(t,a,Math.max(0,255*(Math.abs(M-i+n)/b-h+1))),c=M,q=t,2*c>=-i){for(c+=n,u=a;c<b*h&&(f!=u||i>n);c+=i)e(t,u+=r,Math.max(0,255*(Math.abs(c)/b-h+1)));if(t==o)break;c=M,M-=n,t+=l}if(2*c<=n){for(c=i-c;c<b*h&&(o!=q||i<n);c+=n)e(q+=l,a,Math.max(0,255*(Math.abs(c)/b-h+1)));if(a==f)break;M+=i,a+=r}}}function I(t,a="assert error"){if(!t)throw new Error(a)}function w(t,a,o,f,h,e,i,l){var n=t-2*o+h,r=a-2*f+e,M=t-o,c=a-f,q,u,b;I(i>=0,"width is negative"),M*(h-o)>0&&(c*(e-f)>0&&Math.abs(M*r)>Math.abs(c*n)&&(t=h,h=M+o,a=e,e=c+f),t==h||i==1?u=(t-o)/n:(b=Math.sqrt(4*i*i*(t-o)*(h-o)+(h-t)*(h-t)),o<t&&(b=-b),u=(2*i*(t-o)-t+h+b)/(2*(1-i)*(h-t))),b=1/(2*u*(1-u)*(i-1)+1),M=(u*u*(t-2*i*o+h)+2*u*(i*o-t)+t)*b,c=(u*u*(a-2*i*f+e)+2*u*(i*f-a)+a)*b,q=u*(i-1)+1,q*=q*b,i=((1-u)*(i-1)+1)*Math.sqrt(b),n=Math.floor(M+.5),r=Math.floor(c+.5),c=(M-t)*(f-a)/(o-t)+a,H(t,a,n,Math.floor(c+.5),n,r,q,l),c=(M-h)*(f-e)/(o-h)+e,f=Math.floor(c+.5),t=o=n,a=r),(a-f)*(e-f)>0&&(a==e||i==1?u=(a-f)/(a-2*f+e):(b=Math.sqrt(4*i*i*(a-f)*(e-f)+(e-a)*(e-a)),f<a&&(b=-b),u=(2*i*(a-f)-a+e+b)/(2*(1-i)*(e-a))),b=1/(2*u*(1-u)*(i-1)+1),M=(u*u*(t-2*i*o+h)+2*u*(i*o-t)+t)*b,c=(u*u*(a-2*i*f+e)+2*u*(i*f-a)+a)*b,q=u*(i-1)+1,q*=q*b,i=((1-u)*(i-1)+1)*Math.sqrt(b),n=Math.floor(M+.5),r=Math.floor(c+.5),M=(o-t)*(c-a)/(f-a)+t,H(t,a,Math.floor(M+.5),r,n,r,q,l),M=(o-h)*(c-e)/(f-e)+h,o=Math.floor(M+.5),t=n,a=f=r),H(t,a,o,f,h,e,i*i,l)}function H(t,a,o,f,h,e,i,l){var n=h-o,r=e-f,M=t-h,c=a-e,q=t-o,u=a-f,b=q*r+u*n,s=q*r-u*n,d;if(I(q*n<=0&&u*r<=0,"sign of gradient must not change"),s!=0&&i>0){if(n*n+r*r>q*q+u*u&&(h=t,t-=M,e=a,a-=c,s=-s),q=2*(4*i*n*q+M*M),u=2*(4*i*r*u+c*c),n=t<h?1:-1,r=a<e?1:-1,b=-2*n*r*(2*i*b+M*c),s*n*r<0&&(q=-q,u=-u,b=-b,s=-s),M=4*i*(o-t)*r*s+q/2+b,c=4*i*(a-f)*n*s+u/2+b,i<.5&&(c>b||M<b)){s=(i+1)/2,i=Math.sqrt(i),b=1/(i+1),n=Math.floor((t+2*i*o+h)*b/2+.5),r=Math.floor((a+2*i*f+e)*b/2+.5),M=Math.floor((i*o+t)*b+.5),c=Math.floor((f*i+a)*b+.5),H(t,a,M,c,n,r,s,l),M=Math.floor((i*o+h)*b+.5),c=Math.floor((f*i+e)*b+.5),H(n,r,M,c,h,e,s,l);return}d=M+c-b;do{if(l(t,a),t==h&&a==e)return;o=2*d>c?1:0,f=2*(d+u)<-c?1:0,(2*d<M||f)&&(a+=r,c+=b,d+=M+=q),(2*d>M||o)&&(t+=n,M+=b,d+=c+=u)}while(c<=b&&M>=b)}N(t,a,h,e,l)}function V(t,a,o,f,h,e,i,l){var n=h-o,r=e-f,M=t-h,c=a-e,q=t-o,u=a-f,b=q*r+u*n,s=q*r-u*n,d,z,W;if(I(q*n<=0&&u*r<=0),s!=0&&i>0){if(n*n+r*r>q*q+u*u&&(h=t,t-=M,e=a,a-=c,s=-s),q=2*(4*i*n*q+M*M),u=2*(4*i*r*u+c*c),n=t<h?1:-1,r=a<e?1:-1,b=-2*n*r*(2*i*b+M*c),s*n*r<0&&(q=-q,u=-u,s=-s,b=-b),M=4*i*(o-t)*r*s+q/2+b,c=4*i*(a-f)*n*s+u/2+b,i<.5&&c>M)return s=(i+1)/2,i=Math.sqrt(i),b=1/(i+1),n=Math.floor((t+2*i*o+h)*b/2+.5),r=Math.floor((a+2*i*f+e)*b/2+.5),M=Math.floor((i*o+t)*b+.5),c=Math.floor((f*i+a)*b+.5),V(t,a,M,c,n,r,s,l),M=Math.floor((i*o+h)*b+.5),c=Math.floor((f*i+e)*b+.5),V(n,r,M,c,h,e,s,l);d=M+c-b;do{if(s=Math.min(M-b,b-c),z=Math.max(M-b,b-c),z+=2*z*s*s/(4*z*z+s*s),o=255*Math.abs(d-M-c+b)/z,o<256&&l(t,a,o),W=2*d+c<0){if(a==e)return;M-d<z&&l(t+n,a,255*Math.abs(M-d)/z)}if(2*d+M>0){if(t==h)return;d-c<z&&l(t,a+r,255*Math.abs(d-c)/z),t+=n,M+=b,d+=c+=u}W&&(a+=r,c+=b,d+=M+=q)}while(c<M)}Q(t,a,h,e,l)}function P(t,a,o,f,h){let e=-o,i=0,l=f*f,n=e*(2*l+e)+l;do h(t-e,a+i),h(t+e,a+i),h(t+e,a-i),h(t-e,a-i),l=2*n,l>=(e*2+1)*f*f&&(n+=(++e*2+1)*f*f),l<=(i*2+1)*o*o&&(n+=(++i*2+1)*o*o);while(e<=0);for(;i++<f;)h(t,a+i),h(t,a-i)}function y(t,a,o,f,h,e){let i=o*o,l=f*f;const n=Math.sin(h);let r=(i-l)*n;i=Math.sqrt(i-r*n),l=Math.sqrt(l+r*n),o=i+.5,f=l+.5,r=r*o*f/(i*l),Y(t-o,a-f,t+o,a+f,4*r*Math.cos(h),e)}function Y(t,a,o,f,h,e){let i=o-t,l=f-a,n=i*l;if(h===0)return Z(t,a,o,f,e);n!==0&&(n=(n-h)/(n+n)),I(n<=1&&n>=0,"limit angle to |zd|<=xd*yd"),i=Math.floor(i*n+.5),l=Math.floor(l*n+.5),H(t,a+l,t,a,t+i,a,1-n,e),H(t,a+l,t,f,o-i,f,n,e),H(o,f-l,o,f,o-i,f,1-n,e),H(o,f-l,o,a,t+i,a,n,e)}function Z(t,a,o,f,h){let e=Math.abs(o-t),i=Math.abs(f-a),l=i&1,n=4*(1-e)*i*i,r=4*(l+1)*e*e,M=n+r+l*e*e,c;t>o&&(t=o,o+=e),a>f&&(a=f),a+=(i+1)/2,f=a-l,e=8*e*e,l=8*i*i;do h(o,a),h(t,a),h(t,f),h(o,f),c=2*M,c<=r&&(a++,f--,M+=r+=e),(c>=n||2*M>r)&&(t++,o--,M+=n+=l);while(t<=o);for(;a-f<=i;)h(t-1,a),h(o+1,a++),h(t-1,f),h(o+1,f--)}function x(t,a,o,f){var h=-o,e=0,i=2-2*o;do f(t-h,a+e),f(t-e,a-h),f(t+h,a-e),f(t+e,a+h),o=i,o<=e&&(i+=++e*2+1),(o>h||i>e)&&(i+=++h*2+1);while(h<0)}function aa(t,a,o,f){var h=-o,e=0,i,l,n,r=2-2*o;o=1-r;do i=255*Math.abs(r-2*(h+e)-2)/o,f(t-h,a+e,i),f(t-e,a-h,i),f(t+h,a-e,i),f(t+e,a+h,i),n=r,l=h,r+e>0&&(i=255*(r-2*h-1)/o,i<256&&(f(t-h,a+e+1,i),f(t-e-1,a-h,i),f(t+h,a-e-1,i),f(t+e+1,a+h,i)),r+=++h*2+1),n+l<=0&&(i=255*(2*e+3-n)/o,i<256&&(f(t-l-1,a+e,i),f(t-e,a-l-1,i),f(t+l+1,a-e,i),f(t+e,a+l+1,i)),r+=++e*2+1);while(h<0)}function ta(t,a,o,f,h,e,i){var l=t-o,n=a-f,r=t-2*o+h,M;l*(h-o)>0&&(n*(e-f)>0&&Math.abs((a-2*f+e)/r*l)>Math.abs(n)&&(t=h,h=l+o,a=e,e=n+f),r=(t-o)/r,M=(1-r)*((1-r)*a+2*r*f)+r*r*e,r=(t*h-o*o)*r/(t-o),l=Math.floor(r+.5),n=Math.floor(M+.5),M=(f-a)*(r-t)/(o-t)+a,K(t,a,l,Math.floor(M+.5),l,n,i),M=(f-e)*(r-h)/(o-h)+e,t=o=l,a=n,f=Math.floor(M+.5)),(a-f)*(e-f)>0&&(r=a-2*f+e,r=(a-f)/r,M=(1-r)*((1-r)*t+2*r*o)+r*r*h,r=(a*e-f*f)*r/(a-f),l=Math.floor(M+.5),n=Math.floor(r+.5),M=(o-t)*(r-a)/(f-a)+t,K(t,a,Math.floor(M+.5),n,l,n,i),M=(o-h)*(r-e)/(f-e)+h,t=l,o=Math.floor(M+.5),a=f=n),K(t,a,o,f,h,e,i)}function K(t,a,o,f,h,e,i){var l=h-o,n=e-f,r=t-o,M=a-f,c,q,u,b,s=r*n-M*l;if(I(r*l<=0&&M*n<=0,"sign of gradient must not change"),l*l+n*n>r*r+M*M&&(h=t,t=l+o,e=a,a=n+f,s=-s),s!=0){r+=l,r*=l=t<h?1:-1,M+=n,M*=n=a<e?1:-1,c=2*r*M,r*=r,M*=M,s*l*n<0&&(r=-r,M=-M,c=-c,s=-s),q=4*n*s*(o-t)+r-c,u=4*l*s*(a-f)+M-c,r+=r,M+=M,b=q+u+c;do{if(i(t,a),t==h&&a==e)return;f=2*b<q,2*b>u&&(t+=l,q-=c,b+=u+=M),f&&(a+=n,u-=c,b+=q+=r)}while(u<0&&q>0)}N(t,a,h,e,i)}function fa(t,a,o,f,h,e,i){var l=t-o,n=a-f,r=t-2*o+h,M;l*(h-o)>0&&(n*(e-f)>0&&Math.abs((a-2*f+e)/r*l)>Math.abs(n)&&(t=h,h=l+o,a=e,e=n+f),r=(t-o)/r,M=(1-r)*((1-r)*a+2*r*f)+r*r*e,r=(t*h-o*o)*r/(t-o),l=Math.floor(r+.5),n=Math.floor(M+.5),M=(f-a)*(r-t)/(o-t)+a,L(t,a,l,Math.floor(M+.5),l,n,i),M=(f-e)*(r-h)/(o-h)+e,t=o=l,a=n,f=Math.floor(M+.5)),(a-f)*(e-f)>0&&(r=a-2*f+e,r=(a-f)/r,M=(1-r)*((1-r)*t+2*r*o)+r*r*h,r=(a*e-f*f)*r/(a-f),l=Math.floor(M+.5),n=Math.floor(r+.5),M=(o-t)*(r-a)/(f-a)+t,L(t,a,Math.floor(M+.5),n,l,n,i),M=(o-h)*(r-e)/(f-e)+h,t=l,o=Math.floor(M+.5),a=f=n),L(t,a,o,f,h,e,i)}function L(t,a,o,f,h,e,i){var l=h-o,n=e-f,r=t-o,M=a-f,c,q,u,b,s,d=r*n-M*l;if(l*l+n*n>r*r+M*M&&(h=t,t=l+o,e=a,a=n+f,d=-d),d!=0){r+=l,r*=l=t<h?1:-1,M+=n,M*=n=a<e?1:-1,c=2*r*M,r*=r,M*=M,d*l*n<0&&(r=-r,M=-M,c=-c,d=-d),q=4*n*(o-t)*d+r-c,u=4*l*(a-f)*d+M-c,r+=r,M+=M,b=q+u+c;do{if(d=Math.min(q+c,-c-u),s=Math.max(q+c,-c-u),s+=2*s*d*d/(4*s*s+d*d),i(t,a,255*Math.abs(b-q-u-c)/s),t==h||a==e)break;o=t,d=q-b,f=2*b+u<0,2*b+q>0&&(b-u<s&&i(t,a+n,255*Math.abs(b-u)/s),t+=l,q-=c,b+=u+=M),f&&(d<s&&i(o+l,a,255*Math.abs(d)/s),a+=n,u-=c,b+=q+=r)}while(u<q)}Q(t,a,h,e,i)}function _(t,a,o,f,h,e,i,l,n){var r,M,c,q=1;let u=t<i?1:-1,b=a<l?1:-1,s=-Math.abs(t+o-h-i),d=s-4*u*(o-h),z=u*(t-o-h+i),W=-Math.abs(a+f-e-l),B=W-4*b*(f-e),k=b*(a-f-e+l),j,p,G,E,O,R,T,v,g,S,F,D=.01;if(I((o-t)*(h-i)<D&&((i-t)*(o-h)<D||z*z<d*s+D),"slope change"),I((f-a)*(e-l)<D&&((l-a)*(f-e)<D||k*k<B*W+D),"slope change"),d==0&&B==0)return u=Math.floor((3*o-t+1)/2),b=Math.floor((3*f-a+1)/2),K(t,a,u,b,i,l,n);o=(o-t)*(o-t)+(f-a)*(f-a)+1,h=(h-i)*(h-i)+(e-l)*(e-l)+1;do{j=d*k-z*B,p=d*W-s*B,G=z*W-s*k,S=j*(j+p-3*G)+p*p,r=S>0?1:Math.sqrt(1+1024/o),j*=r,p*=r,G*=r,S*=r*r,R=9*(j+p+G)/8,E=8*(d-B),v=27*(8*j*(k*k-B*W)+S*(B+2*k+W))/64-B*B*(R-B),g=27*(8*j*(z*z-d*s)-S*(d+2*z+s))/64-d*d*(R+d),O=3*(3*j*(3*k*k-B*B-2*B*W)-B*(3*p*(B+k)+B*E))/4,T=3*(3*j*(3*z*z-d*d-2*d*s)-d*(3*p*(d+z)+d*E))/4,R=d*B*(6*j+6*p-3*G+E),p=B*B,E=d*d,R=3*(R+9*r*(E*k*W-z*s*p)-18*z*k*j)/8,S<0&&(v=-v,g=-g,O=-O,T=-T,R=-R,p=-p,E=-E),j=6*B*p,p=-6*d*p,G=6*B*E,E=-6*d*E,v+=R,S=v+g,g+=R;a:for(F=R,M=c=r;t!=i&&a!=l;){n(t,a);do{if(v>F||g<F)break a;f=2*S-g,2*S>=v&&(M--,S+=v+=O,g+=R+=p,T+=G,O+=j),f<=0&&(c--,S+=g+=T,v+=R+=G,O+=p,T+=E)}while(M>0&&c>0);2*M<=r&&(t+=u,M+=r),2*c<=r&&(a+=b,c+=r),F==R&&v<0&&g>0&&(F=D)}O=t,t=i,i=O,u=-u,z=-z,T=a,a=l,l=T,b=-b,k=-k,o=h}while(q--);N(t,a,i,l,n)}function $(t,a,o,f,h,e,i,l,n){let r,M,c,q=1,u=t<i?1:-1,b=a<l?1:-1,s=-Math.abs(t+o-h-i),d=s-4*u*(o-h),z=u*(t-o-h+i),W=-Math.abs(a+f-e-l),B=W-4*b*(f-e),k=b*(a-f-e+l),j,p,G,E,O,R,T,v,g,S,F,D,C,X;const J=.01;if(I((o-t)*(h-i)<J&&((i-t)*(o-h)<J||z*z<d*s+J)),I((f-a)*(e-l)<J&&((l-a)*(f-e)<J||k*k<B*W+J)),d===0&&B===0)return u=Math.floor((3*o-t+1)/2),b=Math.floor((3*f-a+1)/2),L(t,a,u,b,i,l,n);o=(o-t)*(o-t)+(f-a)*(f-a)+1,h=(h-i)*(h-i)+(e-l)*(e-l)+1;do{j=d*k-z*B,p=d*W-s*B,G=z*W-s*k,X=4*j*G-p*p,S=j*(j+p-3*G)+p*p,r=S>0?1:Math.sqrt(1+1024/o),j*=r,p*=r,G*=r,S*=r*r,R=9*(j+p+G)/8,E=8*(d-B),v=27*(8*j*(k*k-B*W)+S*(B+2*k+W))/64-B*B*(R-B),g=27*(8*j*(z*z-d*s)-S*(d+2*z+s))/64-d*d*(R+d),O=3*(3*j*(3*k*k-B*B-2*B*W)-B*(3*p*(B+k)+B*E))/4,T=3*(3*j*(3*z*z-d*d-2*d*s)-d*(3*p*(d+z)+d*E))/4,R=d*B*(6*j+6*p-3*G+E),p=B*B,E=d*d,R=3*(R+9*r*(E*k*W-z*s*p)-18*z*k*j)/8,S<0&&(v=-v,g=-g,O=-O,T=-T,R=-R,p=-p,E=-E),j=6*B*p,p=-6*d*p,G=6*B*E,E=-6*d*E,v+=R,S=v+g,g+=R;let U=!1;a:for(M=c=r;t!==i&&a!==l;){f=Math.min(Math.abs(R-v),Math.abs(g-R)),C=Math.max(Math.abs(R-v),Math.abs(g-R)),C=r*(C+2*C*f*f/(4*C*C+f*f)),f=255*Math.abs(S-(r-M+1)*v-(r-c+1)*g+r*R)/C,f<256&&n(t,a,f),F=Math.abs(S-(r-M+1)*v+(c-1)*g),D=Math.abs(S+(M-1)*v-(r-c+1)*g),e=a;do{if(X>=-J&&(v+O>R||g+T<R)){U=!0;break a}if(f=2*S+v,2*S+g>0)M--,S+=v+=O,g+=R+=p,T+=G,O+=j;else if(f>0){U=!0;break a}f<=0&&(c--,S+=g+=T,v+=R+=G,O+=p,T+=E)}while(M>0&&c>0);2*c<=r&&(D<C&&n(t+u,a,255*D/C),a+=b,c+=r),2*M<=r&&(F<C&&n(t,e+b,255*F/C),t+=u,M+=r)}U&&(2*S<g&&2*c<=r+2&&(D<C&&n(t+u,a,255*D/C),a+=b),2*S>v&&2*M<=r+2&&(F<C&&n(t,e+b,255*F/C),t+=u),O=t,t=i,i=O,u=-u,z=-z,T=a,a=l,l=T,b=-b,k=-k,o=h);break}while(q--);Q(t,a,i,l,n)}function m(t,a,o,f,h,e,i,l,n,r){let M=0,c=0,q=t+o-h-i,u=q-4*(o-h),b=t-o-h+i,s=b+4*(o+h),d=a+f-e-l,z=d-4*(f-e),W=a-f-e+l,B=W+4*(f+e);var k=t,j,p,G,E=a,O,R,T;let v=b*b-u*q,g;const S=[0,0,0,0,0];for(u==0?Math.abs(q)<2*Math.abs(b)&&(S[M++]=q/(2*b)):v>0&&(g=Math.sqrt(v),v=(b-g)/u,Math.abs(v)<1&&(S[M++]=v),v=(b+g)/u,Math.abs(v)<1&&(S[M++]=v)),v=W*W-z*d,z==0?Math.abs(d)<2*Math.abs(W)&&(S[M++]=d/(2*W)):v>0&&(g=Math.sqrt(v),v=(W-g)/z,Math.abs(v)<1&&(S[M++]=v),v=(W+g)/z,Math.abs(v)<1&&(S[M++]=v)),c=1;c<M;c++)(v=S[c-1])>S[c]&&(S[c-1]=S[c],S[c]=v,c=0);for(v=-1,S[M]=1,c=0;c<=M;c++)g=S[c],j=(v*(v*b-2*q)-g*(v*(v*u-2*b)+q)+s)/8-k,O=(v*(v*W-2*d)-g*(v*(v*z-2*W)+d)+B)/8-E,p=(g*(g*b-2*q)-v*(g*(g*u-2*b)+q)+s)/8-k,R=(g*(g*W-2*d)-v*(g*(g*z-2*W)+d)+B)/8-E,k-=G=(g*(g*(3*b-g*u)-3*q)+s)/8,E-=T=(g*(g*(3*W-g*z)-3*d)+B)/8,i=Math.floor(G+.5),l=Math.floor(T+.5),k!=0&&(j*=k=(t-i)/k,p*=k),E!=0&&(O*=E=(a-l)/E,R*=E),(t!=i||a!=l)&&n(t,a,t+j,a+O,t+p,a+R,i,l,r),t=i,a=l,k=G,E=T,v=g}function ra(t,a,o,f,h,e,i,l,n){m(t,a,o,f,h,e,i,l,_,n)}function ia(t,a,o,f,h,e,i,l,n){m(t,a,o,f,h,e,i,l,$,n)}exports.circle=x;exports.circleAA=aa;exports.cubicBezier=ra;exports.cubicBezierAA=ia;exports.cubicBezierSegment=_;exports.cubicBezierSegmentAA=$;exports.ellipse=P;exports.ellipseRect=Z;exports.line=N;exports.lineAA=Q;exports.lineWidth=A;exports.quadBezier=ta;exports.quadBezierAA=fa;exports.quadBezierSegment=K;exports.quadBezierSegmentAA=L;exports.quadRationalBezier=w;exports.quadRationalBezierSegment=H;exports.quadRationalBezierSegmentAA=V;exports.rotatedEllipse=y;exports.rotatedEllipseRect=Y;