-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharc.js-e
155 lines (144 loc) · 4.02 KB
/
arc.js-e
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
## Copyright (c) 2013, Empirical Modelling Group
## All rights reserved.
##
## See LICENSE.txt
${{
Arc = function(x, y, r, sAngle, eAngle, colour, drawingOptions) {
this.x = x;
this.y = y;
this.r = r;
this.sAngle = sAngle;
this.eAngle = eAngle;
var radiansPerUnit = Math.PI / root.lookup("semicircleAngle").value();
var sRadians = sAngle * radiansPerUnit;
var eRadians = eAngle * radiansPerUnit;
var twoPI = 2 * Math.PI;
sRadians = sRadians % twoPI;
if (sRadians < 0) {
sRadians = sRadians + twoPI;
}
eRadians = eRadians % twoPI;
if (eRadians < 0) {
eRadians = eRadians + twoPI;
}
this.sRadians = sRadians;
this.eRadians = eRadians;
this.colour = colour
this.drawingOptions = drawingOptions;
}
}}$;
#! Draw an Arc. See [drawing options](@external CanvasExamples > drawingoptions) to control appearance.
#
# ##Usage Example
# `Arc(x, y, radius, startAngle, endAngle);`
# `Arc(x, y, radius, startAngle, endAngle, "red");`
#
# @param x X position
# @param y Y position
# @param r Radius
# @param sAngle Start angle in degrees
# @param eAngle End angle in degrees
# @param [colour] Default is black
# @param [drawingOptions] Drawing Options object
#
# #canvas #arc #drawingOptions
func Arc {
${{
var x = arguments[0];
var y = arguments[1];
var r = arguments[2];
var sAngle = arguments[3];
var eAngle = arguments[4];
var colour, drawingOptions;
if (arguments.length == 6 && typeof(arguments[5]) == "object" && !(arguments[5] instanceof EdenUI.plugins.Canvas2D.FillStyle)) {
colour = "black";
drawingOptions = arguments[5];
} else {
colour = arguments[5];
drawingOptions = arguments[6];
if (colour === undefined) { colour = "black"; }
}
return new Arc(x, y, r, sAngle, eAngle, colour, drawingOptions);
}}$; };
semicircleAngle ~> [Arc];
${{
Arc.prototype.draw = function(context, scale) {
var anticlockwise;
if (this.drawingOptions !== undefined && "direction" in this.drawingOptions) {
var directionName = this.drawingOptions.direction;
var turnedThroughAC = this.eRadians - this.sRadians;
if (this.eRadians < this.sRadians) {
turnedThroughAC = turnedThroughAC + 2 * Math.PI;
}
switch (directionName) {
case "anticlockwise":
case "acw":
case "ccw":
anticlockwise = true;
break;
case "auto":
anticlockwise = this.sAngle < this.eAngle;
break;
case "clockwise":
case "cw":
anticlockwise = false;
break;
case "major":
anticlockwise = turnedThroughAC > Math.PI;
break;
case "minor":
anticlockwise = turnedThroughAC <= Math.PI;
break;
default:
//Invalid value specified.
anticlockwise = true;
}
} else {
//Default to "auto"
anticlockwise = this.sAngle < this.eAngle;
}
var sRadians = this.sRadians;
var eRadians = this.eRadians;
if (scale < 0) {
sRadians = -sRadians;
eRadians = -eRadians;
anticlockwise = !anticlockwise;
}
var r = this.r - context.lineWidth / 2;
context.beginPath();
context.arc(this.x, this.y, r, -sRadians, -eRadians, anticlockwise);
context.strokeStyle = this.colour;
context.stroke();
if (this.drawingOptions !== undefined && this.drawingOptions.arrowhead instanceof Arrowhead) {
var cos1 = Math.cos(-sRadians);
var sin1 = Math.sin(-sRadians);
var cos2 = Math.cos(-eRadians);
var sin2 = Math.sin(-eRadians);
var x1 = this.x + r * cos1;
var y1 = this.y + r * sin1;
var x2 = this.x + r * cos2;
var y2 = this.y + r * sin2;
var gradient1 = -cos1 / sin1;
var gradient2 = -cos2 / sin2;
var reverse1, reverse2;
if (anticlockwise) {
reverse1 = sRadians <= Math.PI;
reverse2 = eRadians <= Math.PI;
} else {
reverse1 = sRadians > Math.PI;
reverse2 = eRadians > Math.PI;
}
this.drawingOptions.arrowhead.draw(context, scale, x1, y1, gradient1, reverse1, x2, y2,
gradient2, reverse2);
}
};
Arc.prototype.toString = function() {
var s = "Arc(" + Eden.edenCodeForValues(this.x, this.y, this.r, this.sAngle, this.eAngle, this.colour);
if (this.drawingOptions !== undefined) {
s = s + ", " + Eden.edenCodeForValue(this.drawingOptions);
}
s = s + ")";
return s;
}
Arc.prototype.getEdenCode = Arc.prototype.toString;
}}$;