-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvexityDefinition.html
executable file
·490 lines (414 loc) · 17.8 KB
/
ConvexityDefinition.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<!DOCTYPE html>
<html>
<head>
<title>Definition of convexity</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
showProcessingMessages: false,
tex2jax: { inlineMath: [['$','$'],['\\(','\\)']] },
TeX: { extensions: ["color.js"] }
});
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_HTMLorMML"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-debug.js"></script>
<script type="text/javascript" src="https://d3js.org/d3.v5.min.js"></script>
<script type="text/javascript" src="https://d3js.org/d3-selection-multi.v1.min.js"></script>
<!-- <script type="text/javascript" src="./ko/knockout-3.4.2.debug.js"></script>
<script type="text/javascript" src="./d3/d3.v5.js"></script>
<script type="text/javascript" src="./d3-selection-multi/d3-selection-multi.js"></script>
<script type="text/javascript" src="./mathjax-2.7.5/MathJax.js?config=TeX-MML-AM_HTMLorMML"></script> -->
<script type="text/javascript" src="./js/mathexpressions.js"></script>
<script type="text/javascript" src="./js/space2dim.js"></script>
<script type="text/javascript" src="./js/point.js"></script>
<script type="text/javascript" src="./js/rico.js"></script>
<script type="text/javascript" src="./js/circle.js"></script>
<script type="text/javascript" src="./js/vector.js"></script>
<script type="text/javascript" src="./js/line.js"></script>
<script type="text/javascript" src="./js/region.js"></script>
<link rel="stylesheet" type="text/css" href="./css/article.css">
<link rel="stylesheet" type="text/css" href="./css/space2dim.css">
<link rel="stylesheet" type="text/css" href="./css/vector.css">
<link rel="stylesheet" type="text/css" href="./css/line.css">
<link rel="stylesheet" type="text/css" href="./css/region.css">
<style>
</style>
</head>
<body>
<div>
<h1 id="learn_convexity_linesegment">Line segment</h1>
<p>Two vector in 2-dimensional space:</p>
<p>
$$ \begin{aligned}
A &= (a_1, a_2), \text{ in }\mathbb{R}^2\\
B &= (b_1, b_2), \text{ in }\mathbb{R}^2
\end{aligned}$$
</p>
<p>Then a line segment going from A to B can be defined as:</p>
<p>
$$r = \vec{oa} + \lambda \vec{ab}$$
</p>
<p>This is simply the adition of the vector $a$ with a part of the vector going from $a$ to $b$</p>
<p>We know from the section on vector math that the vector going from $a$ to $b$ is equal to $b-a$ and thus we can write:</p>
<p>
$$\begin{aligned}
r &= \vec{OA} + \lambda \vec{AB}\\
&= \mathbf{a} + {\lambda}(\mathbf{b}-\mathbf{a}) \\
&= \mathbf{a} + {\lambda}\mathbf{b}-{\lambda}\mathbf{a} \\
&= (1-{\lambda})\mathbf{a} + {\lambda}\mathbf{b} \\
&= ((1-{\lambda})a1 + {\lambda}b1, (1-{\lambda})a2 + {\lambda}b2) \\
\end{aligned}$$
</p>
<p>A sample:</p>
<div class="sample_container_height100">
<div id="linesegment_sample">
$${}$$
</div>
</div>
</div>
<div style="width:100%">
<p>You can drag the two red dots to change the endpoints $A$ and $B$ of the lnesegment.</p>
<p>Change the value of $\lambda$ by dragging the slider:</p>
<div class="divcentered">
<input type="range" min="0" max="1" step="0.01" data-bind="value: LineSegment.lambda, valueUpdate: 'input'" />
</div>
<div id="linesegment" class="divcentered"></div>
</div>
<div>
<h1 id="learn_convexity_convex">Convex</h1>
<p>In a Euclidean space, a convex region is a region where, for every pair of points within the region, every point on the straight line segment that joins the pair of points is also within the region</p>
</div>
<div style="width:100%">
<div>You can drag the red dots to change the shape of the region. The black dots can be dragged to change the line segemnt. The slider can be dragged to change the point on the line segment. If the point on the linesegment is green, it is inside the region, else if it is red it is outside the region: then the region is not concave</div>
<div class="divcentered">
<input type="range" min="0" max="1" step="0.01" data-bind="value: Convex.lambda, valueUpdate: 'input'" />
</div>
<div id="convex" class="divcentered"></div>
</div>
<div>
<h1 id="learn_convexity_concave">Concave (not Convex)</h1>
<p>If the region is not convext (see above for the definition) then it is concave.</p>
</div>
<div style="width:100%">
<div>You can drag the red dots to change the shape of the region. The black dots can be dragged to change the line segemnt. The slider can be dragged to change the point on the line segment. If the point on the linesegment is green, it is inside the region, else if it is red it is outside the region: then the region is not concave</div>
<div class="divcentered">
<input type="range" min="0" max="1" step="0.01" data-bind="value: Concave.lambda, valueUpdate: 'input'" />
</div>
<div id="notconvex" class="divcentered"></div>
</div>
<script>
function createLineSegmentDefinition() {
let summationMathExpression = new MathExpressions();
let space2dimlinesegment = new Space2Dim("linesegment", 300, 300, -50, 50, -50, 50);
function LineSegmentViewModel() {
this.lambda = ko.observable(0.5);
this.lambda.subscribe(function (newValue) {
space2dimlinesegment.update();
summationMathExpression.QueueUpdate();
});
this.p0 = space2dimlinesegment.CreatePointAndUpdate(0, 0, function(){ summationMathExpression.QueueUpdate(); });
this.p1 = space2dimlinesegment.CreatePointAndUpdate(-40, 10, function(){ summationMathExpression.QueueUpdate(); });
this.p2 = space2dimlinesegment.CreatePointAndUpdate(40, 40, function(){ summationMathExpression.QueueUpdate(); });
this.psx = ko.computed(function () {
let x = (1 - this.lambda()) * this.p1.getX() + this.lambda() * this.p2.getX();
//console.log("x: " + x)
return x;
}, this);
this.psy = ko.computed(function () {
let y = (1 - this.lambda()) * this.p1.getY() + this.lambda() * this.p2.getY();
//console.log("y: " + y)
return y;
}, this);
this.ps = new Point2Dim({ x: this.psx, y: this.psy }, {});
let vector1 = new Vector2Dim(
{ p1: this.p0, p2: this.p1 },
{ name: "a", dvaluelvl: 1, p1draggable: 0, p2draggable: 1 }
);
let vector2 = new Vector2Dim(
{ p1: this.p0, p2: this.p2 },
{ name: "b", dvaluelvl: 2, p1draggable: 0, p2draggable: 1 }
);
let vsegment = new Vector2Dim(
{ p1: this.p0, p2: this.ps },
{ name: "v", showEndArrow: 1, dvaluelvl: 3, p1draggable: 0, p2draggable: 0 }
);
let linesegment = new Vector2Dim(
{ p1: this.p1, p2: this.ps },
{ name: "r", showEndArrow: 0, dvaluelvl: 0, p1draggable: 0, p2draggable: 0 }
);
let me = this;
let getMathExpression = function () {
let lambdaFormatted = d3.format(".1f")(me.lambda());
let a1XFormatted = d3.format(".1f")(me.p1.getX());
let a2YFormatted = d3.format(".1f")(me.p1.getY());
let b1XFormatted = d3.format(".1f")(me.p2.getX());
let b2YFormatted = d3.format(".1f")(me.p2.getY());
let r1Formatted = d3.format(".1f")(me.ps.getX());
let r2Formatted = d3.format(".1f")(me.ps.getY());
let mathExpression =
"\\begin{aligned}"
+ "\\mathbf{r} &= \\mathbf{a} + {\\lambda}(\\mathbf{b}-\\mathbf{a}) \\\\"
+ "&= (a1 + {\\lambda}(b1-a1), a2 + {\\lambda}(b2-a2)) \\\\"
+ "&= ("+a1XFormatted+" + {" + lambdaFormatted + "}("+b1XFormatted+"-"+a1XFormatted+"), "+a2YFormatted+" + {" + lambdaFormatted + "}("+b2YFormatted+"-"+a2YFormatted+")) \\\\"
+ "&= ("+r1Formatted+", "+r2Formatted+")"
+ "\\end{aligned}";
return mathExpression;
}
summationMathExpression.RegisterExpression("linesegment_sample", getMathExpression);
this.circlesToShow = [];
this.vectorsToShow = [vector1, vector2, vsegment, linesegment];
this.raysToShow = [];
this.poliesToShow = [];
}
let lnsgmnt = new LineSegmentViewModel();
space2dimlinesegment.appenGlobalAttributes(Vector2Dim.global);
space2dimlinesegment.registerHandler(Line2Dim.draw, Line2Dim.update, function () { return lnsgmnt.raysToShow; });
space2dimlinesegment.registerHandler(Vector2Dim.draw, Vector2Dim.update, function () { return lnsgmnt.vectorsToShow; });
space2dimlinesegment.registerHandler(Region2Dim.draw, Region2Dim.update, function () { return lnsgmnt.poliesToShow; });
space2dimlinesegment.registerHandler(Circle.draw, Circle.update, function () { return lnsgmnt.circlesToShow; });
space2dimlinesegment.show();
return lnsgmnt;
}
function createConvexDefinition() {
let space2dimconvex = new Space2Dim("convex", 300, 300, 0, 50, 0, 50);
function ConvexDefinitionViewModel() {
let p1 = space2dimconvex.CreatePoint(25, 15);
let p2 = space2dimconvex.CreatePoint(35, 20);
let p3 = space2dimconvex.CreatePoint(40, 25);
let p4 = space2dimconvex.CreatePoint(35, 30);
let p5 = space2dimconvex.CreatePoint(25, 35);
let p6 = space2dimconvex.CreatePoint(15, 30);
let p7 = space2dimconvex.CreatePoint(10, 25);
let p8 = space2dimconvex.CreatePoint(15, 20);
let regionPts = [p1, p2, p3, p4, p5, p6, p7, p8];
let region1 = new Region2Dim({ poly: regionPts }, { draggable: 1 });
let cnvxHandle1 = new Circle(
{ center: p1, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let cnvxHandle2 = new Circle(
{ center: p2, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let cnvxHandle3 = new Circle(
{ center: p3, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let cnvxHandle4 = new Circle(
{ center: p4, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let cnvxHandle5 = new Circle(
{ center: p5, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let cnvxHandle6 = new Circle(
{ center: p6, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let cnvxHandle7 = new Circle(
{ center: p7, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let cnvxHandle8 = new Circle(
{ center: p8, radius: ko.observable(0.5) },
{ draggable: 1 }
);
this.sp1 = space2dimconvex.CreatePoint(15, 22);
this.sp2 = space2dimconvex.CreatePoint(35, 25);
let sp1Handle = new Circle(
{ center: this.sp1, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let sp2Handle = new Circle(
{ center: this.sp2, radius: ko.observable(0.5) },
{ draggable: 1 }
);
this.lambda = ko.observable(0.5);
this.psx = ko.computed(function () {
let x = (1 - this.lambda()) * this.sp1.getX() + this.lambda() * this.sp2.getX();
//console.log("x: " + x)
return x;
}, this);
this.psy = ko.computed(function () {
let y = (1 - this.lambda()) * this.sp1.getY() + this.lambda() * this.sp2.getY();
//console.log("y: " + y)
return y;
}, this);
this.ps = new Point2Dim({ x: this.psx, y: this.psy }, {});
let psHandle = new Circle(
{ center: this.ps, radius: ko.observable(0.5) },
{ draggable: 0, id: "cnvxsegmentend" }
);
let linesegment = new Vector2Dim(
{ p1: this.sp1, p2: this.ps },
{ name: "S", showEndArrow: 0, dvaluelvl: 0, p1draggable: 0, p2draggable: 0 }
);
this.lambdaDraw = function (space2Dim, d) {
if (document.getElementById("cnvxsegmentend") == null)
return;
if (region1.Contains(segmentEndPt)) {
document.getElementById("cnvxsegmentend").style.fill = "green";
}
else {
document.getElementById("cnvxsegmentend").style.fill = "red";
}
}
this.lambdaUpdate = function (space2Dim, d) {
if (document.getElementById("cnvxsegmentend") == null)
return;
if (region1.Contains(segmentEndPt)) {
document.getElementById("cnvxsegmentend").style.fill = "green";
}
else {
document.getElementById("cnvxsegmentend").style.fill = "red";
}
}
let segmentEndPt = this.ps;
this.lambda.subscribe(function (newValue) {
space2dimconvex.update();
});
this.circlesToShow = [
sp1Handle, sp2Handle
, psHandle];
this.vectorsToShow = [linesegment];
this.raysToShow = [];
this.regionsToShow = [region1]
}
let cnvx = new ConvexDefinitionViewModel();
space2dimconvex.appenGlobalAttributes(Vector2Dim.global);
space2dimconvex.registerHandler(Region2Dim.draw, Region2Dim.update, function () { return cnvx.regionsToShow; });
space2dimconvex.registerHandler(Line2Dim.draw, Line2Dim.update, function () { return cnvx.raysToShow; });
space2dimconvex.registerHandler(Vector2Dim.draw, Vector2Dim.update, function () { return cnvx.vectorsToShow; });
space2dimconvex.registerHandler(Circle.draw, Circle.update, function () { return cnvx.circlesToShow; });
space2dimconvex.registerHandler(cnvx.lambdaDraw, cnvx.lambdaUpdate, function () { return []; });
space2dimconvex.show();
cnvx.lambdaUpdate(null, null);
//ko.applyBindings(cnvx);
return cnvx;
}
function createNotConvexDefinition() {
let space2dimconvex = new Space2Dim("notconvex", 300, 300, 0, 50, 0, 50);
function ConvexDefinitionViewModel() {
let p1 = space2dimconvex.CreatePoint(25, 30);
let p2 = space2dimconvex.CreatePoint(35, 20);
let p3 = space2dimconvex.CreatePoint(40, 25);
let p4 = space2dimconvex.CreatePoint(35, 30);
let p5 = space2dimconvex.CreatePoint(25, 35);
let p6 = space2dimconvex.CreatePoint(15, 30);
let p7 = space2dimconvex.CreatePoint(10, 25);
let p8 = space2dimconvex.CreatePoint(15, 20);
let regionPts = [p1, p2, p3, p4, p5, p6, p7, p8];
let region1 = new Region2Dim({ poly: regionPts }, { draggable: 1 });
let cnvxHandle1 = new Circle(
{ center: p1, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let cnvxHandle2 = new Circle(
{ center: p2, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let cnvxHandle3 = new Circle(
{ center: p3, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let cnvxHandle4 = new Circle(
{ center: p4, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let cnvxHandle5 = new Circle(
{ center: p5, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let cnvxHandle6 = new Circle(
{ center: p6, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let cnvxHandle7 = new Circle(
{ center: p7, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let cnvxHandle8 = new Circle(
{ center: p8, radius: ko.observable(0.5) },
{ draggable: 1 }
);
this.sp1 = space2dimconvex.CreatePoint(15, 22);
this.sp2 = space2dimconvex.CreatePoint(35, 25);
let sp1Handle = new Circle(
{ center: this.sp1, radius: ko.observable(0.5) },
{ draggable: 1 }
);
let sp2Handle = new Circle(
{ center: this.sp2, radius: ko.observable(0.5) },
{ draggable: 1 }
);
this.lambda = ko.observable(0.5);
this.psx = ko.computed(function () {
let x = (1 - this.lambda()) * this.sp1.getX() + this.lambda() * this.sp2.getX();
return x;
}, this);
this.psy = ko.computed(function () {
let y = (1 - this.lambda()) * this.sp1.getY() + this.lambda() * this.sp2.getY();
return y;
}, this);
this.ps = new Point2Dim({ x: this.psx, y: this.psy }, {});
let psHandle = new Circle(
{ center: this.ps, radius: ko.observable(0.5) },
{ draggable: 0, id: "notcnvxsegmentend" }
);
let linesegment = new Vector2Dim(
{ p1: this.sp1, p2: this.ps },
{ name: "S", showEndArrow: 0, dvaluelvl: 0, p1draggable: 0, p2draggable: 0 }
);
this.lambdaDraw = function (space2Dim, d) {
if (document.getElementById("notcnvxsegmentend") == null)
return;
if (region1.Contains(segmentEndPt)) {
document.getElementById("notcnvxsegmentend").style.fill = "green";
}
else {
document.getElementById("notcnvxsegmentend").style.fill = "red";
}
}
this.lambdaUpdate = function (space2Dim, d) {
if (document.getElementById("notcnvxsegmentend") == null)
return;
if (region1.Contains(segmentEndPt)) {
document.getElementById("notcnvxsegmentend").style.fill = "green";
}
else {
document.getElementById("notcnvxsegmentend").style.fill = "red";
}
}
let segmentEndPt = this.ps;
this.lambda.subscribe(function (newValue) {
space2dimconvex.update();
});
this.circlesToShow = [
sp1Handle, sp2Handle
, psHandle];
this.vectorsToShow = [linesegment];
this.raysToShow = [];
this.regionsToShow = [region1]
}
let cnvx = new ConvexDefinitionViewModel();
space2dimconvex.appenGlobalAttributes(Vector2Dim.global);
space2dimconvex.registerHandler(Region2Dim.draw, Region2Dim.update, function () { return cnvx.regionsToShow; });
space2dimconvex.registerHandler(Line2Dim.draw, Line2Dim.update, function () { return cnvx.raysToShow; });
space2dimconvex.registerHandler(Vector2Dim.draw, Vector2Dim.update, function () { return cnvx.vectorsToShow; });
space2dimconvex.registerHandler(Circle.draw, Circle.update, function () { return cnvx.circlesToShow; });
space2dimconvex.registerHandler(cnvx.lambdaDraw, cnvx.lambdaUpdate, function () { return []; });
space2dimconvex.show();
cnvx.lambdaUpdate(null, null);
//ko.applyBindings(cnvx);
return cnvx;
}
function ViewModel() {
this.LineSegment = createLineSegmentDefinition();
this.Convex = createConvexDefinition();
this.Concave = createNotConvexDefinition();
}
let vm = new ViewModel();
ko.applyBindings(vm);
</script>
</body>
</html>