forked from apcj/arrows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharcs.html
216 lines (188 loc) · 6.1 KB
/
arcs.html
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
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="../lib/d3.v3.js" charset="UTF-8"></script>
<script type="text/javascript" src="../graph-diagram.js"></script>
<link rel="stylesheet" href="../style/graph-diagram.css" type="text/css">
<link rel="stylesheet" href="../lib/bootstrap.css" type="text/css">
<title>Graph Diagram Example</title>
</head>
<body>
<figure class="graph-diagram">
<svg xmlns="http://www.w3.org/2000/svg" height="1000" width="1000" viewBox="-300 -300 1000 1000"></svg>
</figure>
<script type="text/javascript">
var padding = 10;
var start = { x: 0, y: 0, r: 50 };
var end = { x: 400, y: 0, r: 110 };
var d = end.x - start.x;
// r1 / r2 = p1 / p2
// p2 = p1 + d
// p1 = ( r1 / r2 ) ( p1 + d )
// p1 = p1 ( r1 / r2 ) + d ( r1 / r2)
// p1 (1 - r1 / r2) = d ( r1 / r2 )
// p1 = d ( r1/ r2) / ( 1 - r1 / r2 )
var radiusRatio = (start.r + padding) / (end.r + padding);
var homotheticCenter = -d * radiusRatio / (1 - radiusRatio);
var svg = d3.select( "svg" );
var angles = [ 22.5, 45, 67.5, -22.5, -45, -67.5 ].map( function ( degrees )
{
return degrees * Math.PI / 180;
} );
var curves = angles.map( function ( nodeAngle )
{
function square( l )
{
return l * l;
}
var startAttach = { x: Math.cos( nodeAngle ) * (start.r + padding), y: Math.sin( nodeAngle ) * (start.r + padding) };
var gradient = startAttach.y / (startAttach.x - homotheticCenter);
var hc = startAttach.y - gradient * startAttach.x;
var p = end.x;
var A = 1 + square(gradient);
var B = 2 * (gradient * hc - p);
var C = square(hc) + square(p) - square(end.r + padding);
var endAttach = { x: (-B - Math.sqrt( square( B ) - 4 * A * C )) / (2 * A) };
// var endAttach = { x: (p - (gradient * hc) - Math.sqrt(square(end.r + padding) * (1 + square(gradient)) -
// (square(gradient * p + hc))) ) / (1 + square(gradient)) + homotheticCenter };
// var endAttach = { x: (d - Math.sqrt(square(end.r + padding) * (1 + square(gradient)) -
// (square(gradient) * square(end.x - homotheticCenter))) ) / (1 + square(gradient)) };
endAttach.y = (endAttach.x - homotheticCenter) * gradient;
var
g1 = -startAttach.x / startAttach.y,
c1 = startAttach.y + (square( startAttach.x ) / startAttach.y),
g2 = -(endAttach.x - end.x) / endAttach.y,
c2 = endAttach.y + (endAttach.x - end.x) * endAttach.x / endAttach.y;
var cx = ( c1 - c2 ) / (g2 - g1);
var cy = g1 * cx + c1;
var arcRadius = Math.sqrt(square(cx - startAttach.x) + square(cy - startAttach.y));
return {
gradient: gradient,
hc: hc,
g1: g1,
c1: c1,
g2: g2,
c2: c2,
cx: cx,
cy: cy,
arcRadius: arcRadius,
path: [
"M", startAttach.x, startAttach.y,
"A", arcRadius, arcRadius, 0, 0, nodeAngle > 0 ? 0 : 1, endAttach.x, endAttach.y
].join( " " )
};
} );
svg
.selectAll( "circle.node" ).data( [start, end] ).enter()
.append( "circle" )
.attr("class", "node")
.attr( "fill", "none" )
.attr( "stroke", "black" )
.attr( "stroke-width", 8 )
.attr( "cx", function ( d )
{
return d.x
} )
.attr( "cy", function ( d )
{
return d.y
} )
.attr( "r", function ( d )
{
return d.r
} );
svg
.selectAll( "circle.margin" ).data( [start, end] ).enter()
.append( "circle" )
.attr("class", "margin")
.attr( "fill", "none" )
.attr( "stroke", "blue" )
.attr( "stroke-width", 1 )
.attr( "cx", function ( d )
{
return d.x
} )
.attr( "cy", function ( d )
{
return d.y
} )
.attr( "r", function ( d )
{
return d.r + padding
} );
svg
.selectAll( "path.arrow" ).data( curves ).enter()
.append( "path" )
.attr( "class", "arrow" )
.attr( "stroke", "black" )
.attr( "stroke-width", 8 )
.attr( "fill", "none" )
.attr( "d", function ( curve )
{
return curve.path;
} );
svg
.selectAll( "path.homothetic" ).data( curves ).enter()
.append( "path" )
.attr( "class", "homothetic" )
.attr( "stroke", "blue" )
.attr( "stroke-width", 1 )
.attr( "fill", "none" )
.attr( "d", function ( curve )
{
var xMin = parseInt( svg.attr( "viewBox" ).split( " " )[0] );
var xMax = xMin + parseInt( svg.attr( "viewBox" ).split( " " )[2] );
return ["M", xMin, xMin * curve.gradient + curve.hc, "L", xMax, xMax * curve.gradient + curve.hc].join( " " );
} );
svg
.selectAll( "path.tangent-start" ).data( curves ).enter()
.append( "path" )
.attr( "class", "tangent-start" )
.attr( "stroke", "blue" )
.attr( "stroke-width", 1 )
.attr( "fill", "none" )
.attr( "d", function ( curve )
{
var xLength = 800;
return [
"M", 0, curve.c1,
"L", xLength, xLength * curve.g1 + curve.c1
].join( " " );
} );
svg
.selectAll( "path.tangent-end" ).data( curves ).enter()
.append( "path" )
.attr( "class", "tangent-end" )
.attr( "stroke", "blue" )
.attr( "stroke-width", 1 )
.attr( "fill", "none" )
.attr( "d", function ( curve )
{
var xLength = 800;
return [
"M", 0, curve.c2,
"L", xLength, xLength * curve.g2 + curve.c2
].join( " " );
} );
svg
.selectAll( "circle.arc" ).data( curves ).enter()
.append( "circle" )
.attr("class", "arc")
.attr("stroke", "red")
.attr("stroke-width", 1)
.attr("fill", "none")
.attr("cx", function(curve) { return curve.cx; })
.attr("cy", function(curve) { return curve.cy; })
.attr("r", function(curve) { return curve.arcRadius; });
svg
.append( "path" )
.attr( "stroke", "black" )
.attr( "stroke-width", 8 )
.attr( "fill", "none" )
.attr( "d", [
"M", start.x + start.r + padding, start.y,
"L", end.x - end.r - padding, end.y
].join( " " ) );
</script>
</body>
</html>