Hyperbolic Tree

Sets of classes and objects to plot and calculate a hyperbolic tree.

Summary
Sets of classes and objects to plot and calculate a hyperbolic tree.
A multi-purpose Canvas object decorator.
Canvas initializer.
Canvas context handler.
Calculates canvas absolute position on HTML document.
Canvas absolute position to the HTML document.
Clears the canvas object.
Draws the boundary circle for the Hyperbolic Tree.
Translates canvas coordinates system to the center of the canvas object.
An object that contains the canvas width and height.
A multi-purpose Mouse class.
Returns mouse position relative to canvas.
Captures mouse position.
The HyperLine class allows you to draw “lines” on the Poincare’s disk model for Hyperbolic Geometry.
HyperLine constructor.
Draws the HyperLine on canvas.
For private use only: corrects angle value based on quadrant.
For private use only: sets angle direction to clockwise (true) or counterclockwise (false).
For private use only.
A multi-purpose Complex Class with common methods.
Complex constructor.
Calculates the complex norm.
Calculates the complex squared norm.
Returns the result of adding two complex numbers.
Returns the result of multiplying two complex numbers.
Calculates a moebius transformation for this point / complex.
Returns the conjugate por this complex.
Returns the result of scaling a Complex instance.
Returns a string that shows the Complex properties.
Behaviour of the hyperbolic tree node.
Node constructor.
Node flag
node distance to center
allowed angle span for adjacencies placement
node position
additional node information
node adjacencies
Indicates if the node is adjacent to the node indicated by the specified id
Connects the node to the specified by id.
Creates a label containing the root node information.
Creates a label containing the node information.
Plots the node on canvas.
Intended for private use: sets some label properties, such as positioning and className.
Calculates node’s distance to origin.
Hyperbolic Tree configuration object.
id for label container
show/hide main circle
show/hide nodes
show/hide edges
show/hide labels, must be set to false if the nodes have no additional info.
initial edge length
angle span rate
time slot for animation frame
for non-JSON generated trees: tree depth
for non-JSON generated trees: number of children
for non-JSON generated trees: probability of edge creation
for non-JSON generated trees: customizeable array of nodes per depth level
Hyperbolic Tree (HT) class.
Creates a new HT instance.
Connects nodes specified by id1 and id2.
Adds a node.
Returns a Boolean instance indicating if node belongs to graph or not.
Loads parameters and places the HT.
Calculates a moebius transformation for the hyperbolic tree.
Intended for private use: calculates node positions on canvas by performing a BFS-like algorithm
Intended for private use: calculates positions for children and grandchildren of root node.
Prints graph / tree edges
Tranlates the HT to a given position
For private use: sets a flag.
Used for drawing the HT on a frame
Prints the HT on canvas
Prints the HT nodes after being translated to a relative position.
Creates and displays labels for the centered node and its adjacent nodes.
Translates the HT to the mouse position.
Loads an HT with the depth, children and prob parameters stored in the Config object.
Loads an HT with the loadTreeArray parameter stored in the Config object.
Returns a node info property.
To be set by user: execute this function when performing an event to the label.
In this section you’ll find the objects that you must use in order to make a Canvas or HyperbolicTree instance.
Use this object to store a new Canvas instance.
Use this object to store a new HT instance.

Canvas

A multi-purpose Canvas object decorator.

Summary
Canvas initializer.
Canvas context handler.
Calculates canvas absolute position on HTML document.
Canvas absolute position to the HTML document.
Clears the canvas object.
Draws the boundary circle for the Hyperbolic Tree.
Translates canvas coordinates system to the center of the canvas object.
An object that contains the canvas width and height.

Functions

Canvas

var Canvas= function (canvasId,
fillStyle,
strokeStyle)

Canvas initializer.

Parameters

canvasIdThe canvas tag id.
fillStyle(optional) fill color style.  Default’s to black
strokeStyle(optional) stroke color style.  Default’s to black

Returns

A new Canvas instance.

getContext

getContext: function ()

Returns

Canvas context handler.

setPosition

setPosition: function()

Calculates canvas absolute position on HTML document.

getPosition

getPosition: function()

Returns

Canvas absolute position to the HTML document.

clear

clear: function ()

Clears the canvas object.

drawMainCircle

drawMainCircle: function ()

Draws the boundary circle for the Hyperbolic Tree.

translateToCenter

translateToCenter: function()

Translates canvas coordinates system to the center of the canvas object.

getSize

getSize: function ()

Returns

An object that contains the canvas width and height. i.e.  { x: canvasWidth, y: canvasHeight }

Mouse

A multi-purpose Mouse class.

Summary
Returns mouse position relative to canvas.
Captures mouse position.

Functions

getPosition

getPosition: function (canvas)

Returns mouse position relative to canvas.

Parameters

canvasA canvas object.

Returns

A Complex instance representing the mouse position on the canvas.

capturePosition

capturePosition: function(e)

Captures mouse position.

Parameters

eTriggered event.

HyperLine

The HyperLine class allows you to draw “lines” on the Poincare’s disk model for Hyperbolic Geometry.  You just have to specify two Complex numbers that will be used for drawing the line.

Summary
HyperLine constructor.
Draws the HyperLine on canvas.
For private use only: corrects angle value based on quadrant.
For private use only: sets angle direction to clockwise (true) or counterclockwise (false).
For private use only.

Functions

HyperLine

var HyperLine= function(p1,
p2)

HyperLine constructor.

Parameters

p1A Complex representing one point on the Hyperbolic Plane.
p2Another Complex representing one point on the Hyperbolic Plane.

Returns

A new HyperLine instance.

printOn

printOn: function(canvas)

Draws the HyperLine on canvas.

Parameters

canvasA Canvas object.

correctAngle

correctAngle: function(relTo,
dot,
angle)

For private use only: corrects angle value based on quadrant.

Parameters

relToA Complex instance pointing to the origin of one circle.
dotAn arbitrary point on Canvas.
angleThe angle between relTo and dot.

Returns

The corrected angle.

sense

sense: function(angleBegin,
angleEnd)

For private use only: sets angle direction to clockwise (true) or counterclockwise (false).

Parameters

angleBeginStarting angle for drawing the arc.
angleEndThe HyperLine will be drawn from angleBegin to angleEnd.

Returns

A Boolean instance describing the sense for drawing the HyperLine.

arcThroughTwoPoints

arcThroughTwoPoints: function()

For private use only.  Calculates the line equation that contains p1 and p2 over the poincare disk model of hyperbolic geometry.  For more information go to: http://en.wikipedia.org- /wiki- /Poincar%C3%A9_disc_model

Returns

A set of properties describing univoquely a Hyperbolic Line.  For more information on these properties please refer to the wikipedia page: http://en.wikipedia.org- /wiki- /Poincar%C3%A9_disc_model

Complex

A multi-purpose Complex Class with common methods.

Summary
Complex constructor.
Calculates the complex norm.
Calculates the complex squared norm.
Returns the result of adding two complex numbers.
Returns the result of multiplying two complex numbers.
Calculates a moebius transformation for this point / complex.
Returns the conjugate por this complex.
Returns the result of scaling a Complex instance.
Returns a string that shows the Complex properties.

Functions

Complex

var Complex= function()

Complex constructor.

Parameters

reA real number.
imAn real number representing the imaginary part.

Returns

A new Complex instance.

norm

norm: function ()

Calculates the complex norm.

Returns

A real number representing the complex norm.

squaredNorm

squaredNorm: function ()

Calculates the complex squared norm.

Returns

A real number representing the complex squared norm.

add

add: function(pos)

Returns the result of adding two complex numbers.  Does not alter the original object.

Parameters

posA Complex initialized instance.

Returns

The result of adding two complex numbers.

prod

prod: function(pos)

Returns the result of multiplying two complex numbers.  Does not alter the original object.

Parameters

posA Complex initialized instance.

Returns

The result of multiplying two complex numbers.

moebiusTransformation

moebiusTransformation: function(theta,
c)

Calculates a moebius transformation for this point / complex.  For more information go to: http://en.wikipedia.org- /wiki- /Moebius_transformation.

Parameters

thetaA real number representing a rotation angle.
cAn initialized Complex instance representing a translation Vector.

conjugate

conjugate: function()

Returns the conjugate por this complex.

Returns

The conjugate por this complex.

scale

scale: function(factor)

Returns the result of scaling a Complex instance.  Does not alter the original object.

Parameters

factorA scale factor.

Returns

The result of scaling this complex to a factor.

toString

toString: function ()

Returns a string that shows the Complex properties.

Returns

A string that shows the Complex properties.

Node

Behaviour of the hyperbolic tree node.

Summary
Node constructor.
Node flag
node distance to center
allowed angle span for adjacencies placement
node position
additional node information
node adjacencies
Indicates if the node is adjacent to the node indicated by the specified id
Connects the node to the specified by id.
Creates a label containing the root node information.
Creates a label containing the node information.
Plots the node on canvas.
Intended for private use: sets some label properties, such as positioning and className.
Calculates node’s distance to origin.

Functions

Node

function Node (id,
info)

Node constructor.

Parameters

idThe node id.
infoPlace to store extra information (can be left to null).

Returns

A new Node instance.

Properties

id

drawn

Node flag

initLength

node distance to center

angle span

allowed angle span for adjacencies placement

pos

node position

info

additional node information

adjacencies

node adjacencies

Functions

adjacentTo

adjacentTo: function(id)

Indicates if the node is adjacent to the node indicated by the specified id

Parameters

idA node id.

Returns

A Boolean instance indicating whether this node is adjacent to the specified by id or not.

addAdjacency

addAdjacency: function(id)

Connects the node to the specified by id.

Parameters

idA node id.

printInfoAsRootOn

printInfoAsRootOn: function (id,
canvas)

Creates a label containing the root node information.  This works by creating a div containing the node’s info property.  The div has ht_rootLabel as CSS className.  Finally, the node is placed near the canvas plotted node.

Parameters

idThe label container id.
canvasA Canvas instance.

printInfoOn

printInfoOn: function (id,
canvas)

Creates a label containing the node information.  This works by creating a div containing the node’s info property.  The div has ht_label as CSS className.  Finally, the node is placed near the canvas plotted node.

Parameters

idThe label container id.
canvasA Canvas instance.

printOn

printOn: function(canvas)

Plots the node on canvas.

Parameters

canvasA Canvas instance.

setDivProperties

setDivProperties: function(cssClass,
canvas,
key)

Intended for private use: sets some label properties, such as positioning and className.

Parameters

cssClassA class name.
canvasA Canvas instance.
keyA label (or node) id.

norm

norm: function ()

Calculates node’s distance to origin.

Returns

A Number instance indicating the node’s distance to origin.

Config

Hyperbolic Tree configuration object.  This object must be passed when calling a new hyperbolic tree instance:

var ht= new HT(Config, canvas);

The top 9 parameters (animationTime included) are necessary to execute the HT algorithm properly.

Summary
id for label container
show/hide main circle
show/hide nodes
show/hide edges
show/hide labels, must be set to false if the nodes have no additional info.
initial edge length
angle span rate
time slot for animation frame
for non-JSON generated trees: tree depth
for non-JSON generated trees: number of children
for non-JSON generated trees: probability of edge creation
for non-JSON generated trees: customizeable array of nodes per depth level

Properties

labelContainer

id for label container

drawMainCircle

show/hide main circle

showNodes

show/hide nodes

showEdges

show/hide edges

showLabels

show/hide labels, must be set to false if the nodes have no additional info.

initLength

initial edge length

angleRate

angle span rate

timeSlot

time slot for animation frame

animationTime

depth

for non-JSON generated trees: tree depth

children

for non-JSON generated trees: number of children

prob

for non-JSON generated trees: probability of edge creation

loadTreeArray

for non-JSON generated trees: customizeable array of nodes per depth level

HT

Hyperbolic Tree (HT) class.  There are three ways of generating a HT object.  You can use depth, children and prob properties of the Config object to define a HT.  You can also use the loadTreeArray property of the Config object to define each level of the tree.  Finally, you can load a HT with a JSON array.

Summary
Creates a new HT instance.
Connects nodes specified by id1 and id2.
Adds a node.
Returns a Boolean instance indicating if node belongs to graph or not.
Loads parameters and places the HT.
Calculates a moebius transformation for the hyperbolic tree.
Intended for private use: calculates node positions on canvas by performing a BFS-like algorithm
Intended for private use: calculates positions for children and grandchildren of root node.
Prints graph / tree edges
Tranlates the HT to a given position
For private use: sets a flag.
Used for drawing the HT on a frame
Prints the HT on canvas
Prints the HT nodes after being translated to a relative position.
Creates and displays labels for the centered node and its adjacent nodes.
Translates the HT to the mouse position.
Loads an HT with the depth, children and prob parameters stored in the Config object.
Loads an HT with the loadTreeArray parameter stored in the Config object.
Returns a node info property.
To be set by user: execute this function when performing an event to the label.

Functions

HT

var HT= function(config,
canvas)

Creates a new HT instance.

Parameters

configThe Configuration object Config.
canvasA Canvas instance.

addAdjacence

addAdjacence: function (id1,
id2)

Connects nodes specified by id1 and id2.  If not found, nodes are created.

Parameters

id1Node’s id.
id2Another Node’s id.

addNode

addNode: function(id,
info)

Adds a node.

Parameters

idNode’s id.
infoSome extra information you might find useful.

hasNode

hasNode: function(id)

Returns a Boolean instance indicating if node belongs to graph or not.

Parameters

idNode’s id.

Returns

A Boolean instance indicating if node belongs to graph or not.

initialize

initialize: function ()

Loads parameters and places the HT.

moebiusTransformation

moebiusTransformation: function(theta,
c)

Calculates a moebius transformation for the hyperbolic tree.  For more information go to: http://en.wikipedia.org- /wiki- /Moebius_transformation

Parameters

thetaRotation angle.
cTranslation Complex.

calculatePositions

calculatePositions: function(id)

Intended for private use: calculates node positions on canvas by performing a BFS-like algorithm

Parameters

idRoot node id.

calculateChildrenPositions

calculateChildrenPositions: function(queue)

Intended for private use: calculates positions for children and grandchildren of root node.

Parameters

queueA queue (or Array instance) containing the root node.

printEdges

printEdges: function()

Prints graph / tree edges

translateTo

translateTo: function(complexTo,
millisec)

Tranlates the HT to a given position

Parameters

complexToA Complex instance indicating where to translate the HT.
millisecAnimation time.

setFlag

setFlag: function (val)

For private use: sets a flag.

Parameters

valValue.

printNodesAt

printNodesAt: function(at)

Used for drawing the HT on a frame

Parameters

atA Complex instance indicating where to move the HT before plotting its nodes.

printOn

printOn: function(canvas)

Prints the HT on canvas

Parameters

canvasA Canvas instance.

printNodesRelativeAt

printNodesRelativeAt: function(at)

Prints the HT nodes after being translated to a relative position.

Parameters

atA Complex instance indicating where to relatively move the HT before plotting its nodes.

makeLabelsForCenteredNode

makeLabelsForCenteredNode: function ()

Creates and displays labels for the centered node and its adjacent nodes.

translate

translate: function (Mouse)

Translates the HT to the mouse position.  It can also be used with any other Object / Class implementing a Mouse-like interface.

Parameters

MouseThe Mouse object.

loadTree

loadTree: function ()

Loads an HT with the depth, children and prob parameters stored in the Config object.

loadTreeByArray

loadTreeByArray: function ()

Loads an HT with the loadTreeArray parameter stored in the Config object.

loadTreeByJSON

loadTreeByJSON: function (json_array)

Loads the HT with a JSON array of the form

{"InfoRootNode": {"InfoFirstChild": ["InfoGrandChild1", ..., "InfoGrandChildN"], ..., "InfoLastChild": ["InfoGrandChild1", ..., "InfoGrandChildN"]}}

Parameters

json_arrayA JSON array.

getNodeInfoById

getNodeInfoById: function(ide)

Returns a node info property.

Parameters

ideA node id.

getNodeInfoById

To be set by user: execute this function when performing an event to the label.  Receives the label id.  This label id is also the node id, so feel free to fetch the node information by calling HT.getNodeInfoById

Parameters

ideThe label id.

Objects

In this section you’ll find the objects that you must use in order to make a Canvas or HyperbolicTree instance.  There’s three ways of doing this:

  • Loading a HT with specified depth (Config.depth), children (Config.children) and probability of edge creation Config.prob.  Each level will be completed with the same amount of nodes.  Take for example:
canvas= new Canvas('hypertree', 'white', 'white');
canvas.setPosition();
canvas.translateToCenter();
//use a cross-browser event handler function
addEvent(canvas.canvas, 'click', function (e) { Mouse.capturePosition(e); ht.translate(Mouse); });

ht= new HT(Config, canvas);
ht.loadTree();
canvas= new Canvas('hypertree', 'white', 'white');
canvas.setPosition();
canvas.translateToCenter();
addEvent(canvas.canvas, 'click', function (e) { Mouse.capturePosition(e); ht.translate(Mouse); });

ht= new HT(Config, canvas);
ht.loadTreeByArray();
  • Loading a HT from a JSON array that displays some extra-information.  In order to do this you must set a handler function(<HT.labelHandler>) that will be called onclick.
function    labelHandler(ide) {
var content= ht.getNodeInfoById(ide).replace(/ /g, '+');
//This function could make an AJAX call to
//grab some JSON information somewhere
loadFromJSON(content);
}


//when document loads...
function initOnLoad(content) {
canvas= new Canvas('hypertree', 'white', 'white');
canvas.setPosition();
canvas.translateToCenter();
addEvent(canvas.canvas, 'click', function (e) { Mouse.capturePosition(e); ht.translate(Mouse); });

ht= new HT(Config, canvas);
ht.labelOnEventHandler= labelHandler;


loadFromJSON(content);

}
Summary
Use this object to store a new Canvas instance.
Use this object to store a new HT instance.

Variables

canvas

var canvas

Use this object to store a new Canvas instance.

ht

var ht

Use this object to store a new HT instance.

var Canvas= function (canvasId,
fillStyle,
strokeStyle)
Canvas initializer.
getContext: function ()
Canvas context handler.
setPosition: function()
Calculates canvas absolute position on HTML document.
getPosition: function()
Canvas absolute position to the HTML document.
clear: function ()
Clears the canvas object.
drawMainCircle: function ()
Draws the boundary circle for the Hyperbolic Tree.
translateToCenter: function()
Translates canvas coordinates system to the center of the canvas object.
getSize: function ()
An object that contains the canvas width and height.
getPosition: function (canvas)
Returns mouse position relative to canvas.
capturePosition: function(e)
Captures mouse position.
var HyperLine= function(p1,
p2)
HyperLine constructor.
printOn: function(canvas)
Draws the HyperLine on canvas.
correctAngle: function(relTo,
dot,
angle)
For private use only: corrects angle value based on quadrant.
sense: function(angleBegin,
angleEnd)
For private use only: sets angle direction to clockwise (true) or counterclockwise (false).
arcThroughTwoPoints: function()
For private use only.
var Complex= function()
Complex constructor.
norm: function ()
Calculates the complex norm.
squaredNorm: function ()
Calculates the complex squared norm.
add: function(pos)
Returns the result of adding two complex numbers.
prod: function(pos)
Returns the result of multiplying two complex numbers.
moebiusTransformation: function(theta,
c)
Calculates a moebius transformation for this point / complex.
conjugate: function()
Returns the conjugate por this complex.
scale: function(factor)
Returns the result of scaling a Complex instance.
toString: function ()
Returns a string that shows the Complex properties.
function Node (id,
info)
Node constructor.
adjacentTo: function(id)
Indicates if the node is adjacent to the node indicated by the specified id
addAdjacency: function(id)
Connects the node to the specified by id.
printInfoAsRootOn: function (id,
canvas)
Creates a label containing the root node information.
printInfoOn: function (id,
canvas)
Creates a label containing the node information.
printOn: function(canvas)
Plots the node on canvas.
setDivProperties: function(cssClass,
canvas,
key)
Intended for private use: sets some label properties, such as positioning and className.
norm: function ()
Calculates node’s distance to origin.
var HT= function(config,
canvas)
Creates a new HT instance.
addAdjacence: function (id1,
id2)
Connects nodes specified by id1 and id2.
addNode: function(id,
info)
Adds a node.
hasNode: function(id)
Returns a Boolean instance indicating if node belongs to graph or not.
initialize: function ()
Loads parameters and places the HT.
moebiusTransformation: function(theta,
c)
Calculates a moebius transformation for the hyperbolic tree.
calculatePositions: function(id)
Intended for private use: calculates node positions on canvas by performing a BFS-like algorithm
calculateChildrenPositions: function(queue)
Intended for private use: calculates positions for children and grandchildren of root node.
printEdges: function()
Prints graph / tree edges
translateTo: function(complexTo,
millisec)
Tranlates the HT to a given position
setFlag: function (val)
For private use: sets a flag.
printNodesAt: function(at)
Used for drawing the HT on a frame
printOn: function(canvas)
Prints the HT on canvas
printNodesRelativeAt: function(at)
Prints the HT nodes after being translated to a relative position.
makeLabelsForCenteredNode: function ()
Creates and displays labels for the centered node and its adjacent nodes.
translate: function (Mouse)
Translates the HT to the mouse position.
loadTree: function ()
Loads an HT with the depth, children and prob parameters stored in the Config object.
Hyperbolic Tree configuration object.
loadTreeByArray: function ()
Loads an HT with the loadTreeArray parameter stored in the Config object.
loadTreeByJSON: function (json_array)
getNodeInfoById: function(ide)
Returns a node info property.
var canvas
Use this object to store a new Canvas instance.
A multi-purpose Canvas object decorator.
var ht
Use this object to store a new HT instance.
Hyperbolic Tree (HT) class.
A multi-purpose Mouse class.
for non-JSON generated trees: tree depth
for non-JSON generated trees: number of children
for non-JSON generated trees: probability of edge creation
for non-JSON generated trees: customizeable array of nodes per depth level