forked from topstar210/illustrator-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check if a path fits inside of another path.js
216 lines (202 loc) · 6.09 KB
/
check if a path fits inside of another path.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
What you need to do to start working on it is make a document,
make your containing ellipse and call it "mycir" and test out a path called "mypth".
*/
function test () {
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {
var k;
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
var len = o.length >>> 0;
if (len === 0) {
return -1;
}
var n = +fromIndex || 0;
if (Math.abs(n) === Infinity) {
n = 0;
}
if (n >= len) {
return -1;
}
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
while (k < len) {
if (k in o && o[k] === searchElement) {
return k;
}
k++;
}
return -1;
};
};
function foreach (arr, func) {
for (var i = 0; i < arr.length; i++) {
func(arr[i]);
}
}
function marker (xy, container) {
var container = container || doc;
var z = container.pathItems.ellipse(xy[1] + 2, xy[0] - 2, 4, 4);
z.stroked = false;
z.filled = true;
z.fillColor = cclr;
}
function getPathPoints (path) { // collects pathPoints of a path into segment arrays of 2 anchors & 2 controls between them
var arr = [];
for (var i = 0, ln = path.pathPoints.length; i < ln; i++) { // designed to work on closed paths!
if (i < ln - 1) {
var p = path.pathPoints[i], pnext = path.pathPoints[i + 1];
} else {
var p = path.pathPoints[i], pnext = path.pathPoints[0];
}
arr.push([p.anchor, p.rightDirection, pnext.leftDirection, pnext.anchor]);
}
return arr;
}
function getBezierSegment (segArr) { // turns a pathPoint segment (gotten with getPathPoints) into bezier interpolated points
var anch2 = segArr[3], cont2 = segArr[2], cont1 = segArr[1], anch1 = segArr[0];
var resultPts = [];
var a = anch2[0] - 3 * (cont2[0]) + 3 * (cont1[0]) - anch1[0];
var b = 3 * (cont2[0]) - 6 * (cont1[0]) + 3 * anch1[0];
var c = 3 * (cont1[0]) - 3 * (anch1[0]);
var d = anch1[0];
var e = anch2[1] - 3 * (cont2[1]) + 3 * (cont1[1]) - anch1[1];
var f = 3 * (cont2[1]) - 6 * (cont1[1]) + 3 * anch1[1];
var g = 3 * (cont1[1]) - 3 * (anch1[1]);
var h = anch1[1];
var inc = 0.1;
for (var t = 0; t < 1; t += inc) {
resultPts.push([
a * Math.pow(t, 3) + b * Math.pow(t, 2) + (c * t) + d,
e * Math.pow(t, 3) + f * Math.pow(t, 2) + (g * t) + h
]);
}
return resultPts;
}
function getBezierPath (pathPts) { // turns entire series of segments (gotten with getPathPoints) into a complete set of interpolated bezier points
var pathArr = [];
for (var i = 0; i < pathPts.length; i++) {
var thisPtSet = pathPts[i];
var seg = getBezierSegment(thisPtSet);
if (
i > 0 && (seg[0][0] == pathArr[pathArr.length - 1][0] &&
seg[0][1] == pathArr[pathArr.length - 1][1])
) {
seg.splice(0, 1);
}
pathArr = pathArr.concat(seg);
}
return pathArr;
}
function markBezierPoints (seg) {
var grp = doc.groupItems.add();
grp.name = "MyGroup";
for (var i = 0; i < seg.length; i++) {
marker(seg, grp);
}
}
function makePolygon (pts) {
var pth = doc.pathItems.add();
pth.setEntirePath(pts);
pth.filled = false;
pth.stroked = true;
pth.strokeColor = cclr;
pth.strokeWidth = 1;
}
var RayCaster = { // the whole raycasting through polygon algorithm from http://rosettacode.org/wiki/Ray-casting_algorithm
// evaluates a single point to see if it's inside a polygon.
Point : function (x, y) {
this.x = x;
this.y = y;
},
pointInPoly : function (point, poly) {
var index, intersected, pointA, pointB, segment, segments;
segments = (function () {
var _i, _len, _results;
_results = [];
for (index = 0, _len = poly.length; index < _len; index++) {
pointA = poly[index];
pointB = poly[(index + 1) % poly.length];
_results.push([new RayCaster.Point(pointA[0], pointA[1]), new RayCaster.Point(pointB[0], pointB[1])]);
}
return _results;
})();
intersected = (function () {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = segments.length; _i < _len; _i++) {
segment = segments[_i];
if (
RayCaster.rayIntersectsSegment(new RayCaster.Point(point[0], point[1]), segment)
) {
_results.push(segment);
}
}
return _results;
})();
return intersected.length % 2 !== 0;
},
rayIntersectsSegment : function (p, segment) {
var a, b, mAB, mAP, p1, p2, _ref;
p1 = segment[0], p2 = segment[1];
_ref = p1.y < p2.y ? [p1, p2] : [p2, p1], a = _ref[0], b = _ref[1];
if (p.y === b.y || p.y === a.y) {
p.y += Number.MIN_VALUE;
}
if (p.y > b.y || p.y < a.y) {
return false;
} else if (p.x > a.x && p.x > b.x) {
return false;
} else if (p.x < a.x && p.x < b.x) {
return true;
} else {
mAB = (b.y - a.y) / (b.x - a.x);
mAP = (p.y - a.y) / (p.x - a.x);
return mAP > mAB;
}
}
};
function pathAnchorsInPoly (myPath, polyPts) {
var flag = false;
var all = [];
foreach(myPath.pathPoints, function (arg) {
if (RayCaster.pointInPoly(arg.anchor, polyPts) === false) {
all.push(false);
}
all.push(true);
});
flag = (all.indexOf(false) != -1) ? false : true;
if (flag === false) {
alert("NOT All Path Anchors are inside containing ellipse.");
} else {
alert("Yay! All Path Anchors are INSIDE the containing ellipse!");
}
return flag; // for use later (?)
}
if (app == "[Application Adobe Illustrator]" && app.documents.length > 0) {
app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
var cclr = new CMYKColor();
cclr.cyan = 0;
cclr.magenta = 100;
cclr.yellow = 100;
cclr.black = 0;
var doc = app.activeDocument;
try {
var m = doc.pathItems.getByName('mycir'); // my circle
var mp = doc.pathItems.getByName('mypth'); // my test path
} catch (e) {
alert("Put 2 paths into a document: a containing ellipse, name it 'mycir', and some random path- name it 'mypth'. Then run.");
return;
}
var pts = getPathPoints(m);
var bezPts = getBezierPath(pts);
// testing functions:
// markBezierPoints(bezPts);
// makePolygon(bezPts);
// foreach(bezPts, function(arg) {$.writeln(arg)});
pathAnchorsInPoly(mp, bezPts);
}
}
test();