mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-22 20:11:24 +01:00
1 line
No EOL
984 B
JavaScript
1 line
No EOL
984 B
JavaScript
"use strict";function lineclip(t,e,n){var r,i,o,u,s,h=t.length,c=bitCode(t[0],e),f=[];for(n||(n=[]),r=1;r<h;r++){for(i=t[r-1],u=s=bitCode(o=t[r],e);;){if(!(c|u)){f.push(i),u!==s?(f.push(o),r<h-1&&(n.push(f),f=[])):r===h-1&&f.push(o);break}if(c&u)break;c?c=bitCode(i=intersect(i,o,c,e),e):u=bitCode(o=intersect(i,o,u,e),e)}c=s}return f.length&&n.push(f),n}function polygonclip(t,e,n=0){var r,i,o,u,s,h,c,f;for(i=1;i<=8;i*=2){for(r=[],u=!(bitCode(o=t[t.length-1],e)&i),h=0;h<t.length;h++){s=(f=!(bitCode(c=t[h],e)&i))!==u;const l=intersect(o,c,i,e);s&&r.push(l),n&&s&&r.push(l,l),f&&r.push(c),o=c,u=f}if(!(t=r).length)break}return r}function intersect(t,e,n,r){return 8&n?[t[0]+(e[0]-t[0])*(r[3]-t[1])/(e[1]-t[1]),r[3]]:4&n?[t[0]+(e[0]-t[0])*(r[1]-t[1])/(e[1]-t[1]),r[1]]:2&n?[r[2],t[1]+(e[1]-t[1])*(r[2]-t[0])/(e[0]-t[0])]:1&n?[r[0],t[1]+(e[1]-t[1])*(r[0]-t[0])/(e[0]-t[0])]:null}function bitCode(t,e){var n=0;return t[0]<e[0]?n|=1:t[0]>e[2]&&(n|=2),t[1]<e[1]?n|=4:t[1]>e[3]&&(n|=8),n} |