on master help sign in
add `insideForm :: Point -> Form -> Bool` function that checks to see if a point is inside of a form.
commitbb1d53de2e0aa88d4a7dbcc39d37d8ca35e627af authorevancz <info@elm-lang.org> dateThu 27 Sep 2012, 17:18 (14 years ago) parenteb748ae
2 files changed+118−16
core-js/Graphics/Collage.js+105−9 core-js/Graphics/Element.js+13−7
core-js/Graphics/Collage.js+105−9
@@ -70,27 +70,22 @@ var customLineHelp = function(ctx, pattern, points) {
7070};
7171
7272function drawLine(ctx,form) {
73− var points = Foreign.JavaScript.castListToJSArray(form[3][1]);
73+ var points = form[3][1];
7474 switch(form[1][0]) {
7575 case "Solid" : return solid(ctx,form[2],points);
7676 case "Dotted": return customLine([3,3],ctx,form[2],points);
7777 case "Dashed": return customLine([8,4],ctx,form[2],points);
78− case "Custom":
79− var pattern = Foreign.JavaScript.castListToJSArray(form[1][1]);
80− customLine(pattern,ctx,form[2],points);
78+ case "Custom": return customLine(form[1][1],ctx,form[2],points);
8179 }
8280};
8381
8482function drawShape(redo,ctx,shapeStyle,color,points) {
85− points = Foreign.JavaScript.castListToJSArray(points);
86− if (points.length > 0) points.push(points[0]);
8783 switch(shapeStyle[0]) {
8884 case "Filled": return filled(ctx,color,points);
8985 case "Outlined": return solid(ctx,color,points);
9086 case "Textured": return textured(redo,ctx,shapeStyle[1],points);
9187 case "CustomOutline":
92− var pattern = Foreign.JavaScript.castListToJSArray(shapeStyle[1]);
93− customLine(pattern,ctx,color,points);
88+ return customLine(shapeStyle[1],ctx,color,points);
9489 }
9590};
9691
@@ -209,6 +204,107 @@ function updateCollage(node,currs,nexts) {
209204 }
210205}
211206
212−return {collage:collage, updateCollage:updateCollage};
207+function style(clr,n,list) {
208+ return ["Tuple2",
209+ '<span style="font-size:100%;color:' + clr + ';">' + n + '</span>',
210+ list];
211+}
212+
213+function insideForm(point) { return function(form) {
214+ if (!inBoundsOf(point[1],point[2],form)) return false;
215+ var hw, hh;
216+ switch (form[4][0]) {
217+ case "FShape": return insideShape(point,form[1],form[2],form[3],form[4][3][1]);
218+ case "FLine": return false;
219+ case "FImage":
220+ hw = form[4][1] / 2;
221+ hh = form[4][2] / 2;
222+ break;
223+ case "FElement":
224+ hw = form[4][1][3] / 2;
225+ hh = form[4][1][4] / 2;
226+ break;
227+ }
228+ return insideShape(point,form[1],form[2],form[3],
229+ [ [null, hw, hh],
230+ [null,-hw, hh],
231+ [null,-hw,-hh],
232+ [null, hw,-hh],
233+ [null, hw, hh] ]);
234+ };
235+}
236+
237+function inBoundsOf(px,py,form) {
238+ if (form.length < 6) {
239+ var fx = form[3][1], fy = form[3][2];
240+ var radiusSquared = 0;
241+ var scale = form[2];
242+ switch (form[4][0]) {
243+ case "FShape":
244+ var points = form[4][3][1];
245+ for (var i = points.length; --i; ) {
246+ var p = points[i];
247+ radiusSquared = Math.max(radiusSquared, p[1]*p[1] + p[2]*p[2]);
248+ }
249+ radiusSquared *= scale * scale;
250+ break;
251+ case "FLine":
252+ break;
253+ case "FImage":
254+ var x = scale * form[4][1] / 2;
255+ var y = scale * form[4][2] / 2;
256+ radiusSquared = x*x + y*y;
257+ break;
258+ case "FElement":
259+ var x = scale * form[4][1][3] / 2;
260+ var y = scale * form[4][1][4] / 2;
261+ radiusSquared = x*x + y*y;
262+ break;
263+ }
264+ form.push(function(px,py) {
265+ var dx = px - fx;
266+ var dy = py - fy;
267+ return dx*dx + dy*dy < radiusSquared + 1;
268+ });
269+ }
270+ return form[5](px,py);
271+}
272+
273+function insideShape(point,theta,scale,pos,points) {
274+ var counter = 0;
275+ var list = ["Nil"];
276+ var p1,p2;
277+
278+ var x = (point[1] - pos[1]) / scale;
279+ var y = (point[2] - pos[2]) / scale;
280+ if (theta !== 0) {
281+ var angle = 2 * Math.PI * theta;
282+ var t = x === 0 ? (y > 0 ? Math.PI/2 : -Math.PI/2) - angle
283+ : (Math.atan(y/x) + (x > 0 ? 0 : Math.PI)) - angle;
284+ var r = Math.sqrt(x*x + y*y);
285+ x = r * Math.cos(t);
286+ y = r * Math.sin(t);
287+ }
288+
289+ if (points.length === 0) { return false; }
290+ p1 = points[0];
291+ for (var i = points.length - 1; i--; ) {
292+ p2 = points[i];
293+ var p1x = p1[1], p1y = p1[2], p2x = p2[1], p2y = p2[2];
294+
295+ if (p1y < p2y) {var ymin=p1y, ymax=p2y;} else {var ymin=p2y, ymax=p1y;}
296+ if (p1x < p2x) {var xmin=p1x, xmax=p2x;} else {var xmin=p2x, xmax=p1x;}
297+
298+ if (ymin < y && y <= ymax && x <= xmax) {
299+ if (x <= xmin || x <= ((y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x)) {
300+ ++counter;
301+ }
302+ }
303+ p1 = p2;
304+ }
305+ return (counter % 2) === 1;
306+}
307+
308+return {collage:collage, updateCollage:updateCollage, insideForm:insideForm};
213309
214310}();
core-js/Graphics/Element.js+13−7
@@ -80,19 +80,21 @@ ElmCode.Graphics.Element = function() {
8080 var Dotted_69 = ["Dotted"];
8181 var Dashed_70 = ["Dashed"];
8282 function Custom_71(a1) {
83− return["Custom", a1]
83+ return["Custom", Foreign.JavaScript.castListToJSArray(a1)]
8484 }
8585 var Filled_72 = ["Filled"];
8686 var Outlined_73 = ["Outlined"];
8787 function CustomOutline_74(a1) {
88− return["CustomOutline", a1]
88+ return["CustomOutline", Foreign.JavaScript.castListToJSArray(a1)]
8989 }
9090 function Line_75(a1) {
91− return["Line", a1]
91+ return["Line", Foreign.JavaScript.castListToJSArray(a1)]
9292 }
9393 function Shape_78(a1) {
9494 return function(a2) {
95− return["Shape", a1, a2]
95+ var points = Foreign.JavaScript.castListToJSArray(a1);
96+ if (points.length > 0) { points.push(points[0]); }
97+ return["Shape", points, a2];
9698 }
9799 }
98100 function Form_84(a1) {
@@ -365,7 +367,10 @@ ElmCode.Graphics.Element = function() {
365367 function rect_80(w_174) {
366368 return function(h_175) {
367369 return function(pos_176) {
368− return Shape_78(["Cons", ["Tuple2", 0 - w_174 / 2, 0 - h_175 / 2], ["Cons", ["Tuple2", 0 - w_174 / 2, h_175 / 2], ["Cons", ["Tuple2", w_174 / 2, h_175 / 2], ["Cons", ["Tuple2", w_174 / 2, 0 - h_175 / 2], ["Nil"]]]]])(pos_176)
370+ return Shape_78(["Cons", ["Tuple2", 0 - w_174 / 2, 0 - h_175 / 2],
371+ ["Cons", ["Tuple2", 0 - w_174 / 2, h_175 / 2],
372+ ["Cons", ["Tuple2", w_174 / 2, h_175 / 2],
373+ ["Cons", ["Tuple2", w_174 / 2, 0 - h_175 / 2], ["Nil"]]]]])(pos_176)
369374 }
370375 }
371376 }
@@ -457,7 +462,7 @@ ElmCode.Graphics.Element = function() {
457462 function customOutline_95(pattern) {
458463 return function(clr) {
459464 return function(shape) {
460− return Form_84(0)(1)(shape[2])(FShape_86(["CustomOutline",pattern])(clr)(shape));
465+ return Form_84(0)(1)(shape[2])(FShape_86(CustomOutline_74(pattern))(clr)(shape));
461466 }
462467 }
463468 }
@@ -506,5 +511,6 @@ ElmCode.Graphics.Element = function() {
506511 }
507512 return{left:left_6, right:right_7, down:down_8, up:up_9, inward:inward_10, outward:outward_11, topLeft:topLeft_22, topRight:topRight_23, bottomLeft:bottomLeft_24, bottomRight:bottomRight_25, midLeft:midLeft_26, midRight:midRight_27, midTop:midTop_28, midBottom:midBottom_29, middle:middle_30, middleAt:middleAt, topLeftAt:topLeftAt_31, topRightAt:topRightAt_32, bottomLeftAt:bottomLeftAt_33, bottomRightAt:bottomRightAt_34, absolute:absolute_35, relative:relative_36, width:width_47, height:height_48, size:size_49, opacity:opacity_50,
508513 color:color_51, link:link, widthOf:widthOf_52, heightOf:heightOf_53, sizeOf:sizeOf_54, text:text_56, asText:asText, plainText:plainText, centeredText:centeredText, justifiedText:justifiedText, rightedText:rightedText, image:image_57, images:images, video:video_58, fittedImage:fittedImage_59, flow:flow_60, above:above_61, below:below_62, beside:beside_63, layers:layers_64, collage:collage_65, spacer:spacer_66, container:container_67, line:line_76, segment:segment_77, polygon:polygon_79, rect:rect_80, oval:oval_81, circle:circle_82, ngon:ngon_83, solid:solid_89, dotted:dotted_90, dashed:dashed_91, customLine:customLine_92, filled:filled_93,
509− outlined:outlined_94, customOutline:customOutline_95, textured:textured, sprite:sprite_96, toForm:toForm_97, rotate:rotate_98, scale:scale_99, move:move_100}
514+ outlined:outlined_94, customOutline:customOutline_95, textured:textured, sprite:sprite_96, toForm:toForm_97, rotate:rotate_98, scale:scale_99, move:move_100,
515+ insideForm: Collage.insideForm}
510516}();