Following up from my last post, I have created yet another sine wave animation (YASWA) and I want to blog about how I achieved a smooth animation for the svg path shapes and arcs with d3.js.
I am not going to go over how to set up the basic shapes in the animation, you can find out how that is done by referring to my last post.
The 3 path shapes that I found challenging to animate are the red sine wave that is progressively added and removed from the document as the unit circle rotates along the x axis and the two blue angled arcs that form the small angled arc at the centre of the centre of the larger circle and the blue arc that expands around the circumference of the larger unit circle.
The first step is to append the paths to an svg group element. Their initial position is not important at this stage.
There is a state object (line 1) that is used as a container to hold a reference to all the svg shapes that will be transformed on each tick of the animation. The shapes are added as properties of the state object. The sine curve path shape is added to the state object on line 10 and the two path shapes that will render the arcs are bound as properties of the state object on lines 13 and 17 repectively.
Line 3 creates a d3.js line function that will be used to generate the rather cryptic svg path mini language instructions for the d attriute of the svg path shape that will plot the points of the sine curve.
A sineData array is initialised on line 8 and the line function will be executed for every element in the array and the x and y accessor functions on lines 5 and 6 will be executed exactly once for each element in the array. Obviously as the array is initially empty, these functions will not be invoked until there actually is some data.
Now that the the svg shapes are on the document, the next step is to kick off the animation function:
line 3 initialises a state.time variable that will be incremented on each tick of the animation. This variable is central to all calculations that will be used to position.
The body of the animate function that is called on line 5 of the above code snippit takes the following form:
animate.js
123456789101112131415161718192021
animate(state,direction){if(direction.forward){state.time+=state.increase;}else{state.time-=state.increase;}// position shapesif(direction.forward&&state.time>(Math.PI*2)){direction={backwards:true};}if(direction.backwards&&state.time<0){state.time=0;direction={forward:true};}requestAnimationFrame(this.animate.bind(this,state,direction));}
The state.time counter is either incremented or decremented on each tick of the animation depending on whether the animation is moving forwards or backwards.
Lnes 11 to 18 make a simple check of whether the time is greater than 2π, in which case it is time to start animating backwards or if the animation is moving backwards and time is less than 0 then the shapes need to move forward. The current direction is passed into the animate function with each call to requestAnimationFrame.
My first attempt at incrementally drawing the sine wave was to add and remove the sinewave on each call to animate but this led to a very jerky horrible visual effect as the browser struggled to recreate the curve each time from the origin.
The solution was incredibly simple and I simply make a call to a function called progressSineGraph that is listed below:
The state object that contains references to all the shapes and properties that are needed to perform the animation is passed into the function along with the direction argument that specifies whether the shapes are animating forwards or backwards.
The code between lines 2 and 6 either adds the current value of the time variable to the sineData array if the animation is progressing forward or removes the current head of the array if the animation is moving backwards. The elements of this array will be used to plot the sine graph.
On line 8 the d attribute of the path shape is set to the state.sine function that is listed below. As mentioned earlier, the d attribute contains the mini language instructions to plot the curve. The line function will generate the instructions by calling the function below for each element in the array:
The line function will be called for each element int the array and x and y coordinates will be created by calling the x and y accessor functions that accept the current element as an argument.
I was pretty amazed how easy this technique is to animate shapes progressing or regressing.
The d3.js line function is a great abstraction and you can see what the generated output of the line function looks like below. The state.sine function has created the d attribute of the path shape that contains the instructions to draw the sine curve. I know which I would rather work with:
It is also worth noting that montone interpolation is set to ensure a smooth curve is drawn.
Animating the Arcs
All of the shapes in the animation are interlinked in some certain way and all the calculations of where to place the shapes on each tick of the animation are based on the incrementing state.time counter.
The black line that rotates from the centre of the circle to the circumference can be thought of as the hypotenuse of a right angle triangle. The hypotenuse in this document is an svg line shape with x1, y1, x2 and y2 properties that are set using cartesian coordinates to position the shape.
The two arcs that form the angle at the centre of the circle and at the circumference will also use the hypotenuse coordinates once they have been calculated.
Below is the code that positions the svg line shape on each tick of the animation:
Once the x1, y1, x2 and y2 corrdinates have been calculated, they are bound as properties of a hypotenuseCoords object that can be referrenced later. Lines 15 to 18 positions the shape.
We can now use basic trigonometry and the d3.js arc function to create the arcs. The arc function is also a to the d3.js svg line function as it also generates instructions for the path shape’s d attribute but it also has some additional properties such as the innerRadius, outerRadius, startAngle and endAngle properties that are specific to generating arcs.
Below is the code that sets the properties pf the two arcs:
The main calculation takes place between lines 1 and 10 and I would love somebody to tell me there is a better way than this. I always find that if I have used let to declare a variable and later reassign the variable after initialisation, this usually tells to me that I have got something wrong or I have taken the wrong path.
In order to set the angle each time, I need a to give the d3.svg.arc function a startAngle and an endAngle each time the function is called.
The startAngle properties of the 2 arcs are set on lines 15 and 20 and I am using a static value of PI \ 2 which is 90 degrees in radians. This is because a startAngle of 0 will position the startAngle at 12 o’clock on the circle and I want it to be at 3 o’clock or 90 degrees.
On line 1 the atan2 function is called to find the angle that the hypotenuse makes with the startAngle of the arc or 0 on the y axis property.
Arctan2 is different than arctan because it takes 2 arguments and returns an angle in the correct quadrant. If you are unfamiliar with the 4 trigonometric quadrants, you can read about it here.
The atan2 function takes into the account the two signs of the arguments that are passed in and places the angle in the correct quadrant. In this example the angle of the hypotenuse is found by subracting the y2 and y1 properties and subtracting x2 and x1 of the hypotenuse and passing them into atan2. atan2 returns the angle in radians between π and -π. Thus, atan2(1, 1) = π/4 and atan2(−1, −1) = −3π/4.
My first attempt at drawing the arcs did not contain the readjustment of the angle variable below;
angle.js
123
if(angle>0){angle=(-2*(Math.PI)+angle);}
Without this readjustment, the arcs where being rendered like this:
This is because atan2 returns the angles between π and -π and the angle was being calculated correctly for rays in quadrant 1 and 2 but not what was required for quadrants 3 and 4. The readjustment of (-2 * (Math.PI) + angle) gives same ray but in the correct direction when in quadrants 3 and 4 by adding the angle onto -2π.
Epilogue
I am very happy with the end result. If you can suggest a better way for any of the above then please a comment below.
I have spent the last year learning some of the maths I should have learned about 27 years ago. One of the things that I have found interesting while learning maths is the relationship between the unit circle and a sine wave graph of y = sin(x).
A sine wave is a mathmatical curve that describes a smooth repetitive oscillation and the unit circle is a circle of radius 1 centred at the origin (0, 0). The unit circle can be used to find special trigonometric ratios as well as aid in graphing. There is also a real number line wrapped around the circle that serves as the input value when evaluating trig functions such as sine and cosine.
A sine wave is a periodic function or a function that repeats itself at regular intervals. The most important examples of periodic functions are the trigonometric functions that repeat themselves over intervals of 2π. One journey around the unit circle is 360 degrees or 2π in radians. A sine wave shows the excursion around the circle happening in time and is ultimately a circle expressed in time. I have used d3.js to illlustrate how the journey around the circle corresponds to the sine wave movement over time.
There are a number of concepts that I wanted to capture in the animation:
Illustrate where the input value of the unit circle corresponds to the (x, y) coordinate on the horizontal axis of the sine wave.
Demonstrate how the right angle is formed by the angle of the radius moving counter-clockwise around the unit circle.
Show the number scale of the unit circle in radians as ratios in proper mathmatical notation in the browser in both the unit circle and the x axis of the sine graph. MathJax appears to be the only show in town that fits this requiremnt.
This process of creating graphs from the unit circle is often called unwrapping the unit circle.
Unit Circle Setup
The first step is to create the basic shapes that will illustrate the unit circle and use cartesian coordinates to position them. D3.js has an excelent scale abstraction that allows you to deal in a finer grained scale than pixels. I can now think of the dimensions of the svg document as a 20 x 20 grid which makes positioning things easier to reason about.
On lines 6 and 10 of the above code, horizontal x and vertical y scales are created and bound to two variables. These d3 scale functions take the input domain of 0 to 20 and map it to an output range of either the viewport width for the horizontal x axis or the viewport height for the vertical y axis. It is much easier to think of cartesian coordinates ranging from 0 to 20 than the very fine grained pixel scale.
With the scales in place, it is much easier to start positioning the elements:
An svg group element or a g element is created on line 5. All elements will be added to this group or container. Transformations applied to an svg group or g element are applied to all elements in the group which makes it extremely useful.
The three lines that will form the right angle triangle are created and bound to the adjacent, opposite and hypotenuse variables using the d3.js svg line shape. The initial values of the triangle line’s x1, x2, y1 and y2 coordinates are irrelevant at this stage, they will be set on each tick of the animation. It is just important to get them onto the document during the initialisation phase.
The larger unit circle is created and appended onto the group on line 10 of the above snippet and another smaller circle is added on line 38 which will be positioned counter-clockwise around the large circle by setting its cx and cy coordinates on each tick of the animation. This will give the impression that the small circle is rotating around the larger circle.
Below is how things look so far:
The next stage is to add the number line in radians of the unit circle:
Number Scale
In order to divide the circle into 8 lines and position the labels, I can kill two birds with one stone by creating an array of objects which have label and angle properties that I can iterate over and create the lines and labels:
Lines 2 to 11 define the array of objects to iterate over. Each object contains a val property that can be thought of as an angle to move counter-clockwise around the unit circle for each element in the array. There is also a label property which is the text of the angle in radians. The cryptic syntax of the label is written in LaTex, which allows you to define mathmatical notation that can be displayed in the browser. Mathjax will later parse these labels into authentic looking math symbols in the browser. More on my difficulties with MathJax at the end of the post.
On lines 12 and 13 the x and y coordinates are determined for each angle value in the array. By finding the cos(x) * radius and sin(x) * radius of each angle in the iteration of objects, I can ascertain the (x, y) coordinates of the point on the circumference of where to position the radian label and of where do draw the dividing line that will show the number scale on my unit circle.
Lines 15 to 16 add offset values for each x and y coordinate to ensure the labels all appear outside of the circle and not flush on the circumference.
Line 18 adds a new svg group element and the textual label is added to this newly created group.
Line 24 simply adds a line from the centre of the circle to the x and y coordinates on the circumference.
Before MathJax does the parsing, the circle looks like this:
A similar approach is used to create the sine graph axis but instead of iterating over an array, a pair of d3.js axis are created and latex syntax is used for the tickValues of the x-axis.
My first step is to create a hash of values that I have unimaginatively called state that serves as a container for all the elements and dimensions that I am going to need to transform the svg elements on the documents. I am going to pass this state object into the animation function that will set the new position of the elements on each tick of the animation. After that, the animation is kicked off with a call to drawGraph:
Arguably the most important value in the above is the time property on line 15 which will provide the x value of the horizontal value of the sine graph. This will be incremented on each tick of the animation.
Every time the drawGraph function is called, the time property of the state variable is incremented by roughly 1 degree on line 4 to simulate time moving along the x axis. I do the same on line 5 for the circle that travels along the x axis. I have a separate counter as I need to reset it to zero each time the xIncrement variable exceeds 2π.
Line 7 calls the drawSine function that draws the sine wave on each tick of the animation. More on this later.
Lines 9 to 17 positions the small circle that traverses along the horizontal x. The counter for this coordinate is reset each time it exceeds 2π.
The rest of the code in this function takes care of finding and positoning the shapes on the unit circle to simulate the small circle rotating counter-clockwise on the unit circle and constructing the right angle triangle.
Lines 19 and 20 find the next x and y coordinates of the next point on the circumference to position the small circle that rotates the larger circle and bind them to the variables dy and dx. I use minus in the expression radius * -Math.sin(state.time) because we want to simultate rotating back counter-clockwise. Once we have this coordintate it is easy to position the three lines that make up the right angled triangle on lines 26 to 38.
Line 49 calls requestAnimationFrame which tells the browser that you wish to perform an animation and requests the browser call a specific function, drawGraph in this instance, before the next browser repaint.
I am also using a lesser known overload of bind on line 37 that creates a new partially applied function with some or all of the arguments already bound each time it is called. I have blogged about this previously here. In this case, the state hash will be bound every time the function is called because I am passing it into bind as an extra argument after this. I use this technique a lot in javascript land.
Animating the Sine wave
Now to the meat and potato of the piece, namely animating a smooth sine wave. The sine wave is mathmatically a very simple curve and a very simple graph. It is a simple x-y plot with the x-axis representing time and the y-axis representing angular displacement around the unit circle.
Below is the drawSineWave function that is called on each tick of the animation:
The upper bound of the array which is 53 is chosen to fill the available width in the viewport
I will come back to why the calculation on line 5 is needed but on line 6 I do a further map of the array that was initialised on line 4 to create an x and y coordinate for each element in the array that can be used to plot the sine wave. The x coordinate is simply the element of the array and y is calculated by getting the negative sine of x because we are simulating the sine wave moving counter-clockwise. For each tick of the animation we simply subtract the state.time variable that we have used previously from x. If you look at the animation, you can see how the small circle in the middle of the unit circle moves up and down at exactly the same rate. Maths in action!
Below is how the sine wave looks without the transformation .map(x => x * 10 / 84) applied to the original array:
If we just use integers to plot the points we get the rough sine wave but if we use floats, we get a much smoother flowing sine wave. I multiply each value by 10 to space it out across the width and then divide by 84 to ensure I get a float back. 84 was arrived at by trial and error to ensure the wave spans across the graph.
Once I have my coordinates to plot the curve, the following code takes care of creating the curve on each animation tick:
Lines 7 to 10 define a d3 line function that will be used by the svg path element on lines 12 to 15. The d3 line function can be thought of as a path generator for a line or in this case a curve as the interpolation mode is set to montone interpolation in order to create a smooth curve for the sine wave. The line function will take the data array (sineData) and convert it into the rather cryptic svg path mini language instructions that the svg path element on lines 12 to 15 will use to construct the curve. We define accessor functions on lines 9 and 10 that will be called for every x and y coordinate of the sineData array on line 1. A monotone interpolation will then be performed by the path function for each point. Every x and y is mapped to the correct scale on lines 3 and 4.
Lines 12 to 15 attach the path element and sets its data via the datum attribute. The d attribute sets the path data or the mini language of path commands that the line function on line 7 will generate. The x and y accessors of the line function are invoked exactly once for each element in the array.
MathJax
All that remains is to tell MathJax to parse the latex into math symbols. I cannot believe how hard I found this. Below is how the code ended up after a lot of coffee, profanity and self doubt:
First of all MathJax did not seem to play nicely with react, I have to use a wait function to suspend execution until the MathJax is available. On lines 12 to 18 can best be desribed as extreme hackery. I have tagged any element on the svg document that contains latex with the tick css class. I add a hook to mathjax that is called whenever it has done its first parse, I then remove the svg element that was added by MathJax and re-add it again. This causes MathJax to reparse the markup and the symbols are rendered correctly. I don’t know if it was the fact that this is a react site that made this so difficult but it really did not play well with this site.
I am available for work right now, if you are interested then email me directly.
Following on from my last post on axes positioning, I have added the functionality to add a tangent to the curve on mousemove. You can see a working example here by dragging the mouse over the svg document.
Below is a screenshot of the end result:
The first steps are to create the elements that I will use to display the tangent indicator and also to hook up an event listener for mousemove on the svg document:
The above creates a circle to indicate where on the curve the mouse is relative to the x axis. A label is created to display the coordinate that the mouse is currently on with respect to the curve and lastly I create a line that will display the tangent.
Line 19 adds a mousemove handler to the svg element that has been previously created with the code below:
With this, I can use mathematical differential calculus to work out the tangent line. If I was to perform these steps with pen and paper, I would take the following steps:
Find the derivative of the curve
Substitute the x retreived from the mousemove event into the derivative to calculate the gradient (or slope for the US listeners) of the line.
Substitute the gradient of the tangent and the coordinates of the given point into the equation of the line in the format y = mx + c.
Solve the equation of the line for y.
What was surprising and enjoyable for me was that the steps on paper transferred into machine instructions quite well which is not always the case.
Before I plot the line, I want to position my circle and label onto the curve. I am already using the excellent mathjs library to get the coordinates to draw the curve:
Line 2 of the above code uses mathjs’s parse function to create an expression from the string input of the form:
Once I have the expression, I can evaluate it with different values. Lines 5 and line 9 evaluates the expression for each x value in a predetermined range of values. Line 19 plots the line.
As I know what x is from the mouse event, I can use mathjs to parse my expression with respect to x and get the y coordinate to position my label on the curve:
Lines 2 and 4 retrive x from the mouse event and line 6 evaluates y by parsing and evaluating the equation of the curve with respect to x. I then use the x and y coordinates to position my label elements.
Mathjs does not come with its own differentiation module to work out the derivative of the user entered expression but I found this plugin that seems to work out well for this task.
Armed with this module, it was plain sailing to create an equation for the tangent line that I could use to find out y values for the tangent.
constderivative=math.diff(math.parse(me.props.expression),"x");constgradient=derivative.eval({x:point.x});constyIntercept=getYIntercept(point,gradient);constlineEquation=math.parse("m * x + c");constgetTangentPoint=(delta)=>{constdeltaX=xScale.invert(x+delta);consttangentPoint={x:deltaX,y:lineEquation.eval({m:gradient,x:deltaX,c:yIntercept})};returntangentPoint;};constlength=xScale(200);consttangentPoint1=getTangentPoint(+length);consttangentPoint2=getTangentPoint(-length);g.select('.tangent').attr('x1',xScale(tangentPoint1.x)).attr('y1',yScale(tangentPoint1.y)).attr('x2',xScale(tangentPoint2.x)).attr('y2',yScale(tangentPoint2.y));
Line 1 creates a derivative function from the equation of the curve.
The derivative function is evaluted to get the gradient or slope for the US viewers on line 3.
The c constant or y-intercept of the equation of the line y = mx + c is retrieved on line 5.
Mathjs is employed to create an expression in the format of y = mx + c.
A getTangentPoint function is created that will be used to get the points at either end of the function.
Lines 24 - 26 create 2 points for x that will be far off the length and height of the svg document to give the impression of stretching off to infinity.
Each point gets its y value by calling the getTangentPoint funtion on line 9 that in turn will solve for x for the equation of the line function previously created on line 7.
Once we have the pair of points, the line can be plotted on lines 29 - 33.
I am available for work right now, if you are interested then email me directly.
Up until now when I have been dealing with d3.js’s axes components, I have always kept the axes positive, i.e. both the x and y axes where showing values greater than 0.
I have been hacking around with this fun side project that takes the input of an algebraic expression and plots a graph for a sample range of values for x. You can checkout the source code here if you are interested.
Below is a screenshot of the end result:
It quickly became apparent that in order to show the curve of the expression properly, I would need to construct negative and positive x and y axes.
I have the following function below that constructs the data of x and y coordinates to plot the curve against which uses the excellent mathjs library to transform the string algebraic expression into a javascript function (line 2). I then create a sample range for x values ranging from -10 to 11 and evaluate the y coordinate for each item in the range by applying the function on line 5 to each item:
The domain function of d3 allows you to specify a minimum and maximum value as the range of values that we can use for a particular axis.
Below I am using d3’s extent function that returns the minum and maximum values of an array and is equivalent to calling d3.min and d3.max simultaneously.
As I am supplying the values for the range of x values in the code above, I know that I will always have negative x values and positive x values.
The y coordinates are different depending on the function generated from the algebraic expression. Depending on the expression, there are basically 3 conditions I want to capture when displaying a curve.
The first case is when there are only positive y values:
The next case is when there are both negative and positive y values;
Lastly, only negative y values:
With this in mind, the code below creates a domain based on the minimum values of y and the maximum values of y:
I either start my domain at 0 or use d3.extent again to get the maximum and minimum values for y like I did before for x.
The last problem to solve was to position the x axis. In the 3 code samples below, I am capturing the domain for y and the x axis position for each condition.
This is easy if I only have negative or positive values for y. I can simply place the x axis at the bottom for only positive values:
The interesting case was when I have both positive and negative values for y.
What I ended up doing was selecting all the ticks or labels from the y axis and finding the label that had 0 against it and from that I could use d3 to to select its position and then use that for my x axis position.
In the code above, I filter out all the other ticks apart from the 0 label. The zero tick is then passed into the map function which selects the transform attribute of the tick which might look something like this translate(0,280). The second value of translate, 280 in this instance gives me the position of the 0 label in the y axis. I can use this value to position my x axis.
Once I have the position of 0 in the y axis, I can position the axis to the document:
You can change the current triangle effects by changing the radio buttons at the top. You can also drag and drop the triangle vertices by dragging the red circles at each triangle endpoint or vertex. This led to an interesting problem, which was how to how to maintain the current state or coordinates of all the shapes when the user selects a new effect from the radio buttons.
Another, more challening problem was to make sure that everything resized to the current ratio or scale if the browser window is resized. If you go to this url and resize the browser, you can see that everything re-renders nicely to scale. This does not happen out of the box. You need to code for this eventuality.
The bulk of the work takes place in the render method below:
The render function as you might expect, creates the svg document and renders all the shapes onto their specific coordinates as I outlined in the previous blog posts here and here. I use this function to both initially draw the shapes and also as the function that is attached to the resize event.
Below is the end of the render function that creates a hash that will keep track of the current state of the document or the coordinates of the all the shapes at any given time.
Lines 1 to 3 create a the hash and assign the xScale and yScaled3 scale objects that allow you to deal with a finer granuated scale than pixels. The x and y axis in the documment were created using these scale objects and you can think in terms of placing these objects at coordinates on these scales, e.g. (1, 1).
Lines 6 to 29 assign properties to this area hash such as the vertices of the triangle that will be used to read and write to when drawing the shapes. I pass this structure into most functions.
Line 31 uses the lesser known partial application properties of the bind function to create a new version of the render function. This partial function when called, will always be called with the area hash as an argument, that contains all the information we need to reconstruct the document. Line 33 adds this function to the hash, we will use this to remove the event listener each time it is called or else there will be a memory leak. LIne 35 creates an event listener for the resize event and assigns the new version of render to this event.
The beginning of the render function below uses the new es6 default paramaters feature to allow render to be called with no arguments or called from the resize event with an argument. If it is called in response to a resize event then there will be a state argument. Lines 2 to 5 remove the event listener each time it is called.
If we have a state hash then the invert method of the scale objects is used to get the value in pixels before using the scale to recreate the new x and y coordinates that are in scale with the new browser dimensions.
I also use partial application when adding the drag and drop event to the red circles at the vertices of the triangle on line 22 of the below:
Following up from my last post on how to draw the altitude of a side of a triangle through a vertex, I wanted to draw the 3 perpendicular bisectors of a triangle and the circumcircle of the triangle.
Let us get some definitions for these terms, the perpendicular bisectors of a circle are described as:
the lines passing through the midpoint of each side of which are perpendicular to the given side.
Below is a triangle with one perpendicular bisector running through side AB
.
The circumcircle of a triangle is:
The point of concurrency of the 3 perpendicular bisectors of each side of the triangle.
The centre point of the circumcircle is the point of intersection of all the perpendicular bisectors of a triangle.
Below is a triangle with all 3 perpendicular bisectors and the circumcircle drawn with d3.js.
The first step was to draw one perpendicular bisector of a triangle.
I chose 3 arbitary points for the vertices of the triangle.
This is all the information I need, to calculate the perpendicular bisectors and the circumcircle.
If I wanted to find the perpendicular bisector of AB using pen and paper, I would perform the following steps:
I would find the gradient or slope (for US readers) of the point AB.
I would then find the perpendicular gradient or slope which would give me the ratio of rise over run that the perpendicular line flows through. If lines are perpendicular then M_{1} x M_{2} = -1.
I would find the midpoint of the line using the distance formula ((x_{1} + x_{2} / 2), (y_{1} + y_{2} / 2)).
I could then plug these values into the equation of a line which takes the form of y = mx + c.
I have blogged previously in this post about how to set up the graduated x and y axis and a more managable scale for positioning vertices etc.
My first step was to find the perpendicular bisector of the line AB.
Below are two helper functions that take javascript point objects as arguments with x and y properties that map to coordinates and return either a gradient/slope or the perpendicular gradient/slope that occurrs between the 2 coordinates:
The x-intercept on line 5 is again re-arranging the equation of the line formula y = mx + c to solve for x.
The finshed function looks like this and there are a number of if statements I had to add for the conditions when the slope or gradient function might end up undefined or equalling infinity when it encounters horizontal or vertical values that have catches with the formula. I would love to know if there is an algorithm that will avoid such checks:
In order to find the centre of the circumcirle or the point of intersection of the perpendicular bisectors, the function takes two arguments lineA and lineB which are two of the perpendicular bisectors of the traingle. The function then arranges these line objects into y = mx + c format on lines 6 to 11 of the above. I then solve these equations simulataneously using matrices and specifically using cramer’s rule to find the point where the line intersect.
Once I have the 2x2 matrix assembled on lines 13-16, I then pass it to the solveMatrix function with the 2 y-intercept values that will apply cramer’s rule:
I now have the point of intersection of the perpendicular bisectors. All I need to know now is the radius of the circle. The calculation I used is to use the distance formula. From the point of intersection we just found to one of the vertices of the triangle.
Below is a helper function for the distance formuala:
I’m back at college learning the maths that I should have learned a long time ago. I am also trying to kill 2 birds with one stone by using what I’ve learned to help me learn d3.js at the same time. The task I set myself this week was to draw the altitude of a triangle through a point.
In geometry, an altitude of a triangle is a line segment through a vertex (point) and perpendicular (i.e. forming a right angle with) a line containing the base (the opposite side of the triangle). This line containing the opposite side is called the extended base of the altitude.
My first steps are to create a scale that is of much lower resolution than the finely grained pixels and below is the code that creates both the scale and the axis. I blogged about scales in more detail in my last blog post:
This is effectively a DSL or mini-language for drawing shapes.
I’ll add a translation for each line:
'M ' + a.x +' '+ a.y + - This means place a point at the x and y coordinates of the point I previously created with var a = {x: xScale(1), y: yScale(1)}. This is analgous to the starting point where you might place your pen.
' L' + b.x + ' ' + b.y + - This means draw a line created from the point created above to the point b that was declared like this b = {x: xScale(6), y: yScale(18)}. Because we are using scales, we can pick nice friendly points like (6,18) rather than the harshness of pixels.
' L' + c.x + ' ' + c.y + - draw a line to the c point
the z command closes the path.
I really like the path function as it is how a human would draw a triangle with pen and paper and is very easy to grok.
With the easy bit done, I now wanted to draw the altitude through point A that would be perpendicular to line BC.
If I was doing this with pen and paper, I would perform the following steps:
I would find the gradient (or slope for those of you from the US) of the line BC.
I would use this gradient/slope to create the equation of the line in y = mx + c format.
I would find the perpendicular gradient/slope of BCthat I can use to create an equation of the line that will go through point A and will be perpendicular to point C in y = mx + c format.
I would then solve these simultaneously to find the point of intersection from point A to the point on BC that was perpendicular to point A.
What I quickly found out was that transfering pen to paper calculations to machine instructions or javascript was extremelly difficult and different. Here are the steps I took:
Like in the pen and paper version, I found the grandient of BC and created this function:
In order to get both line equations into y = mx + c, I needed a function that would take a point and a gradient and give me the y-intercept or the point where the line cuts the y-axis:
I would use the substitution method or the addition method to solve a series of equations with pen in paper but writing it in code was a different matter and matrices seemed like the obvious fit. Please write a comment below if there is a more efficient way. There is cramer’s law which seemed ideal for my needs. I needed to get my vars into the following format:
matrix.js
12
[x1,y1][x]=[c1][x2,y2][y]=[c2]
Below is my altitude function that gets the values into matrices before passing to a function that will use cramer’s law to find the point of intersection.
I want to add drag and drop to rotate the triangle so I’ll probably need to put checks in for vertical and horizontal values for x and y but this will do for now.
Below is the end result of my troubles with the altitudes of all three vertices shown.
The hardest part was to solve the simultaneous equations and I belive there is a better and more efficient way that uses vectors to work this out but I have not covered this with my course as yet.
I’ve recently gone back to college to do a higher maths class at night and I want to use some of the concepts I am learning in my programming to enforce what I have learned so far. I’ve been meaning to learn d3 for some time so this seems like a perfect opportunity to kill two birds with one stone. I thought I would start with something simple and have a draggble line that shows the equation of a straight line with respect to the two coordinates at the end of each line. The equation of a line may still exist in the memories of your shool days.
Here is a jsbin with the product of my fumblings. You can drag the line by either of the red circles at each end and the calculations will recalculate.
Below is the end result
I will now breakdown the code:
The first code blocks create an x and a y scale that will scale from 0 to 20 units in each axis which is preferable to using the much more granular pixels. These scales are used to create the labels on the x and y axis and also make positioning elements much, much easier.
Lines 1 to 3 simply define some dimensions for the document and a margin.
Line 5 - 7 and lines 8 - 11 create an x axis and y axis respectively which will use the available width and height to spread out the 20 units of scale across each axis.
Scales are functions that map from an input domain to an output range.
Scales transform a number in a certain interval (called the domain) into a number in another interval called the range. If we look at the code used to create the x axis scale:
The code above returns a converter function and binds it to the variable xScale. We will use this function a lot to convert the x coordinate of a point from the real pixel value into the 0 - 20 output range scale we have specified. The code specifies that the scale is linear and that it has a minum value of 0 and a maximum value of 20. This is achieved by passing the array [0, 20] as an argument to the domain function on line 2 of the above. The code on line 2 then specifies the range that is used to equally space out the 0 to 20 units. In this example we are using the available width of the svg document.
The exact same logic is used to create a converter function for the y scale but this time, the height of the svg document is used as the range.
The call to d3.svg.axis on line 1 unsurprisingly returns an instance of the axis object.
Line 2 makes a call to the scale function and passes in our xScale converter reference we created earlier. The labels or ticks as they are known as in d3 speak will be graduated with respect to the [0, 20] domain array of min and max values we specified.
Line 3 positions the axis at the bottom of the document.
On line 4 the innerTickSize function of the axis object is called to create the horizontal lines that are vertically aligned from each label or tick. I struggled with a good way of creating the gridlines for a long time and it turns out you need to pass in a negarive argument like -height in line 2 of the above because we want the lines to fill the full document and if we do not pass a negative value then grid lines flow down from the labels and not up. You can see this by adding and removing the line from the jsbin.
The yAxis is created in a similar fashion only the horizontal grid lines are creating by passing in -width into innerTickSize to create the horizontal lines.
Positioning the line and circles was very easy after I had my grid created and my scales set up. The line is created below:
Line 6 creates an svg g element or container that can be used to group shapes together.
Lines 11 to 14 specify the start and end points of the line. What is important about this is that the xScale and yScale converter functions are used to place the line on the 0..20 scale.
The circle and text label objects that specify the coordinates of the end points follow a similar approach:
What is interesting about the above code is the data function call on lines 5 and 16. I liken this to data binding and a circle or text object is created for each element in the lineData array on line 1.
What I found confusing about the above code when I first encountered it is that selectAll works on elements that you are going to create rather than what you have created. The enter function on lines 6 and 17 is used to add the new elements to the document as the data changes.
All that is left is to display the code that handles the drag events:
The above code creates a drag function that I can attach to elements of the document.
I struggled for a while with getting the correct text for labels of the coordinates of the end points of the line and the main header label. The drag event handler on line 3 gets passed a d argument which is a d3 behaviour object that has the x and y mouse cooridinates of where the mouse was dragged to. I had to use xScale.invert and yScale.invert to get the correct scaled cooridinates for the text labels. The invert function returns the inverse mapping of range to domain. The rest of the code just positions the elements to the new mouse positions.
The drag function is then attached to each circle as it is created on the last line of the code below;
Rendering long lists of tabular data on the client is something I have spent a lot of time on and this dates back to my backbone days. You need to at the very least employ infinite scrolling which opens up other cans of worms. What if my tabular data has a cell with a link and I click the link and then click the back button. Unless I spend a lot of engineering effort, I will have lost my place in the infinitely scrolled list.
This was a problem I recently faced on the application that I am working on. We have a large table of infinitely scrolled rows of contact data that had links to the detail of each contact on each row. We started off with the links redirecting to a new route that displayed the individual contact’s details. This meant that when you clicked the back button from the detail view, the table would be re-renderd from scratch.
I came up with quite a nice and not so technical solution that avoided many dark hours of development. I came up with the idea of sliding out the view of the contact detail from the same route that contains the table. This means the user never loses their place in the table. Throw in some css3 animations and I am very happy with the solution. Below is an animated gif that shows what I have christened the x-drawer component:
Here is a working jsbin which shows the code in action. The x-drawer component is a container for other components and is declared in its nested form and any component can be placed inside it:
I actually use the component helper in the production app as I now use the drawer a lot but in the above gist I simply have a property showDetail that when set to true will activate the drawer.
The x-drawer component is actually very simple and the sliding animation is achieved with css3 animations and specifically css transitons that enables the drawer to slide out slowly rather than just appear. I used a combination of translateX and transform to slide out the view.
Another requirement that I had thrust apon me was to make sure the drawer was closed if the user clicked anywhere outside of the drawer. Below is the code that creates an overlay on the remaining screen real estate that when clicked sends the action to close the drawer:
This was actually quite a simple solution to a difficult problem. My first thoughts where that I need to work on the performance of rendering the table but with this solution, the table is always present as we don’t have to naviagate away.
Why should you care about functional javascript? I’d heard of the merits of functional programming for a while now and up until recently, I thought that the main benefits of functional programming where really only applicable when dealing with highly concurrent programs when functional programming can help guarantee thread safety. Writing side effect free functions that use immutable data structures rules out interaction problems between threads that mutate data. All well and good but why should I care about any of this in the single threaded realm of the browser?
The pendulum between server heavy app and client heavy app. has swung back to the client again for this epoch and single page applications are all the rage. This would have seemed like insanity at the turn of the century but advances in browser technology and a demand for more ambitious applications has led us to the now infamous SPA. Our programs live a lot longer in the browser than they used to. Gone are the days when we could rely on a browser refresh to reboot the browser code and rebuild clear all our objects from memory. reactjs has come up with a beautfiul top down, one way data push that can simulate this behaviour by abstracting away that gargantuan side effect known as the DOM into a virtual DOM to diff against the previous state. Reactjs has embraced functional programming and so should you.
Side effects, function Purity and Referential Transparency
Functional programming favours functions that, for a given input, will consistently return the same output. A pure function does not depend on and does not modify the state of variables outside of its scope. When a function performs any other action, apart from calculating its return value, the function is impure. A program or function is said to have side effects if it can produce different outputs given the same input.
A very simple example of a pure function is below:
mult.js
1
constmult=(a,b)=>a*b;
Referential transparency is the ability to freely replace an expression with its value and not change the behaviour of the program. We can show that the mult function is both pure and referentially transparent with the code below:
We can clearly see that providing the same input each time does not give us the same output each time.
Side effects are functions that rely on the outside OR can be functions that alter the outside world. The ubiquitous Hello World program is actually a side effect because it alters the state of the output on the screen. A call to console.log is a side effect. All IO is side effect laden, reading a file from a disk is a side effect because it might throw an exception because the network is unavailable. We therefore cannot guarantee the result of calling the function.
Any function that does not return a value will more than likely have side effects because it is probably mutating some external state.
Our programs would not be very practical without side effects and the goal of functional programming is not to eliminate side effects but instead we want to limit them and also more importantly, isolate them. How can we limit side effects in the sea of mutation that is javascript? The first thing we can do is to use immutable data structrues.
Immutable Data Structures
I think the penny is starting to drop with many in the javascript world about how sensible immutable data structures are. A data structure is immutable if it cannot be changed after it has been created.
In the chaotic realm of the single page application, I find it very reassuring to know that after I have created an object or assigned a value to a variable, it will not be changed by some well meaning code outside of my control. A lot of SPA frameworks use two way data binding to bind objects to DOM elements or other structures. I have also had hellish results with using observation through Object.observe or some other mechanism of reacting to state changes, I now consider this to be an anti-pattern.
es6 has introduced the const keyword which is analogous to final in java, it does not mean that all data structures defined with the const keyword are immuable but it does mean they cannot be reassigned once they have been assigned. The code sample below will illustrate this:
const.js
1234567891011
constnumber=3;number=4;// => error: Attempting to override 'number' which is a constant.constperson={name:'Paul Cowan'};person.name='Bob Cowan';// mutate awayconsole.log(person.name);person={name:'Paul Cowan'};//=> error: "person" is read-only
If we want full immutability then we can use something like immutable-js or mori. Both libraries provide persistent data structures for your use. The data structures are called persistent because they always presist a previous version of themselves when modified.
Below is an example of immutable-js that illustrates this:
immutable.js
12345678910111213141516
letlist1=Immutable.List.of(1,2);letlist2=Immutable.List.of(1,2);console.log(Immutable.is(list1,list2)===true);// => true, equality by value and not referenceletmap1=Immutable.Map({a:1,b:2,c:3});letmap2=map1.set('b',2);console.log(map1===map2);// => true, map2 returns a reference to map1 because no change was madeletmap3=map1.set('b',50);console.log(map1!==map3);console.log(map1.toJS());//=> {a: 1, b: 2, c: 3}, map1 is unchangedconsole.log(map3.toJS());//=> {a: 1, b: 50, c: 3}
You can see from the above that we can check equality by value and not reference on line4 when comparing list1 and list2.
Also illustrated is how map2 returns a reference to map1 on line 7 because no change was made to the underlying hash and on line 11, map1 remains unchanged and a new version of the data structure is returned after changing or adding a new value to part of the data strurcture. I hope you can see how we could use persistent data structures to trivially hookup undo and redo functionality (more on this later).
Statelessness
Imperative programming works by changing programming state to produce answers. Conversely, functional programming produces answers through stateless operations.
This brings us back to function purity, our programs are easier to reason about if we can be sure that given the same input, we get the same output. We want to avoid functions that reference member variables or any thing outside of the functions environment wherever possible.
Back to the Real world
Here are some things to consider everytime you create a function in javascript:
Are my functions dependant on the context in which they are called, or are they pure and independent?
Can I write these functions in such a way that I could depend on them always returning the same result for a given input?
Am I sure that these functions won’t modify anything outside of themselves?
Pass values as parameters, don’t store state in member variables.
I blogged here here about using a real world example to limit side effects in an emberjs application and here about how you can use persistent data structures in an emberjs application to implement basic undo and redo functionality.
Higher Order Functions
A higher order function is either:
A function that takes a function as an argument
A function that returns a function
I am going to concentrate on the latter and I am going to first of all use partial application.
Wikipedia gives the following definition of partial application:
partial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity
If we return to our mult function that I showed in the beginning, we can create a new function double by using partial application:
On line 3 of the above, I am using the lesser known property of bind that after the first argument where you can specify a context, every parameter after the first will be prepended to the list of parameters when invoking the bound function.
We could rewrite the double to return a function like this:
We now have a makeMultiplier that can create many different types of functions based on mult.
A more practical use of a higher order function is the maybe function below that can take a function and return a null safe version:
maybe.js
123456789101112131415161718192021
constmaybe=(fn)=>{return(input)=>{if(!input){return;}returnfn.call(this,input);};};consttoLower=i=>i.toLowerCase();console.log(toLower("henry"));letnullString;console.log(toLower(nullString));// TypeError: Cannot read property 'toLowerCase' of undefinedconstnullSafeToLower=maybe(toLower);console.log(nullSafeToLower(nullString));// returns undefined, no exception
Here we create a new null save function that is returned from the maybe function. We can now create other functions from this maybe function that will handle null values.
Composability
Another benefit of higher order functions is to take 2 or more functions and combine them into one. Below is an and function that will perform a logical and on the output of two functions: