Skip to content

Commit de24fce

Browse files
committed
Catch vector creation bugs
1 parent e384990 commit de24fce

File tree

4 files changed

+66
-58
lines changed

4 files changed

+66
-58
lines changed

GlowScriptOffline/glowscript_libraries/glow.3.2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GlowScriptOffline3.2.zip

97 Bytes
Binary file not shown.

lib/glow/vectors.js

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// {...} "object"; [...] "array"; new Date "date"; /.../ "regexp"; Math "math"; JSON "json";
1010
// Number "number"; String "string"; Boolean "boolean"; new ReferenceError) "error"
1111

12-
var toType = function(obj) {
12+
let toType = function(obj) {
1313
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
1414
}
1515

@@ -26,9 +26,9 @@
2626
parent.__oldaxis = undefined
2727
}
2828
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)
3030
if (angle > 1e-6) { // smaller angles lead to catastrophes
31-
var rotaxis, newup
31+
let rotaxis, newup
3232
// If axis is flipped 180 degrees, cross(oldaxis,newaxis) is <0,0,0>:
3333
if (Math.abs(angle-Math.PI) < 1e-6) newup = parent.__up.multiply(-1)
3434
else {
@@ -54,9 +54,9 @@
5454
parent.__oldup = undefined
5555
}
5656
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)
5858
if (angle > 1e-6) { // smaller angles lead to catastrophes
59-
var rotaxis, newaxis
59+
let rotaxis, newaxis
6060
// If up is flipped 180 degrees, cross(oldup,newup) is <0,0,0>:
6161
if (Math.abs(angle-Math.PI) < 1e-6) newaxis = parent.__axis.multiply(-1)
6262
else {
@@ -73,15 +73,23 @@
7373
function vec(x, y, z) {
7474

7575
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
7776
// Mentioning arguments in a function slows the function down.
7877
// 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")
8186
return new vec(x, y, z)
8287
}
8388

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")
8593
this.x = x
8694
this.y = y
8795
this.z = z
@@ -151,7 +159,7 @@
151159
if (parent) {
152160
if (parent.__sizing) { // Not sphere or ring or text or compound
153161
// Be careful not to alter the attributeVectorAxis character of parent.__axis
154-
var pa = parent.__axis
162+
let pa = parent.__axis
155163
if (pa.x === 0 && pa.y === 0 && pa.z === 0) {
156164
if (parent.__oldaxis !== undefined) {
157165
pa = parent.__oldaxis
@@ -160,7 +168,7 @@
160168
pa = vec(1,0,0)
161169
}
162170
}
163-
var v = pa.norm().multiply(x)
171+
let v = pa.norm().multiply(x)
164172
parent.__axis.__x = v.x
165173
parent.__axis.__y = v.y
166174
parent.__axis.__z = v.z
@@ -172,7 +180,7 @@
172180
attributeVectorSize.prototype.constructor = attributeVectorSize
173181

174182
function attributeVectorUp(parent, x, y, z) { // for size in VPython environment
175-
var oldup
183+
let oldup
176184
this.__parent = parent
177185
if (parent) oldup = norm(parent.__up)
178186
this.__x = x
@@ -278,7 +286,7 @@
278286
function () { return this.__x },
279287
set:
280288
function (value) {
281-
var oldaxis = norm(this.__parent.__axis)
289+
let oldaxis = norm(this.__parent.__axis)
282290
this.__x = value
283291
if (this.__parent.__sizing) this.__parent.__size.x = this.mag
284292
adjust_up(this.__parent, oldaxis, this)
@@ -292,7 +300,7 @@
292300
function () { return this.__y },
293301
set:
294302
function (value) {
295-
var oldaxis = norm(this.__parent.__axis)
303+
let oldaxis = norm(this.__parent.__axis)
296304
this.__y = value
297305
if (this.__parent.__sizing) this.__parent.__size.x = this.mag
298306
adjust_up(this.__parent, oldaxis, this)
@@ -306,7 +314,7 @@
306314
function () { return this.__z },
307315
set:
308316
function (value) {
309-
var oldaxis = norm(this.__parent.__axis)
317+
let oldaxis = norm(this.__parent.__axis)
310318
this.__z = value
311319
if (this.__parent.__sizing) this.__parent.__size.x = this.mag
312320
adjust_up(this.__parent, oldaxis, this)
@@ -325,7 +333,7 @@
325333
this.__x = value
326334
// Be careful not to alter the attributeVectorAxis character of this.__parent.__axis
327335
if (this.__parent.__sizing) {
328-
var pa = this.__parent.__axis
336+
let pa = this.__parent.__axis
329337
if (pa.x === 0 && pa.y === 0 && pa.z === 0) {
330338
if (this.__parent.__oldaxis !== undefined) {
331339
pa = this.__parent.__oldaxis
@@ -334,7 +342,7 @@
334342
pa = vec(1,0,0)
335343
}
336344
}
337-
var v = pa.norm().multiply(value)
345+
let v = pa.norm().multiply(value)
338346
this.__parent.__axis.__x = v.x
339347
this.__parent.__axis.__y = v.y
340348
this.__parent.__axis.__z = v.z
@@ -376,7 +384,7 @@
376384
function () { return this.__x },
377385
set:
378386
function (value) {
379-
var oldup = norm(this.__parent.__up)
387+
let oldup = norm(this.__parent.__up)
380388
this.__x = value
381389
adjust_axis(parent, oldup, this)
382390
}
@@ -389,7 +397,7 @@
389397
function () { return this.__y },
390398
set:
391399
function (value) {
392-
var oldup = norm(this.__parent.__up)
400+
let oldup = norm(this.__parent.__up)
393401
this.__y = value
394402
adjust_axis(parent, oldup, this)
395403
}
@@ -412,9 +420,9 @@
412420

413421
vec.prototype.toString = function () {
414422
// 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++) {
418426
output.push(__convert(input[i]))
419427
}
420428
return "< " + output[0] + ", " + output[1] + ", " + output[2] + " >"
@@ -454,7 +462,7 @@
454462
mag: {
455463
get: function () { return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z) },
456464
set: function (value) {
457-
var v = this.norm().multiply(value)
465+
let v = this.norm().multiply(value)
458466
this.x = v.x
459467
this.y = v.y
460468
this.z = v.z
@@ -463,7 +471,7 @@
463471
mag2: {
464472
get: function () { return this.x * this.x + this.y * this.y + this.z * this.z },
465473
set: function (value) {
466-
var v = this.norm().multiply(Math.sqrt(value))
474+
let v = this.norm().multiply(Math.sqrt(value))
467475
this.x = v.x
468476
this.y = v.y
469477
this.z = v.z
@@ -472,7 +480,7 @@
472480
hat: {
473481
get: function () { return this.norm() },
474482
set: function (value) {
475-
var v = value.hat.multiply(this.mag)
483+
let v = value.hat.multiply(this.mag)
476484
this.x = v.x
477485
this.y = v.y
478486
this.z = v.z
@@ -496,7 +504,7 @@
496504
})
497505

498506
vec.prototype.norm = function () {
499-
var r = this.mag
507+
let r = this.mag
500508
if (r == 0) return new vec(0, 0, 0)
501509
return new vec(this.x / r, this.y / r, this.z / r)
502510
}
@@ -511,7 +519,7 @@
511519
}
512520

513521
vec.prototype.proj = function (v) {
514-
var B = norm(v)
522+
let B = norm(v)
515523
return B.multiply(this.dot(B))
516524
}
517525

@@ -534,13 +542,13 @@
534542
}
535543

536544
vec.prototype.rotate = function (args) {
537-
var angle, axis
545+
let angle, axis
538546
// canonical form v.rotate({angle:a, axis:ax}), but can also be
539547
// such forms as v.rotate(a, ax) or v.rotate(ax, {origin:or}), etc.
540-
var L = arguments.length
548+
let L = arguments.length
541549
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]
544552
if (toType(arg) == 'object' && !(arg instanceof vec)) {
545553
if (arg.angle !== undefined) angle = arg.angle
546554
if (arg.axis !== undefined) axis = arg.axis
@@ -552,14 +560,14 @@
552560
if (angle === undefined) throw new Error("To rotate a vector you must specify an angle.")
553561
if (angle === 0) return new vec(this.x, this.y, this.z)
554562
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
559567
if (pmag === 0) return new vec(this.x, this.y, this.z)
560568
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)))
563571
return parallel.add(rotated)
564572
}
565573

@@ -615,10 +623,10 @@
615623
// if (typeof a == 'number') return n * a
616624
// if (typeof r == 'string') return a.repeat(n)
617625
// 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])
622630
// }
623631
// return ret
624632
// }
@@ -654,7 +662,7 @@
654662
//vec.prototype['eq'] = function(r) {return (r instanceof vec) ? this.equals(r) : equal_error()}
655663
//vec.prototype['ne'] = function(r) {return (r instanceof vec) ? !this.equals(r) : notequal_error()}
656664

657-
var exports = { vec: vec,
665+
var exports = { vec: vec,
658666
vector: vec,
659667
attributeVector: attributeVector,
660668
attributeVectorPos: attributeVectorPos,
@@ -669,11 +677,11 @@
669677

670678
function __array_times_number(a, r) { // array * number, used by VPython programs
671679
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])
677685
}
678686
return ret
679687
}
@@ -713,18 +721,18 @@
713721
// {...} "object"; [...] "array"; new Date "date"; /.../ "regexp"; Math "math"; JSON "json";
714722
// Number "number"; String "string"; Boolean "boolean"; new ReferenceError) "error"
715723

716-
var toType = function(obj) {
724+
let toType = function(obj) {
717725
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
718726
}
719727

720-
var v, angle, axis
728+
let v, angle, axis
721729
// canonical form rotate(v, {angle:a, axis:ax}), but can also be
722730
// such forms as rotate(v, a, ax) or rotate(v, ax, {origin:or}), etc.
723-
var L = arguments.length
731+
let L = arguments.length
724732
if (L < 1 || L > 3) throw new Error('rotate(vector, ...) takes 1 to 3 arguments')
725733
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]
728736
if (toType(arg) == 'object' && !(arg instanceof vec)) {
729737
if (arg.angle !== undefined) angle = arg.angle
730738
if (arg.axis !== undefined) axis = arg.axis
@@ -748,14 +756,14 @@
748756
// all VPython instances of ".replace" (in user code) with ".__GSrep".
749757
String.prototype.__GSrep = function(t, r, n) {
750758
if (n === undefined) n = 1000000
751-
var s = this
759+
let s = this
752760
if (t === r) return s
753-
var tlong = t.length
754-
var i = 0
761+
let tlong = t.length
762+
let i = 0
755763
while (true) {
756764
if (i >= n) return s
757765
i++
758-
var loc = s.search(t)
766+
let loc = s.search(t)
759767
if (loc < 0) return s
760768
s = s.slice(0,loc) + r + s.slice(loc+tlong)
761769
}

package/glow.3.2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)