Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added helper function that calulates graph element position in 2d space
- Loading branch information
1 parent
ab8d861
commit 8f31bfa
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"use strict"; | ||
if (typeof define !== 'function') { var define = require('amdefine')(module) } | ||
|
||
/** | ||
* Helper function used to calculate the pixel position of a graph vertex | ||
* (identified by its numerical id, starting at 1) on a 2D plane | ||
*/ | ||
|
||
define([], function() { | ||
var calculatePosition = function(id, options) { | ||
var position = {}; | ||
var rowsize = options.width / options.rows; | ||
var colsize = options.height / options.columns; | ||
position.x = ((id - 1) % options.rows) * rowsize; | ||
position.y = Math.floor((id - 1) / options.columns) * colsize; | ||
return position; | ||
}; | ||
return calculatePosition; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"use strict"; | ||
|
||
var calculatePosition = require("../../lib/helper/calculatePosition"); | ||
|
||
describe("calculatePosition", function() { | ||
it("places item 8 at 448,0", function() { | ||
var options = { | ||
width: 640, | ||
height: 500, | ||
rows: 10, | ||
columns: 10 | ||
}; | ||
var position = calculatePosition(8, options); | ||
expect(position.x).toEqual(448); | ||
expect(position.y).toEqual(0); | ||
}); | ||
|
||
it("places item 10 at 576,0", function() { | ||
var options = { | ||
width: 640, | ||
height: 500, | ||
rows: 10, | ||
columns: 10 | ||
}; | ||
var position = calculatePosition(10, options); | ||
expect(position.x).toEqual(576); | ||
expect(position.y).toEqual(0); | ||
}); | ||
|
||
it("places item 11 at 0,50", function() { | ||
var options = { | ||
width: 640, | ||
height: 500, | ||
rows: 10, | ||
columns: 10 | ||
}; | ||
var position = calculatePosition(11, options); | ||
expect(position.x).toEqual(0); | ||
expect(position.y).toEqual(50); | ||
}); | ||
}); |