-
Notifications
You must be signed in to change notification settings - Fork 19
/
IntersectionUtilities.js
124 lines (103 loc) · 3.06 KB
/
IntersectionUtilities.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
/*****
*
* IntersectionUtilities.js
*
* copyright 2002, Kevin Lindsey
*
*****/
/*****
*
* globals
*
*****/
var svgns = "http://www.w3.org/2000/svg";
var azap, mouser;
var points = new Array();
var shapes = new Array();
var info;
/*****
*
* init
*
*****/
function init(e) {
if ( window.svgDocument == null )
svgDocument = e.target.ownerDocument;
azap = new AntiZoomAndPan();
mouser = new Mouser();
var infoElem = svgDocument.getElementById("info");
var background = svgDocument.getElementById("background");
info = infoElem.firstChild;
azap.appendNode(infoElem);
azap.appendNode(mouser.svgNode);
azap.appendNode(background);
loadShapes();
showIntersections();
}
/*****
*
* loadShapes
*
*****/
function loadShapes() {
var children = svgDocument.documentElement.childNodes;
for ( var i = 0; i < children.length; i++ ) {
var child = children.item(i);
if ( child.nodeType == 1 ) {
// found element node
var edit = child.getAttributeNS(
"http://www.kevlindev.com/gui",
"edit"
);
if ( edit != null && edit != "" ) {
// ignore value for now
var shape;
switch ( child.localName ) {
case "circle": shape = new Circle(child); break;
case "ellipse": shape = new Ellipse(child); break;
case "line": shape = new Line(child); break;
case "path": shape = new Path(child); break;
case "polygon": shape = new Polygon(child); break;
case "rect": shape = new Rectangle(child); break;
default:
// do nothing for now
}
if ( shape != null ) {
shape.realize();
shape.callback = showIntersections;
shapes.push(shape);
}
}
}
}
}
/*****
*
* showIntersections
*
*****/
function showIntersections() {
if ( shapes.length >= 2 ) {
var inter = Intersection.intersectShapes( shapes[0], shapes[1] );
info.data = inter.status;
for ( var i = 0; i < inter.points.length; i++ ) {
var coord = inter.points[i];
if ( i >= points.length ) {
var point = svgDocument.createElementNS(svgns, "use");
point.setAttributeNS(
"http://www.w3.org/1999/xlink",
"href",
"#intersection"
);
svgDocument.documentElement.appendChild(point);
points.push(point);
}
points[i].setAttributeNS(null, "x", coord.x);
points[i].setAttributeNS(null, "y", coord.y);
points[i].setAttributeNS(null, "display", "inline");
}
for ( var i = inter.points.length; i < points.length; i++ ) {
points[i].setAttributeNS(null, "display", "none");
}
}
}