-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostitDoodle.js
More file actions
216 lines (193 loc) · 4.81 KB
/
postitDoodle.js
File metadata and controls
216 lines (193 loc) · 4.81 KB
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
let w = 800;
let h = 800;
let X_MARGIN = w/10;
let Y_MARGIN = h/10;
let NUM_POINTS = 50;
let MAX_NUM_CURVES = 1;
let pArr;
let count = 0;
let POSTIT_YELLOW;
let a, b;
function setup() {
POSTIT_YELLOW = color(240,240,0);
createCanvas(w, h);
background(POSTIT_YELLOW);
//pArr = randomPoints(NUM_POINTS);
pArr = pyramidPoints(NUM_POINTS);
pArr = pArr.sort(sortByY);
frameRate(25);
//treeScan(pArr);
a = {
x: w/2,
y: h/2,
};
b = randomPoint();
DrawSquigglies();
}
function draw(){
b = {x: mouseX, y: mouseY};
// background(240, 240, 0);
// DrawSquigglies();
//treeScan(pArr);
//drawPoint(pArr[count]);
//drawCurve(pArr[count],pArr[(count+1)%pArr.length]);
//count++;
}
function mouseClicked(){
}
function placePoint(){
}
function DrawSquigglies(){
// drawCurve(a,b)
drawCurvesBetween(a,b);
drawPoint(a);
drawPoint(b);
// drawPerpdendicularBisector(a,b)
}
function treeScan(points){
let root = points[0];
drawPoint(root);
let childArr = points.slice(1);
if(childArr.length > 0){
childArr = childArr.sort(sortByX);
let l_sub = childArr.slice(0,childArr.length/2).sort(sortByY);
let r_sub = childArr.slice(childArr.length/2).sort(sortByY);
if(l_sub.length > 0){ drawCurve(root,l_sub[0]); treeScan(l_sub); }
if(r_sub.length > 0){ drawCurve(root,r_sub[0]); treeScan(r_sub); }
}
}
function sortByX(p1,p2){
return p1.x > p2.x ? 1 : -1;
}
function sortByY(p1,p2){
return p1.y > p2.y ? 1 : -1;
}
function linearScan(pArr){
for(let i = 0; i < pArr.length; i++)
{
drawPoint(pArr[i]);
if(i + 1 < pArr.length){
drawCurve(pArr[i],pArr[i+1]);
}
}
}
function randomControlPoint(point){
let variance = 10;
let result =
{
x: point.x + point.x*random(variance) * random(-1,1),
y: point.y + point.y*random(variance)*random(-1,1),
};
return result;
}
function drawCurvesBetween(a,b){
noFill();
//stroke(color(random(255),random(255),random(255)));
stroke(0,0,60);
//strokeWeight(random(0.1,10));
strokeWeight(5);
let numCurves = 2;
let intPoints = [];
intPoints.push(a);
intPoints.push(b);
let randDisp = random(50);
for(let i = 0; i < numCurves; i++){
let interiorPoint = pointOnParallelLine(intPoints[i], intPoints[intPoints.length - 1], randDisp * pow(-1,i));
drawPoint(interiorPoint);
intPoints.splice(i+1,0,interiorPoint);
}
for(let i = 0; i < intPoints.length - 1; i++){
drawCurve(intPoints[i], intPoints[i+1]);
drawPoint(intPoints[i])
}
}
function randomPointBetween(a,b){
return {x: random(a.x,b.x), y: random(a.y,b.y)};
}
function drawCurve(a,b){
noFill();
//stroke(color(random(255),random(255),random(255)));
stroke(0,0,60);
//strokeWeight(random(0.1,10));
strokeWeight(5);
let popb = pointOnPerpendicularBisector(a,b);
let popl = pointOnParallelLine(a,b,50);
let ctrlA = popl;
let ctrlB = popl;
bezier(
a.x,a.y,
ctrlA.x,ctrlA.y,
ctrlB.x,ctrlB.y,
b.x,b.y
);
}
function pointOnParallelLine(a,b,displ){
let midPoint = {x: (a.x + b.x)/2, y: (a.y + b.y)/2};
let slope = (a.y - b.y)/(a.x - b.x);
let intercept = midPoint.y - midPoint.x * slope;
let randDisplacement = random(-100,100);
let res_x = midPoint.x + displ;
let res_y = res_x * slope + intercept + displ;
return {x: res_x, y: res_y };
}
function pointOnPerpendicularBisector(a,b){
let midPoint = {x: (a.x + b.x)/2, y: (a.y + b.y)/2};
let slope = -(a.y - b.y)/(a.x - b.x);
let intercept = midPoint.y - midPoint.x * slope;
let res_x = midPoint.x + 20;
let res_y = res_x * slope + intercept;
return {x: res_x, y: res_y };
}
function drawPerpdendicularBisector(a,b){
let midPoint = {x: (a.x + b.x)/2, y: (a.y + b.y)/2};
ellipse(midPoint.x,midPoint.y,10,10)
let slope = -(a.y - b.y)/(a.x - b.x);
let intercept = midPoint.y - midPoint.x * slope;
let res_x = midPoint.x + 50;
let res_y = res_x * slope + intercept;
strokeWeight(1)
stroke(200,0,0)
line(midPoint.x,midPoint.y,res_x,res_y)
}
function drawPoint(point) {
ellipseMode(CENTER);
stroke(10);
fill(10);
// noFill();
// strokeWeight(5);
ellipse(point.x,point.y,20,20);
fill(POSTIT_YELLOW);
ellipse(point.x,point.y,18,18);
}
function pyramidPoints(number){
result = [];
let numLevels = floor(Math.log2(number));
for(let i = 0; i < numLevels; i++){
for(let j = 0; j < pow(2,i); j++){
result.push( {
x: w/2+ random(-1,1)*random(w/2),
y: h/numLevels*i + random(h/numLevels),
});
}
}
// console.log(result.length);
return result;
}
function randomPoints(number){
result = [];
for(let i = 0; i < number; i++){
result[i] = randomPoint();
}
return result;
}
function randomPoint() {
let point = {
x:
random(0 + X_MARGIN, w - X_MARGIN),
//randomGaussian(w/2,w/8),
y:
random(0 + Y_MARGIN, h - Y_MARGIN),
//randomGaussian(h/2,h/8),
};
return point ;
}