-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindyPath.java
190 lines (126 loc) · 5.03 KB
/
WindyPath.java
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
package SwansonLibrary;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import java.util.Random;
/**
* Created by Brandon on 4/29/14.
*/
public class WindyPath extends Path{
////CONTROL VARIABLES
private int mLineNumber;
/////////////////////
///DEFAULT CONTROL VARIABLES
protected int mLineNumberDEFAULT=100;
protected int mMarginDEFAULT=30;
protected float mMinLengthPercentageDEFAULT=.17F;
///FIXED CONTROL VARIABLES
static public float MINIMUM_ANGLE_DEFAULT = .23f; //as a percent of 180 .25 = 45degrees = 90 degrees total
///DATA STRUCTURE
protected Point mLinePoints[];
protected PointGenerator mPointGenerator;
private Rect mBounds;
/////UTILITY
private Random randomGen = new Random();
//////////////CONSTRUCTOR
public WindyPath(int Width, int Height) {
inflateData(mLineNumberDEFAULT,mMarginDEFAULT,Width,Height,mMinLengthPercentageDEFAULT,MINIMUM_ANGLE_DEFAULT);
generate();
}
public WindyPath(Point size){
inflateData(mLineNumberDEFAULT,mMarginDEFAULT,size.x,size.y,mMinLengthPercentageDEFAULT,MINIMUM_ANGLE_DEFAULT);
generate();
}
public WindyPath(int Width, int Height, int LineNumber, float MinLengthPercentage, int Margin, float minAngle) {
inflateData(LineNumber,Margin,Width,Height,MinLengthPercentage,minAngle);
generate();
}
public WindyPath(int Width, int Height,int LineNumber ,float MinLengthPercentage, float minAngle,Rect bounds) {
inflateData(LineNumber,MinLengthPercentage,minAngle,bounds);
generate();
}
public WindyPath(Rect bounds, int LineNumber) {
inflateData(LineNumber,mMinLengthPercentageDEFAULT,MINIMUM_ANGLE_DEFAULT,bounds);
generate();
}
protected WindyPath() {
}
protected void inflateData(int LineNumber, int Margin, int Width, int Height, float MinLengthPercentage, float MinAngle){
Rect bounds = new Rect(0,0,Width,Height);
bounds.inset(Margin,Margin);
inflateData(LineNumber, MinLengthPercentage, MinAngle, bounds);
}
protected void inflateData(int LineNumber,float MinLengthPercentage, float MinAngle, Rect bounds) {
mBounds = bounds;
mLineNumber=LineNumber;
int MaxLength = (int) ViewTools.getHypotenuse(bounds.width(),bounds.height());
int MinLength = (int) (MaxLength*MinLengthPercentage);
if(MinAngle>MINIMUM_ANGLE_DEFAULT)MinAngle=MINIMUM_ANGLE_DEFAULT;
mPointGenerator = new PointGenerator(MinLength,bounds,MinAngle);
//put PointGenerator modifiers here
modifyPointGenerator(mPointGenerator);
}
private void modifyPointGenerator(PointGenerator pointGenerator){
}
public void generate(){
rewind();
mLinePoints = new Point[mLineNumber+1];
mLinePoints[0]=ViewTools.getRandomPoint(mBounds);
mLinePoints[1]=mPointGenerator.makeMap(mLinePoints[0],mLinePoints[0]).getPoint();
finishGenerate();
}
public void generate(Point startPoint, int LineNumber){
rewind();
mLineNumber=LineNumber;
mLinePoints = new Point[mLineNumber+1];
mLinePoints[0]=startPoint;
mLinePoints[1]=mPointGenerator.makeMap(mLinePoints[0],mLinePoints[0]).getPoint();
finishGenerate();
}
public void generate(ViewTools.Line startLine, int LineNumber){
rewind();
mLineNumber=LineNumber;
mLinePoints = new Point[mLineNumber+1];
mLinePoints[0]=startLine.start;
mLinePoints[1]=startLine.end;
finishGenerate();
}
public void generate(boolean Continue){
if(Continue&&mLinePoints!=null){
generate(new ViewTools.Line(mLinePoints[mLinePoints.length-2],mLinePoints[mLinePoints.length-1]), mLineNumber);
}else generate();
}
public void generate(boolean Continue, int LineNumber){
mLineNumber=LineNumber;
generate(Continue);
}
private void finishGenerate(){
for(int x=2;x<mLineNumber+1;x++){
mLinePoints[x]=mPointGenerator.makeMap(mLinePoints[x-2],mLinePoints[x-1]).getPoint();
}
makeCurves();
}
protected void makeCurves() {
Point midpoints[] = new Point[mLineNumber];
for(int x=0;x<mLineNumber;x++)midpoints[x]=getMidPoint(mLinePoints[x], mLinePoints[x+1]);
moveTo(midpoints[0].x, midpoints[0].y);
for(int i=1;i<mLineNumber;i++){
quadTo(mLinePoints[i].x, mLinePoints[i].y, midpoints[i].x, midpoints[i].y);
}
}
////////////UTILITY FUCTIONS
private Point getMidPoint(Point start, Point end){
int dx = end.x - start.x;
int dy = end.y - start.y;
int orginalLineMidX = start.x + Math.round(.5f * dx);
int orginalLineMidY = start.y + Math.round(.5f * dy);
return new Point(orginalLineMidX,orginalLineMidY);
}
///FOR DEBUG
public Point[] getLinePoints(){
return mLinePoints;
}
public int getLineNumber() {
return mLineNumber;
}
}