Skip to content

Commit

Permalink
added helper function that calulates graph element position in 2d space
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelkiessling committed Apr 2, 2012
1 parent ab8d861 commit 8f31bfa
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/helper/calculatePosition.js
@@ -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;
});
41 changes: 41 additions & 0 deletions spec/helper/calculatePosition.spec.js
@@ -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);
});
});

0 comments on commit 8f31bfa

Please sign in to comment.