MCPcopy Create free account
hub / github.com/microsoft/SandDance / clipRectangle

Function clipRectangle

docs/app/js/sanddance-app.js:133069–133188  ·  view source on GitHub ↗
(x0, y0, x1, y1)

Source from the content-addressed store, hash-verified

133067var _d3Array = require("d3-array");
133068var clipMax = 1e9, clipMin = -clipMax;
133069function clipRectangle(x0, y0, x1, y1) {
133070 function visible1(x, y) {
133071 return x0 <= x && x <= x1 && y0 <= y && y <= y1;
133072 }
133073 function interpolate(from, to, direction, stream) {
133074 var a = 0, a1 = 0;
133075 if (from == null || (a = corner(from, direction)) !== (a1 = corner(to, direction)) || comparePoint(from, to) < 0 ^ direction > 0) do stream.point(a === 0 || a === 3 ? x0 : x1, a > 1 ? y1 : y0);
133076 while ((a = (a + direction + 4) % 4) !== a1);
133077 else stream.point(to[0], to[1]);
133078 }
133079 function corner(p, direction) {
133080 return (0, _mathJs.abs)(p[0] - x0) < (0, _mathJs.epsilon) ? direction > 0 ? 0 : 3 : (0, _mathJs.abs)(p[0] - x1) < (0, _mathJs.epsilon) ? direction > 0 ? 2 : 1 : (0, _mathJs.abs)(p[1] - y0) < (0, _mathJs.epsilon) ? direction > 0 ? 1 : 0 : direction > 0 ? 3 : 2; // abs(p[1] - y1) < epsilon
133081 }
133082 function compareIntersection(a, b) {
133083 return comparePoint(a.x, b.x);
133084 }
133085 function comparePoint(a, b) {
133086 var ca = corner(a, 1), cb = corner(b, 1);
133087 return ca !== cb ? ca - cb : ca === 0 ? b[1] - a[1] : ca === 1 ? a[0] - b[0] : ca === 2 ? a[1] - b[1] : b[0] - a[0];
133088 }
133089 return function(stream) {
133090 var activeStream = stream, bufferStream = (0, _bufferJsDefault.default)(), segments, polygon, ring1, x__, y__, v__, x_, y_, v_, first, clean;
133091 var clipStream = {
133092 point: point1,
133093 lineStart: lineStart,
133094 lineEnd: lineEnd,
133095 polygonStart: polygonStart,
133096 polygonEnd: polygonEnd
133097 };
133098 function point1(x, y) {
133099 if (visible1(x, y)) activeStream.point(x, y);
133100 }
133101 function polygonInside() {
133102 var winding = 0;
133103 for(var i = 0, n = polygon.length; i < n; ++i)for(var ring = polygon[i], j = 1, m = ring.length, point = ring[0], a0, a1, b0 = point[0], b1 = point[1]; j < m; ++j){
133104 a0 = b0, a1 = b1, point = ring[j], b0 = point[0], b1 = point[1];
133105 if (a1 <= y1) {
133106 if (b1 > y1 && (b0 - a0) * (y1 - a1) > (b1 - a1) * (x0 - a0)) ++winding;
133107 } else if (b1 <= y1 && (b0 - a0) * (y1 - a1) < (b1 - a1) * (x0 - a0)) --winding;
133108 }
133109 return winding;
133110 }
133111 // Buffer geometry within a polygon and then clip it en masse.
133112 function polygonStart() {
133113 activeStream = bufferStream, segments = [], polygon = [], clean = true;
133114 }
133115 function polygonEnd() {
133116 var startInside = polygonInside(), cleanInside = clean && startInside, visible = (segments = (0, _d3Array.merge)(segments)).length;
133117 if (cleanInside || visible) {
133118 stream.polygonStart();
133119 if (cleanInside) {
133120 stream.lineStart();
133121 interpolate(null, null, 1, stream);
133122 stream.lineEnd();
133123 }
133124 if (visible) (0, _rejoinJsDefault.default)(segments, compareIntersection, startInside, interpolate, stream);
133125 stream.polygonEnd();
133126 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected