-
Notifications
You must be signed in to change notification settings - Fork 0
/
landformPath_tools.js
182 lines (182 loc) · 6.36 KB
/
landformPath_tools.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
/*landformPath
* landformPath_tools.js
*===================================================================
* Copyright (c) 2018 Yuji SODE <yuji.sode@gmail.com>
*
* This software is released under the MIT License.
* See LICENSE or http://opensource.org/licenses/mit-license.php
*===================================================================
*/
//Tool script using "landformPath.js"
//This function returns function that returns the number of available landform contour paths and has some methods to draw imaginary landform contour paths
/*
*=== Synopsis ===
* Activating tools
* `var L=getLandform(canvas);`
* - `canvas`: a canvas element
*
*------ Methods ------
* - `L.reset();`
* method that resets parameters for available landform paths
*
* - `L.curves(x,y,radius,linesN,dN,N);`
* method that generates landform contour lines using curved paths and returns modified radii
*
* - `L.lines(x,y,radius,linesN,dN,N);`
* method that generates landform contour lines using linear paths and returns modified radii
*
* - `L.coveringCurves(radius,linesN,dN,N);`
* method that generates landform contour lines covering the available landform paths and returns modified radii
* the generated landform contour lines using curved paths
*
* - `L.coveringLines(radius,linesN,dN,N);`
* method that generates landform contour lines covering the available landform paths and returns modified radii
* the generated landform contour lines using linear paths
*
*--- Parameters for methods ---
* - `x` and `y`: values of coordinates for the center
* - `radius`: an array of radii
* - `linesN`: the total number of contour lines
* - `dN`: a span between two red lines
* - `N`: an optional number of contour lines above the top red line
* 0 is default value
*/
//============================================================================
//this function returns function that returns the number of available landform contour paths and has some methods to draw imaginary landform contour paths
function getLandform(canvas){
// - canvas: a canvas element
//returned function that returns the number of available landform paths
var F=function(){return F.R.length;};
F.X=[],F.Y=[],F.R=[];
//### method that resets parameters for available landform paths ###
F.reset=function(){
F.X=[],F.Y=[],F.R=[];
};
//### method that generates landform contour lines using curved paths and returns modified radii ###
F.curves=function(x,y,radius,linesN,dN,N){
// - x and y: values of coordinates for the center
// - radius: an array of radii
// - linesN: the total number of contour lines
// - dN: a span between two red lines
// - N: an optional number of contour lines above the top red line
// 0 is default value
var ctx=canvas.getContext('2d'),arr=radius,arr2=[],i=0,P=[],_P=[],n=0;
linesN=Math.floor(Math.abs(linesN));
dN=Math.floor(Math.abs(dN));
N=!N?0:-Math.floor(Math.abs(N));
ctx.strokeStyle="#000";
ctx.lineWidth=1.0;
ctx.beginPath();
while (i<linesN) {
//contour line to color
if(!((i+N)%dN!=0)){
arr2.push(i!=0?P:arr);
}
//when i=(linesN-1)
if(!(i<linesN-1)){
F.X.push(x);
F.Y.push(y);
_P=linesN>1?P.map(a=>a):arr.map(a=>a);
F.R.push(_P.sort((a,b)=>b-a)[0]);
}
P=i>0?ctx.landformPathCurves(x,y,P,10):ctx.landformPathCurves(x,y,arr,10);
i+=1;
}
ctx.stroke();
n=arr2.length;
i=0;
while(i<n){
ctx.strokeStyle="#f00";
ctx.lineWidth=1.0;
ctx.beginPath();
ctx.landformPathCurves(x,y,arr2[i]);
ctx.stroke();
i+=1;
}
ctx=arr2=i=_P=n=null;
return P;
};
//### method that generates landform contour lines using linear paths and returns modified radii ###
F.lines=function(x,y,radius,linesN,dN,N){
// - x and y: values of coordinates for the center
// - radius: an array of radii
// - linesN: the total number of contour lines
// - dN: a span between two red lines
// - N: an optional number of contour lines above the top red line
// 0 is default value
var ctx=canvas.getContext('2d'),arr=radius,arr2=[],i=0,P=[],_P=[],n=0;
linesN=Math.floor(Math.abs(linesN));
dN=Math.floor(Math.abs(dN));
N=!N?0:-Math.floor(Math.abs(N));
ctx.strokeStyle="#000";
ctx.lineWidth=1.0;
ctx.beginPath();
while (i<linesN) {
//contour line to color
if(!((i+N)%dN!=0)){
arr2.push(i!=0?P:arr);
}
//when i=(linesN-1)
if(!(i<linesN-1)){
F.X.push(x);
F.Y.push(y);
_P=linesN>1?P.map(a=>a):arr.map(a=>a);
F.R.push(_P.sort((a,b)=>b-a)[0]);
}
P=i>0?ctx.landformPathLines(x,y,P,10):ctx.landformPathLines(x,y,arr,10);
i+=1;
}
ctx.stroke();
n=arr2.length;
i=0;
while(i<n){
ctx.strokeStyle="#f00";
ctx.lineWidth=1.0;
ctx.beginPath();
ctx.landformPathLines(x,y,arr2[i]);
ctx.stroke();
i+=1;
}
ctx=arr2=i=_P=n=null;
return P;
};
//### method that generates landform contour lines covering the available landform paths and returns modified radii ###
//the generated landform contour lines using curved paths
F.coveringCurves=function(radius,linesN,dN,N){
// - radius: an array of radii
// given radius array must be the minimum values such as initial values
// - linesN: the total number of contour lines
// - dN: a span between two red lines
// - N: an optional number of contour lines above the top red line
// 0 is default value
//value of circle is [x,y,radius]
var ctx=canvas.getContext('2d'),arr=[],P=[],circle=ctx.coveringCircle(F.X,F.Y,F.R);
//radii are adjusted to cover other paths
arr=radius.map(a=>a+circle[2]);
F.reset();
//drawing paths
P=F.curves(circle[0],circle[1],arr,linesN,dN,N);
ctx=arr=circle=null;
return P;
};
//### method that generates landform contour lines covering the available landform paths and returns modified radii ###
//the generated landform contour lines using linear paths
F.coveringLines=function(radius,linesN,dN,N){
// - radius: an array of radii
// given radius array must be the minimum values such as initial values
// - linesN: the total number of contour lines
// - dN: a span between two red lines
// - N: an optional number of contour lines above the top red line
// 0 is default value
//value of circle is [x,y,radius]
var ctx=canvas.getContext('2d'),arr=[],P=[],circle=ctx.coveringCircle(F.X,F.Y,F.R);
//radii are adjusted to cover other paths
arr=radius.map(a=>a+circle[2]);
F.reset();
//drawing paths
P=F.lines(circle[0],circle[1],arr,linesN,dN,N);
ctx=arr=circle=null;
return P;
};
return F;
}