What Do I Need To Know About JavaScript Part I

This is a frequent question I receive from those wishing to parlay a knowledge of Javascript into a long career as a developer. Speaking of long careers, mine currently spans 35 years of professional experience.  I began this long journey writing assembly-language math libraries for supercomputers.  Of course, how you start is rarely indicative of how you will end.  Since then, I’ve also worked with Fortran, C, C++, Java, JavaScript (and Mocha/LiveScript), ActionScript (1/2/3), and Typescript.  This reminds me of the line from RUSH’s popular Tom Sawyer song, “changes aren’t permanent, but change is.”

Whenever I take on the task of learning a new language, I’m always interested in the internals of what makes that language work.  What was the design philosophy of the language? Is there a single concept or a group of core concepts that explain how and why the language/environment produces run-time results?  I’ll discuss three areas in this post and then provide a second post on additional topics if there is sufficient interest.

Functions are first-class language objects in Javascript and almost everything of importance in an application revolves around them.  So, I’ve personally found understanding function execution context to be highly valuable in constructing an understanding of Javascript internals.

EXECUTION CONTEXT

Now, I don’t like re-inventing the wheel, so wherever possible, I will provide links to articles on individual topics that I’ve found to be not only accurate, but very helpful in leading to an in-depth understanding of that topic.  And, when it comes to execution context, I still think Dave Shariff’s article is one of the best,

http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/

This article is well worth spending a lot of time understanding, particularly the section on hoisting.

Following are a couple key takeaways from that article.  You will hear the terms ‘context’ and ‘scope’ mentioned a lot and these concepts are not unique to JavaScript.  A ‘scope’ is simply a well-defined area in a run-time environment that is used to look up variable references.  Let’s not overcomplicate things.  If I say to someone, “Hey, look at that red Mustang,” but there is a red Mustang in both the Kroger and Walmart parking lots, then I need to specify which lot (or scope) in order to make the vehicle (variable reference) unique.  I may have to be even more specific and identify the red Mustang at the south end of the Kroger parking lot.

There are three possible contexts in Javascript,

– Global (the application has to start somewhere)

– Function (program flow enters a function body)

– Eval (execution inside an internal eval function)

So, what is ‘scope’ in Javascript?  At any single point in the flow of program execution, it will be one of these three contexts (parking lots).

Execution Context consists of three items,

  a) A scope chain, represented as an ordered list of variable objects (V0), or variable tables, that are ordered from the function’s direct parent back up to the global context.

  b) An internal variable table or VO for the function itself that includes the arguments Array and an entry for each variable defined internally in the function.

  c) A unique resolution to the self-referential pointer (i.e. ‘this’).

The final item requires some additional explanation.  We must be able to uniquely and predictably resolve references such as this.x inside a function, so how Javascript assigns the self-referential pointer is very important.  It is also unique to JavaScript in that the assignment depends on how the function is invoked.  I’ve found this knowledge to be rather important as it can explain a lot when you are forced to debug/modify code written in the past by one or more devs.

Here are the ways JavaScript assigns the self-referential pointer based on function invocation:

1 – Method invocation.  The function is a property of an Object.  ‘this’ is bound to Object in which the function is a property.

2 – Function invocation. The function is defined outside an Object and invoked by direct call.  ‘this’ is bound to the global (typically Window) object.  Crockford correctly pointed this out as a flaw in the language and one that can result in undesired (and not easily understood) results.  Let’s look at some examples.

Copy and paste the following code block into your browser console and execute,

var x = 2;
function foo() {
  var x = 1;
  console.log('x in foo:', this.x);
};

foo();

The function, foo, is defined ‘standalone.’  When it is called on the last line, that is known as Function invocation.  The execution context for the foo function consists of a scope chain containing only the global context.  The VO consists of a single variable, x, which is not defined until the activation stage of execution context.  The arguments Array is empty.  The self-referential pointer is set to the global context.

So, even though there is a reference to a variable named x inside the foo function, resolution of this.x comes from the global context.  So, ‘x’ is logged as 2 in this example.

Suppose the self-referential pointer was removed and the example was,

var x = 2;
function foo() {
  var x = 1;
  console.log('x in foo:', x);
};

foo();

Based on what we have studied from the concept of execution context, x is defined inside foo’s VO as 1 during the activation stage of execution context.  With only a ‘raw’ variable reference, the first attempt at name binding or variable lookup is from the function’s VO, from which a variable with that name exists.  In this case, x is logged as 1.

Now, let’s change the example to

var x = 2;
function foo() {
  console.log('x in foo:', x);
};

foo();

In this case, the first lookup for x inside foo’s VO fails.  Subsequent lookups proceed along the scope chain defined during the activation stage of execution context.  The first (and only) VO in this chain is the global context, in which the variable, x, exists.  In this instance, x logs as 2.

And, since you understand that unique assignment of the self-referential pointer is part of a function’s execution context, and you understand the steps in variable lookup, you should understand that in this example,

var y = 2;
function foo() {
  var x = 1;
  console.log('x in foo:', this.x);
};

foo();

The variable, x, now logs as undefined as there is no such variable in the global context.

3 – Constructor invocation.  The function is defined and stored in a variable and then invoked with the new keyword, i.e.

var x = 7;
var y = 8;

var Foo = function() {
  this.x = 1;
  this.y = 2;
};

Foo.prototype.z = function() {
  return this.x + this.y;
};

var bar = new Foo();
console.log('z: ', bar.z());

This is an important example as it is the means by which traditional classes are approximated in JavaScript, which is built on prototypal inheritance, i.e. Objects simply inherit from other Objects.  When constructor invocation is applied, an addition is made to the function’s VO that is a reference to the function’s prototype.  The self-referential pointer is then set to that reference.

So, in the above example, the variable z logs as 3.  However, in this example,

var x = 7;
var y = 8;

var Foo = function() {
  var x = 1;
  var y = 2;
};

Foo.prototype.z = function() {
  return this.x + this.y;
};

var bar = new Foo();

console.log('z: ', bar.z());

z logs as NaN.  Why?  The function’s VO includes table entries for variables x and y, however, this operation does not call a method created by the function, but one that exists on its prototype.  Variables x and y are never defined on the prototype, so the addition can not be coerced to a meaningful result (more on that later).

4 –  Apply/bind – Apply/Call/Bind allow a function to receive a specific context for the self-referential pointer.  This is the easiest invocation example to understand.

5 – Lexical propagation, aka arrow functions.  Lexical propagation is simply a fancy name given to passing of the parent context of a function to the function itself.  It’s simply a replacement for assigning a reference to the ‘this’ pointer to a variable, i.e. var parent = this or var that = this and then using the parent variable inside a child function body.  Instead of this.x inside the function body, the code references parent.x, for example.

Lexical propagation via arrow functions allows a function to be defined independently using references such as this.x .  The function may be imported into a Typescript class, for example, and lexical propagation provides an automatic method closure.

This statement leads us right into the next section 🙂

SCOPE AND CLOSURE

Well, now, we get into some of that computer-science stuff, but some principles are best understood from a more  fundamental, language-independent context.  Some of what you may read on these topics in Javascript are wrong or only partially correct.

Name-binding or the mapping of a name such as a variable to a specific value is important in any language. Scope is a specific region of the program in which a specific name-binding is applicable. Scope is normally identified at the syntax level, although it is applied at run-time.

Shariff’s article was written before ES6 and at that point in time, variables in Javascript were scoped at the function level.  A variable x, for example, might be defined inside an if-block, but a property for that single variable is created inside the function’s VO during the activation stage of execution context.  That same variable might be assigned a new value outside the if-block, but there is still only a single entry in the VO that serves to bind the name, x, to a single value inside the function, regardless of where the variable is declared inside the function.

ES6 introduced let and const variables that are block-scoped, which makes the function’s VO table more complicated.  Name binding during the process of function execution can now depend on a specific, basic block inside the function body.  This is illustrated by example,

var x = 2;
function foo() {
  var x = 1;
  var y = 2;
  let z;

  if (y > 0) {
    let x = 3;
    z     = x + 2;
    console.log('x/z inside if:', x, z);
  }

  console.log('x in foo:', x);
};

foo();

There are two references to the variable, x, inside the function, foo.  So, how is name-binding applied?  There are no references to the ‘this’ pointer, so the variable is looked up first inside the function’s VO table.  However, there are two scopes in which x can be resolved.  The first is block-level and is inside the basic block encapsulated by the if statement.  Name-binding begins at the current scope, which is the if-block when the first console log is encountered.  Since there is an immediate resolution of the variable reference, the value of 3 is logged for x.  However, that scope only persists for the duration of the if-block.  If the let x = 3 statement were removed (try this) then name-binding is applied at the next-highest scope, which is the function level.  There is a match at that level, so x logs as 1.  If the var x = 1 statement were removed, there is no match anywhere in the function’s VO, so lookup proceeds along the scope chain created during the activation phase.  As with the other examples, there is only one VO in that chain, which is the the global context.  There is a match on x in that VO and the value of 2 is applied to the variable, x.

So, in this example, we could say that there are three scopes or regions of the program in which name-binding can be applied, each of which produce a possibly different resolution for the same variable name, or the variable may not be bound at all. These scopes are the if-block inside the function foo, the function itself, and the global context.  Think of it as dividing the parking lot into north and south partitions and then further specifying an east/west partition for the south partition.  These union of these partitions may or may not be the entire parking lot, so we may think of the entire parking lot as the global context.  The variable ‘red Mustang’ is uniquely resolved (or not resolved at all) based on a specification of scope.

Now, what about those pesky closures you may have heard of or read about somewhere else?  Yes, here we go with more computer science stuff …

I like the Wikipedia definition of closure the best, which is simply an implementation of lexically scoped name binding in languages with first-class functions.  Lexical scoping refers to independence from a run-time environment and dependence upon the lexical or program-level structure of the language to define scope.  A closure is applied to a function as an independent entity by creating a combination of the function with a context for name-binding.  From the prior material, we can see that a function closure is created by the totality of the environment created during execution context.

When closures are applied to a method of a ‘class’ (recall this method exists on the function’s prototype), this combination of function and context is called a method closure.  ActionScript, for example, was ECMAScript compliant since version 5 of the Flash player.  ActionScript 2 introduced the syntactic sugar of classes that compiled down to AS 1 in a similar manner to how TypeScript compiles down to ES5.  Classes are not first-class language objects in any current version of JavaScript and this was also true of AS 2.  To use a class method as an event handler, for example, we created method closures using a helper function called Delegate.

For the remainder of this article, we can infer that closure always means function closure.  Closures may be created and manipulated by developers, generally for the purpose of variable privacy or to avoid undesirable side-effects of code that may seem to perform a required task, but do not execute as desired.  Closures may also be used in the technique of partial application of functions and the following is a good introduction to the topic,

https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-closure-b2f0d2152b36

While you can understand a lot (theoretically) about closures through function execution context, there are some subtle issues with how they are created and applied that are best illustrated by example.  Consider the construction-invocation example, above, with an expanded console log.

var x = 7;
var y = 8;

var Foo = function() {
  this.x = 1;
  this.y = 2;
};

Foo.prototype.z = function() {
  return this.x + this.y;
};

var bar = new Foo();
console.log('z: ', bar.z(), bar.x, bar.y);

Execute this inside your browser console and note that z, x, and y, log to 3, 1, and 2, respectively.  While it may be tempting to consider the variables x and y to be ‘closed’ inside Foo, they are completely visible (and can be manipulated) outside that function,

var x = 7;
var y = 8;

var Foo = function() {
  this.x = 1;
  this.y = 2;
};

Foo.prototype.z = function() {
  return this.x + this.y;
};

var bar = new Foo();
bar.x   = 10;
bar.y   = 11;

console.log('z: ', bar.z());

Ouch.  Now, z is 21.

While the latest version of JavaScript supports truly private variables in ‘classes’ by prefixing with # symbol, you may be called upon to work with code created by other developers, perhaps many years ago, in which manual closures were created to facilitate data privacy.  For example,

const point = function(x, y) {
  let xCoord = x;
  let yCoord = y;

  return {
    getX: () => xCoord,
    getY: () => yCoord
  };

};

const somePoint = point(0, 3);
const xValue    = somePoint.getX();
const yValue    = somePoint.getY();

console.log('values:', xValue, yValue, somePoint.xCoord, somePoint.yCoord);

Note that this logs 0, 3, undefined, undefined.

The accessor functions, getX() and getY() reference variables defined in the VO of the point function, although we can not directly access those variables from outside the point function.  This is unlike the prior example, which utilized the Foo function’s prototype (which any Object may access) to create variables and methods.  Sometimes, functions such as getX() and getY() are referred to as privileged methods.  These methods were defined at a level in which name-binding of the variables xCoord and yCoord could be resolved to known, defined values.  So, they are ‘privileged’ to have access to that information during their own execution context activation stages as a result of being defined inside the function, point.

This type of closure can also be used to create stateful functions, sometimes in tandem with an IIFE (Immediately Invoked Function Expression).  An example I use when teaching this topic is a Wythoff iterator.  Strictly speaking, this iterator does not produce sequences that are exact rows of a Wythoff array, but it is Fibonacci-like and accepts two starting values, just like the Fibonacci sequence.  Subsequent values of the iterator are obtained by adding the two previous values.  This example, with starting values of 0 and 1, could be considered a Fibonacci iterator.  Now, the Fibonacci sequence is a recurrence relation with constant coefficients and all such relations have a closed-form solution (see Knuth TAOCP).  The closed-form solution for the Fibonacci sequence is Binet’s formula, but that is not used in this example.  Instead, we maintain internal variables, first and second, which are overwritten each time the iterator’s next() function is called.  This code is crude, but serves to illustrate the desired concept,

const iterator = (function (f, s){
  let first  = f;
  let second = s;

  return {
    next: () => {
      const tmp = first + second;
      first     = second;
      second    = tmp;

      return tmp;
    }
  }
})(0, 1);

console.log('next:', iterator.next());
console.log('next:', iterator.next());
console.log('next:', iterator.next());
console.log('next:', iterator.next());
console.log('next:', iterator.next());
console.log('next:', iterator.next());
console.log('next:', iterator.next());
console.log('next:', iterator.next());
console.log('next:', iterator.next(), iterator.first, iterator.second);

The IFFE results in immediate invocation of the function with the arguments 0 and 1.  This triggers the activation and execution phases of that function’s execution context in one step, so to speak.  Space for the function variables first and second is created in the VO table during the activation phase.  Specific values of 0 and 1 are assigned to those variables in the execution phase.  Since the result is stored in a variable, iterator, we might think of this process as defining a state for the function that influences its execution on each call of the privileged method, next().

Run this code in a fresh browser console and you should see the result,

next: 1
next: 2
next: 3
next: 5
next: 8
next: 13
next: 21
next: 34
next: 55 undefined undefined

The final line once again illustrates that we can not directly access the internal variables, first and second.  State is maintained between function calls.

This example can be extended more complex examples and many have labeled this general approach the ‘Revealing Module Pattern,’

https://weblogs.asp.net/dwahlin/techniques-strategies-and-patterns-for-structuring-javascript-code-revealing-module-pattern

Loop induction variables that serve as function arguments provide another interesting example of closures and a segue into some of the newer language features.  Consider a classic example,

var funcRefs = [];
for (var i = 0; i < 5; i++) {
  funcRefs[i] = function() {
    console.log("value is: " + i);
  };
}

for (var j = 0; j < 5; j++) {
  funcRefs[j]();
}

Run this in a browser console and you will see ‘value is: 5’ repeated five times.  The variable, i, is scoped to the global context in this example, or a level just above the first for-loop.  That variable is maintained after loop execution is terminated and its value is 5.  Why 5?  The induction variable is incremented before the test for loop termination is made.

When each anonymous function is created, its scope chain contains the global context as part of the execution context created during the activation phase.  When each function is executed, variable lookup starts at the anonymous function’s VO.  There is no reference to a variable, i, so lookup proceeds up the scope chain. That name is bound in the global context to the fixed value, 5.  So, in the second loop, each function resolved the variable, i, to the same value.

I’ve seen a number of resolutions to this problem, even to the extent of IIFE’s created inside the loop.  I’ll show you the ‘classic’ or ‘old-school’ resolution to the problem and then discuss a couple more modern solutions, should you encounter this case in code you are assigned to debug and/or modify.

From the example above, we can use the internal VO table of a function as a means to store ‘state.’  The value of the induction variable for the first loop can be passed to a function factory and then stored in the VO for that function,

var funcRefs = [];
function fcnFactory(i) {
  return function() {
    console.log("value is: " + i);
  };
}

for (var i = 0; i < 5; i++) {
  funcRefs[i] = fcnFactory(i);
}

console.log('variable i is:', i);

for (var j = 0; j < 5; j++) {
  funcRefs[j]();
}

Verify that this displays the result that was originally intended,

variable i is: 5
value is: 0
value is: 1
value is: 2
value is: 3
value is: 4

The problem in this example is function-level scoping.  If we could make the first loop induction variable block-scoped (as in ES6), then the example should work as desired.  This is due to the fact that when the anonymous function is created, the ‘outer’ scope is the context containing the value of the induction variable at each loop iteration.

var funcRefs = [];
for (let i = 0; i < 5; i++) {
  funcRefs[i] = function() {
    console.log("value is: " + i);
  };
}

console.log('variable i is:', i);

for (var j = 0; j < 5; j++) {
  funcRefs[j]();
}

Note that in this example, the loop induction variable does not exist outside the loop; it can only be bound inside the loop and each iteration produces a different bound value.

We can also avoid problems of this nature with automatic closures provided by Array methods such as forEach.  Each invocation of the callback function is given its own context (or closure) based on the variable(s) used in the function argument(s). 

COERCIONS

This is my final must-know topic in part I of this series, especially since JavaScript likes to ‘make things work,’ even things that on the surface seem to make no sense.  In order to better understand what I’m talking about, let’s discuss the difference between casts and coercions.

A cast in any language is a ‘hard’ statement that is intended to force the compiler to generate code to convert a variable of one type into a variable of another type.  In C++, for example, a developer may force a variable to be converted from a double to an int by a statement of the form,

n = (int) z

where the variable, n, is typed int and the variable, z is typed double.  This statement could be interpreted as “yes, I know z is a double-precision float, but FORCE it to be an integer and then assign the result to the variable, n“.  Casts, while specific, still require some understanding of how the cast is performed.  For the above example, does the compiler truncate or round the floating-point number?

At least a cast is a specific statement by the developer that indicates intent.  A coercion on the other hand is performed silently by the environment and some developers may not even know that the coercion occurred.  Coercions occur when assignments are made from one variable type to another, during some operations, and even during comparisons.  The latter case often takes new JS developers by surprise and is the primary reason that strict equality (triple-equals) tests are preferred over basic equality tests (double-equals).

Here is a great example.  Study the table in this link and see what can happen when variables of different type are compared with a double-equals test.

https://dorey.github.io/JavaScript-Equality-Table/

Coercions occur ‘under the hood,’ sometimes without a developer understanding that they are happening at all.  Internally, JavaScript ties to convert one or more variables of an existing type into a type that somehow makes the operation or assignment ‘work,’ even if the definition of ‘work’ does not make sense to us.  There are also some instances in the language that are somewhat in-between a cast and a pure coercion.

One example is the infamous double negation, ie. var x = !!y.  There is no double-negate operator and I personally do not like this practice.  But, some people like writing tricky or excessively compact code, so you may encounter this syntax in pre-existing code.  The first negation attempts to convert the existing variable, y, to a Boolean primitive and then logically negates the result.  Something that is truthy is converted to Boolean false and something falsey is converted to Boolean true.

The second value logically negates the first, so the final result of the coercion is that truthy variables are converted to Boolean true and falsey variables are converted to Boolean false.

Another example is placing a plus or minus sign before a string variable, i.e. +x or -y (the unary prefix operator).  This operator attempts to coerce the string into a positive or negative number.

JavaScript actually has a very organized system for converting complex objects to primitives and then converting from one primitive to another.  I remember an discussion with a somewhat new JS developer (less than five years experience) who was frustrated in an interview when asked to provide the result of the operation [] + {}.

I think the question, as posed, is a bit ridiculous as it infers a sense that someone might intentionally attempt such an operation.  It could, however, occur as a side-effect in a legacy system were variables were generalized from primitives such as string or number into Object and/or Array. These variables could be passed in some rare outlier case to a function that expected primitives, but received something more complex.  JavaScript is in the business of results.  Some result.  Any result.  Making sense does not matter 🙂

So, it does help to understand the internals of this coercion system, especially if you are called upon to work on code that has existed for a long time and has been revised by a number of developers over the years.  Now, before we being, memorize the primitive types in Javascript, namely undefined, null, boolean, number, string, bigint, and symbol.  Everything else is an Object (including Arrays and functions).  Primitives are immutable and can not be changed, and we’ll discuss that more in part II. 

On the subject of coercion, primitives do not have properties, so how is it that we can access the length property of a string?  JavaScript will happily and temporarily coerce a primitive to an Object, imbue the temp Object with properties, and then make the Object available for garbage collection after the property access.  Be very careful with the last sentence as it possible to assign properties to a primitive, but access to those properties is undefined.  For example,

var someString = "Math is cool";
someString.numWords = 3;
var words = someString.numWords;
console.log(words);

The variable, words, is undefined.  Try it in your console.  Behind the scenes, JavaScript creates a temporary Object that allows assignment of the numWords property, but no permanent reference is created for that Object.  It is immediately marked for garbage collection.  JavaScript creates a second, new temporary Object in order to allow access of the numWords property, which of course, does not exist.

Now, how does Javascript perform coercions from Objects to primitives and primitive to primitive?  In particular, how does JavaScript perform seemingly crazy operations such as [] + {}?

I’ve found this article to be an excellent introduction to the topic,

https://2ality.com/2012/01/object-plus-object.html

The key takeaways from this article are first understanding important built-in Object methods such as valueOf and toString.  Then, the coercion process can be broken down to convert to primitive followed by convert to similar primitives.  The answer to the above question is covered in that article, namely the string, ‘[object Object]’.

Coercions are subtle because there is no direct indication in the code (such as a hard cast) as to their application.  Even casts may not produce a desired result simply because at run-time, JavaScript attempts to perform the cast and then executes a coercion before completing execution of a specific line of code.  There is only one way to become effective a dealing with coercions and that is to study JS internals and understand where coercions are applied.  That will make it easier to diagnose issues in legacy code and, hopefully, force you to take more time in being explicit about intent when writing your own code.

Well, that brings us to the end of part I and we have not discussed important topics such as event loop, pass by value or reference, data structures such as Set/Map, Promises, and more.  If there is interest, I will author parts II and III of this series an we can dive into those topics along with curated links to quality content that should be part of your regular study.

In the mean time, here’s a preview of what is coming next.  Try predicting and then explaining the output of the following code block.  I regularly use this example in lectures to JS students.

var arr = [1, 2, 3];
var value = 1.0;

var somefunction = function(v, a)
{
  v = 2.0;
  a = [4, 5, 6];
};

somefunction(value, arr);

console.log( "v/a: ", value, arr);

var anotherfunction = function(v, a)
{
  v    = 3.0;
  a[0] = 4;
  a[1] = 5;
  a[2] = 6;
};

anotherfunction(value, arr);

console.log( "v/a: ", value, arr);

Hint: Primitives are immutable and are passed by value. JavaScript creates a separate handle or reference to Objects and passes that reference by value. I like to use the analogy of a pointer variable in C and C++ . So, there is a difference when a pointer’s value changes and thus points to something new vs. dereferencing that pointer. The copy still points back to the original entity, so the dereference affects the original’s value. Okay, that was more like an explanation than a hint, but I hope it stokes your appetite for new content.

Thanks for reading such a long post and best of luck to you in your JavaScript career!

New Articles on LinkedIn

I’ve moved several blocks of content from the algorithmist.net domain into my public profile on LinkedIn.  Here are the articles, which cover a variety of topics including angular, typescript, math, and datastructures.

https://www.linkedin.com/pulse/angular-champion-jim-armstrong

https://www.linkedin.com/pulse/typescript-decision-tree-jim-armstrong

https://www.linkedin.com/pulse/typescript-sequencing-engine-jim-armstrong

https://www.linkedin.com/pulse/reactive-data-driven-finite-state-machine-jim-armstrong

https://www.linkedin.com/pulse/5-sided-die-problem-jim-armstrong/

https://www.linkedin.com/pulse/minimizing-worst-case-complexity-jim-armstrong/

The Blog is Back!!!

After many years of maintaining the algorithmist.net domain (and a business blog on that site), the cost-effectiveness of that effort in combination with the meteoric rise of social media has led me to let go of that domain.

My primary online presence is now my LinkedIn Profile.  This is a quick post to inform past readers to expect new content here based on my efforts since May 2015 to focus almost exclusively on Angular/Typescript development.

And yes, just like before, … there will be math!

Please note that some of the older posts may contains links to the old site (Flash/Flex projects) that are no longer valid.  If there is sufficient interest, I will try to re-create these in Angular/Typescript and place the repos in my GitHub.

Thank you.

 

Time To Say Goodbye

When I started this blog, I wasn’t sure how much time I would devote to blogging, so I took the free WordPress route. I’ve been incredibly busy over the last year and a half and the lack of posts reflects that situation. I’m now in a position to devote a small amount of time to blogging and I decided to take the leap into the 21st century and create a WordPress site at algorithmist.net (my business site).

Since I now have a blog at my site, I will no longer maintain this one.  I will leave all the posts online since many of them have code samples from which readers may realize some benefit.

I will continue blogging about math and programming, just in a different place.  I hope you enjoyed the content of this blog and will keep up with the new one.

thanks!

– jim armstrong

FlashBuilder 4.6 Hangs on Startup

Sorry about the extreme lack of posts.  I hope this tip makes up for it 🙂  So, this morning I load FB 4.6 and it hangs.  I tried all the usual suspects and still no luck.  I searched online and read the amusing story of Adobe wanting to charge people $250 for a tech support call to fix what appears to be a bug.  Clearly a one-trick pony company with Photoshop – they sure can’t make anything else work.

Don’t waste your time or money with DumbDobe.  A bit more searching yielded this post and it worked for me without reinstall or any other hassle.  I wanted to pass along some props to the original poster and give the fix some attention in case this happens to you.

And Adobe is leading the charge for HTML 5?  Yikes! I knew I should have been a lawyer.

Rotate A Point Collection About Another Point

Well, this will probably be the last, or nearly last post of a busy year that involved a lot of work on gigs and little focus on this blog.  Sorry, maybe next year will be better 🙂

In the previous example, I showed how to rotate a box (rectangle) around an arbitrary point, but the algorithm and code never presumed anything about the geometric nature of the object.  It’s possible to extend the exact same algorithm to a collection of points and that is the subject of the current post.

Instead of maintaining variables for the four vertices of a rectangle, the code was modified to work with an arbitrary point collection.  A RotatablePoint class was used to hold data points and offload some of the computations.  This greatly simplifies the actual demo and provides you with ample means for experimentation.

The online example starts with a small collection of points as shown below.

rotate

These points are rotated about the fixed point, drawn as a red dot.  The drawing may be cleared after each rotation increment or continually updated from the prior drawing as shown below.

rotate1

View online demo.

View source.

Have a Merry Christmas and I’ll try to post more next year!

Trig Plus Algebra Plus Geometry Plus Vectors Equals Fun

Math teachers hear the most often-uttered student phrase in history a lot more than I do, but my ears have twitched to the infamous, “I’ll never use that” or its many variants over the years.  Funny how people know right now what will and will not be needed or used for the rest of their lives 🙂

Now, taken in strict isolation, a single trig or algebra formula might appear to have limited or no use in the myriad of life situations in someone’s future.  It is, however, quite interesting how often problems can be solved by strategic use of a single formula from multiple areas of study.  The problem discussed in this post is one such example.

In the prior post, I illustrated how some basic trig concepts could be used to solve a rotation and bounding-box problem without any presumptions or special considerations of the programming environment.  This example is similar in that it illustrates how to rotate a box around an arbitrary point in its interior.  The box is represented by a sequence of four coordinate pairs.  It is not a Flash symbol or anything other than a sequence of coordinates.  The drawing environment is simple; it can move the pen, draw lines, and draw filled circles.  The programming environment contains the typical set of math functions.

Our math background includes a semester of trig, analytic geometry, and algebra.  We know the very basics of vectors, but have not been introduced to matrices or matrix/coordinate transforms.  As it happens, all we need to know to solve the stated problem is

1 – Polar coordinates, i.e. x = r cos(a) , y = r sin(a)

2 – Parametric equation of a straight line, i.e. P = (1-t)P0 + tP1

3 – How to add and subtract vectors

4 – How to compute the distance between two points

Refer to the diagram below.

The four points of the box are A (x1,y1), B (x2,y2), C (x3,y3), and D (x4,y4).  The rotation point is R (rx,ry). The example code was written in Flex and the coordinate system in the Flash player is y-down.  We are not given the coordinates of R.  In fact, the box may be at an angle to the horizontal as shown in the lower, right part of the diagram.  The only thing we know about R is that the component parallel to each side of the box is a fraction of that side’s length.  This is where vector math can be useful.

The coordinates of R can be computed by adding the vectors V1 and V2.  The percentage inputs can be converted into numbers t1 and t2 in [0,1].  The exact coordinates of the terminal points of these vectors are obtained from the parametric equation of a line.  Let the non-bolded notation V1 and V2 refer to vectors from the origin representing these terminal points.  Then, V1 = (1-t1)A + t1B and V2 = (1-t2)A + t2D. Since the initial point of both V1 and V2 is A, the vector constituents (Δx and Δy) are immediately available.  Add the vectors and add the result to A to obtain the coordinates for R.  So, we can adjust sliders that change the percentage along each side and quickly compute R regardless of any prior rotation since we should always recompute the correct coordinates for A, B, C, and D.

When R is set, imagine lines drawn from R to each of the vectors A, B, C, and D.  Let the distances be r1, r2, r3, and r4, respectively.  Also compute the angle each line segment makes with the horizontal using the atan2() function.  Since this returns a value in [-Π, Π], a small adjustment is made to always record the angles in [0, 2Π].  This makes studying the angle computations a bit easier if you want to trace out the results as part of deconstructing the code.

Also record the initial rotation angle when R is assigned.  Subsequent changes to this value rotate the box around R by some delta.  If the initial angle of the box is θ, and the delta is δ, then the four points are rotated around R by an angle θ+δ. Compute the coordinates of each new vector, A, B, C, and D by using the formula for polar coordinates and add the center, R.

These concepts are illustrated in an online demo.

The percentage along each side used to compute R is adjusted by sliders as well as the rotation angle.  The red dot visually identifies the rotation point, R.  You may optionally display the circle traced by each of the four corner points as the box is rotated.  There is a _clear (Boolean) variable in the code that you may adjust to either clear the drawing each rotation update or choose to draw over each update of the box.  If the value is false, you can produce some spirograph-style drawings.

The UI was created in Flex, but the actual code is very straightforward and does not rely on anything unique to Actionscript.  It could be ported to many other environment quite easily.  There is also some internal documentation on how to use trig identities to reduce the number of trig computations at each rotation update.

Even more importantly, there is nothing in the code or the algorithm that relies on a box being rotated other than computing R.  The actual rotation is applied to a sequence of points using the simplest of concepts from a few math disciplines.  I hope this provides some sense of appreciation of problems that can be solved using only the most fundamental techniques from a few branches of applied math.

View demo

View source