|
9 | 9 | // {...} "object"; [...] "array"; new Date "date"; /.../ "regexp"; Math "math"; JSON "json";
|
10 | 10 | // Number "number"; String "string"; Boolean "boolean"; new ReferenceError) "error"
|
11 | 11 |
|
12 |
| - var toType = function(obj) { |
| 12 | + let toType = function(obj) { |
13 | 13 | return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
|
14 | 14 | }
|
15 | 15 |
|
|
26 | 26 | parent.__oldaxis = undefined
|
27 | 27 | }
|
28 | 28 | if (newaxis.dot(parent.__up) === 0) return // axis and up already orthogonal
|
29 |
| - var angle = oldaxis.diff_angle(newaxis) |
| 29 | + let angle = oldaxis.diff_angle(newaxis) |
30 | 30 | if (angle > 1e-6) { // smaller angles lead to catastrophes
|
31 |
| - var rotaxis, newup |
| 31 | + let rotaxis, newup |
32 | 32 | // If axis is flipped 180 degrees, cross(oldaxis,newaxis) is <0,0,0>:
|
33 | 33 | if (Math.abs(angle-Math.PI) < 1e-6) newup = parent.__up.multiply(-1)
|
34 | 34 | else {
|
|
54 | 54 | parent.__oldup = undefined
|
55 | 55 | }
|
56 | 56 | if (newup.dot(parent.__axis) === 0) return // axis and up already orthogonal
|
57 |
| - var angle = oldup.diff_angle(newup) |
| 57 | + let angle = oldup.diff_angle(newup) |
58 | 58 | if (angle > 1e-6) { // smaller angles lead to catastrophes
|
59 |
| - var rotaxis, newaxis |
| 59 | + let rotaxis, newaxis |
60 | 60 | // If up is flipped 180 degrees, cross(oldup,newup) is <0,0,0>:
|
61 | 61 | if (Math.abs(angle-Math.PI) < 1e-6) newaxis = parent.__axis.multiply(-1)
|
62 | 62 | else {
|
|
73 | 73 | function vec(x, y, z) {
|
74 | 74 |
|
75 | 75 | if (!(this instanceof vec)) { // "this" is an instance of vec if the function was invoked as "new vec(...)"
|
76 |
| - // vec(vec) makes a copy of the vec |
77 | 76 | // Mentioning arguments in a function slows the function down.
|
78 | 77 | // In the case of Microsoft Edge (Dec. 2015), vec was 10 times slower if arguments mentioned!!
|
79 |
| - if (y === undefined) |
80 |
| - if (z === undefined) return new vec(x.x, x.y, x.z) |
| 78 | + if (y === undefined && z === undefined) { |
| 79 | + // vec(vec) makes a copy of the vec |
| 80 | + if (!(x instanceof vec)) throw new Error("vector(non-vector) is an error.") |
| 81 | + return new vec(x.x, x.y, x.z) |
| 82 | + } |
| 83 | + if (typeof x != 'number') throw new Error("In a vector, x must be a number") |
| 84 | + if (typeof y != 'number') throw new Error("In a vector, y must be a number") |
| 85 | + if (typeof z != 'number') throw new Error("In a vector, z must be a number") |
81 | 86 | return new vec(x, y, z)
|
82 | 87 | }
|
83 | 88 |
|
84 |
| - if (z === undefined || y === undefined) throw new Error("vector() requires 3 arguments: x, y, and z.") |
| 89 | + if (x === undefined || y === undefined || z === undefined) throw new Error("vector() requires 3 arguments: x, y, and z.") |
| 90 | + if (typeof x != 'number') throw new Error("In a vector, x must be a number") |
| 91 | + if (typeof y != 'number') throw new Error("In a vector, y must be a number") |
| 92 | + if (typeof z != 'number') throw new Error("In a vector, z must be a number") |
85 | 93 | this.x = x
|
86 | 94 | this.y = y
|
87 | 95 | this.z = z
|
|
151 | 159 | if (parent) {
|
152 | 160 | if (parent.__sizing) { // Not sphere or ring or text or compound
|
153 | 161 | // Be careful not to alter the attributeVectorAxis character of parent.__axis
|
154 |
| - var pa = parent.__axis |
| 162 | + let pa = parent.__axis |
155 | 163 | if (pa.x === 0 && pa.y === 0 && pa.z === 0) {
|
156 | 164 | if (parent.__oldaxis !== undefined) {
|
157 | 165 | pa = parent.__oldaxis
|
|
160 | 168 | pa = vec(1,0,0)
|
161 | 169 | }
|
162 | 170 | }
|
163 |
| - var v = pa.norm().multiply(x) |
| 171 | + let v = pa.norm().multiply(x) |
164 | 172 | parent.__axis.__x = v.x
|
165 | 173 | parent.__axis.__y = v.y
|
166 | 174 | parent.__axis.__z = v.z
|
|
172 | 180 | attributeVectorSize.prototype.constructor = attributeVectorSize
|
173 | 181 |
|
174 | 182 | function attributeVectorUp(parent, x, y, z) { // for size in VPython environment
|
175 |
| - var oldup |
| 183 | + let oldup |
176 | 184 | this.__parent = parent
|
177 | 185 | if (parent) oldup = norm(parent.__up)
|
178 | 186 | this.__x = x
|
|
278 | 286 | function () { return this.__x },
|
279 | 287 | set:
|
280 | 288 | function (value) {
|
281 |
| - var oldaxis = norm(this.__parent.__axis) |
| 289 | + let oldaxis = norm(this.__parent.__axis) |
282 | 290 | this.__x = value
|
283 | 291 | if (this.__parent.__sizing) this.__parent.__size.x = this.mag
|
284 | 292 | adjust_up(this.__parent, oldaxis, this)
|
|
292 | 300 | function () { return this.__y },
|
293 | 301 | set:
|
294 | 302 | function (value) {
|
295 |
| - var oldaxis = norm(this.__parent.__axis) |
| 303 | + let oldaxis = norm(this.__parent.__axis) |
296 | 304 | this.__y = value
|
297 | 305 | if (this.__parent.__sizing) this.__parent.__size.x = this.mag
|
298 | 306 | adjust_up(this.__parent, oldaxis, this)
|
|
306 | 314 | function () { return this.__z },
|
307 | 315 | set:
|
308 | 316 | function (value) {
|
309 |
| - var oldaxis = norm(this.__parent.__axis) |
| 317 | + let oldaxis = norm(this.__parent.__axis) |
310 | 318 | this.__z = value
|
311 | 319 | if (this.__parent.__sizing) this.__parent.__size.x = this.mag
|
312 | 320 | adjust_up(this.__parent, oldaxis, this)
|
|
325 | 333 | this.__x = value
|
326 | 334 | // Be careful not to alter the attributeVectorAxis character of this.__parent.__axis
|
327 | 335 | if (this.__parent.__sizing) {
|
328 |
| - var pa = this.__parent.__axis |
| 336 | + let pa = this.__parent.__axis |
329 | 337 | if (pa.x === 0 && pa.y === 0 && pa.z === 0) {
|
330 | 338 | if (this.__parent.__oldaxis !== undefined) {
|
331 | 339 | pa = this.__parent.__oldaxis
|
|
334 | 342 | pa = vec(1,0,0)
|
335 | 343 | }
|
336 | 344 | }
|
337 |
| - var v = pa.norm().multiply(value) |
| 345 | + let v = pa.norm().multiply(value) |
338 | 346 | this.__parent.__axis.__x = v.x
|
339 | 347 | this.__parent.__axis.__y = v.y
|
340 | 348 | this.__parent.__axis.__z = v.z
|
|
376 | 384 | function () { return this.__x },
|
377 | 385 | set:
|
378 | 386 | function (value) {
|
379 |
| - var oldup = norm(this.__parent.__up) |
| 387 | + let oldup = norm(this.__parent.__up) |
380 | 388 | this.__x = value
|
381 | 389 | adjust_axis(parent, oldup, this)
|
382 | 390 | }
|
|
389 | 397 | function () { return this.__y },
|
390 | 398 | set:
|
391 | 399 | function (value) {
|
392 |
| - var oldup = norm(this.__parent.__up) |
| 400 | + let oldup = norm(this.__parent.__up) |
393 | 401 | this.__y = value
|
394 | 402 | adjust_axis(parent, oldup, this)
|
395 | 403 | }
|
|
412 | 420 |
|
413 | 421 | vec.prototype.toString = function () {
|
414 | 422 | // Mimics the vector display of VPython
|
415 |
| - var input = [this.x, this.y, this.z] |
416 |
| - var output = [] |
417 |
| - for (var i = 0; i < 3; i++) { |
| 423 | + let input = [this.x, this.y, this.z] |
| 424 | + let output = [] |
| 425 | + for (let i = 0; i < 3; i++) { |
418 | 426 | output.push(__convert(input[i]))
|
419 | 427 | }
|
420 | 428 | return "< " + output[0] + ", " + output[1] + ", " + output[2] + " >"
|
|
454 | 462 | mag: {
|
455 | 463 | get: function () { return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z) },
|
456 | 464 | set: function (value) {
|
457 |
| - var v = this.norm().multiply(value) |
| 465 | + let v = this.norm().multiply(value) |
458 | 466 | this.x = v.x
|
459 | 467 | this.y = v.y
|
460 | 468 | this.z = v.z
|
|
463 | 471 | mag2: {
|
464 | 472 | get: function () { return this.x * this.x + this.y * this.y + this.z * this.z },
|
465 | 473 | set: function (value) {
|
466 |
| - var v = this.norm().multiply(Math.sqrt(value)) |
| 474 | + let v = this.norm().multiply(Math.sqrt(value)) |
467 | 475 | this.x = v.x
|
468 | 476 | this.y = v.y
|
469 | 477 | this.z = v.z
|
|
472 | 480 | hat: {
|
473 | 481 | get: function () { return this.norm() },
|
474 | 482 | set: function (value) {
|
475 |
| - var v = value.hat.multiply(this.mag) |
| 483 | + let v = value.hat.multiply(this.mag) |
476 | 484 | this.x = v.x
|
477 | 485 | this.y = v.y
|
478 | 486 | this.z = v.z
|
|
496 | 504 | })
|
497 | 505 |
|
498 | 506 | vec.prototype.norm = function () {
|
499 |
| - var r = this.mag |
| 507 | + let r = this.mag |
500 | 508 | if (r == 0) return new vec(0, 0, 0)
|
501 | 509 | return new vec(this.x / r, this.y / r, this.z / r)
|
502 | 510 | }
|
|
511 | 519 | }
|
512 | 520 |
|
513 | 521 | vec.prototype.proj = function (v) {
|
514 |
| - var B = norm(v) |
| 522 | + let B = norm(v) |
515 | 523 | return B.multiply(this.dot(B))
|
516 | 524 | }
|
517 | 525 |
|
|
534 | 542 | }
|
535 | 543 |
|
536 | 544 | vec.prototype.rotate = function (args) {
|
537 |
| - var angle, axis |
| 545 | + let angle, axis |
538 | 546 | // canonical form v.rotate({angle:a, axis:ax}), but can also be
|
539 | 547 | // such forms as v.rotate(a, ax) or v.rotate(ax, {origin:or}), etc.
|
540 |
| - var L = arguments.length |
| 548 | + let L = arguments.length |
541 | 549 | if (L < 1 || L > 2) throw new Error('vector.rotate takes 1 or 2 arguments')
|
542 |
| - for (var i=0; i<L; i++) { |
543 |
| - var arg = arguments[i] |
| 550 | + for (let i=0; i<L; i++) { |
| 551 | + let arg = arguments[i] |
544 | 552 | if (toType(arg) == 'object' && !(arg instanceof vec)) {
|
545 | 553 | if (arg.angle !== undefined) angle = arg.angle
|
546 | 554 | if (arg.axis !== undefined) axis = arg.axis
|
|
552 | 560 | if (angle === undefined) throw new Error("To rotate a vector you must specify an angle.")
|
553 | 561 | if (angle === 0) return new vec(this.x, this.y, this.z)
|
554 | 562 | if (axis === undefined) axis = new vec(0, 0, 1)
|
555 |
| - var axis = axis.norm() |
556 |
| - var parallel = axis.multiply(axis.dot(this)) // projection along axis |
557 |
| - var perp = axis.cross(this) |
558 |
| - var pmag = perp.mag // length of 'this' projected onto plane perpendicular to axis |
| 563 | + axis = axis.norm() |
| 564 | + let parallel = axis.multiply(axis.dot(this)) // projection along axis |
| 565 | + let perp = axis.cross(this) |
| 566 | + let pmag = perp.mag // length of 'this' projected onto plane perpendicular to axis |
559 | 567 | if (pmag === 0) return new vec(this.x, this.y, this.z)
|
560 | 568 | perp = perp.norm()
|
561 |
| - var y = perp.cross(axis) // y, perp, axis is an orthogonal coordinate system |
562 |
| - var rotated = y.multiply(pmag * Math.cos(angle)).add(perp.multiply(pmag * Math.sin(angle))) |
| 569 | + let y = perp.cross(axis) // y, perp, axis is an orthogonal coordinate system |
| 570 | + let rotated = y.multiply(pmag * Math.cos(angle)).add(perp.multiply(pmag * Math.sin(angle))) |
563 | 571 | return parallel.add(rotated)
|
564 | 572 | }
|
565 | 573 |
|
|
615 | 623 | // if (typeof a == 'number') return n * a
|
616 | 624 | // if (typeof r == 'string') return a.repeat(n)
|
617 | 625 | // if (Array.isArray(a)) { // number * array
|
618 |
| -// var na = a.length |
619 |
| -// var ret = [] |
620 |
| -// for (var i=0; i<n; i++) { |
621 |
| -// for (var j=0; j<na; j++) ret.push(a[j]) |
| 626 | +// let na = a.length |
| 627 | +// let ret = [] |
| 628 | +// for (let i=0; i<n; i++) { |
| 629 | +// for (let j=0; j<na; j++) ret.push(a[j]) |
622 | 630 | // }
|
623 | 631 | // return ret
|
624 | 632 | // }
|
|
654 | 662 | //vec.prototype['eq'] = function(r) {return (r instanceof vec) ? this.equals(r) : equal_error()}
|
655 | 663 | //vec.prototype['ne'] = function(r) {return (r instanceof vec) ? !this.equals(r) : notequal_error()}
|
656 | 664 |
|
657 |
| - var exports = { vec: vec, |
| 665 | + var exports = { vec: vec, |
658 | 666 | vector: vec,
|
659 | 667 | attributeVector: attributeVector,
|
660 | 668 | attributeVectorPos: attributeVectorPos,
|
|
669 | 677 |
|
670 | 678 | function __array_times_number(a, r) { // array * number, used by VPython programs
|
671 | 679 | if (typeof r === 'number') {
|
672 |
| - var n = r |
673 |
| - var na = a.length |
674 |
| - var ret = [] |
675 |
| - for (var i=0; i<n; i++) { |
676 |
| - for (var j=0; j<na; j++) ret.push(a[j]) |
| 680 | + let n = r |
| 681 | + let na = a.length |
| 682 | + let ret = [] |
| 683 | + for (let i=0; i<n; i++) { |
| 684 | + for (let j=0; j<na; j++) ret.push(a[j]) |
677 | 685 | }
|
678 | 686 | return ret
|
679 | 687 | }
|
|
713 | 721 | // {...} "object"; [...] "array"; new Date "date"; /.../ "regexp"; Math "math"; JSON "json";
|
714 | 722 | // Number "number"; String "string"; Boolean "boolean"; new ReferenceError) "error"
|
715 | 723 |
|
716 |
| - var toType = function(obj) { |
| 724 | + let toType = function(obj) { |
717 | 725 | return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
|
718 | 726 | }
|
719 | 727 |
|
720 |
| - var v, angle, axis |
| 728 | + let v, angle, axis |
721 | 729 | // canonical form rotate(v, {angle:a, axis:ax}), but can also be
|
722 | 730 | // such forms as rotate(v, a, ax) or rotate(v, ax, {origin:or}), etc.
|
723 |
| - var L = arguments.length |
| 731 | + let L = arguments.length |
724 | 732 | if (L < 1 || L > 3) throw new Error('rotate(vector, ...) takes 1 to 3 arguments')
|
725 | 733 | v = arguments[0]
|
726 |
| - for (var i=1; i<L; i++) { |
727 |
| - var arg = arguments[i] |
| 734 | + for (let i=1; i<L; i++) { |
| 735 | + let arg = arguments[i] |
728 | 736 | if (toType(arg) == 'object' && !(arg instanceof vec)) {
|
729 | 737 | if (arg.angle !== undefined) angle = arg.angle
|
730 | 738 | if (arg.axis !== undefined) axis = arg.axis
|
|
748 | 756 | // all VPython instances of ".replace" (in user code) with ".__GSrep".
|
749 | 757 | String.prototype.__GSrep = function(t, r, n) {
|
750 | 758 | if (n === undefined) n = 1000000
|
751 |
| - var s = this |
| 759 | + let s = this |
752 | 760 | if (t === r) return s
|
753 |
| - var tlong = t.length |
754 |
| - var i = 0 |
| 761 | + let tlong = t.length |
| 762 | + let i = 0 |
755 | 763 | while (true) {
|
756 | 764 | if (i >= n) return s
|
757 | 765 | i++
|
758 |
| - var loc = s.search(t) |
| 766 | + let loc = s.search(t) |
759 | 767 | if (loc < 0) return s
|
760 | 768 | s = s.slice(0,loc) + r + s.slice(loc+tlong)
|
761 | 769 | }
|
|
0 commit comments