Saturday, January 21, 2017

Convert triples to random x,y

Code (for a unit area of one):

var triples = [["<subject>", "<predicate>", "<object>"],["<subject-one>", "<predicate-two>", "<object-two>"]];
var accumulator = [];

for(var i = 0; i < triples.length; i++) {
 var pointstwo = triples[i].map(function(elem) {
    return [Math.random(), Math.random()];
 });
 Array.prototype.push.apply(accumulator, pointstwo);
}

console.log("The accumulator is");
console.log(accumulator);


Output:

The accumulator is
[ [ 0.27492627189257113, 0.9965854825881204 ],
  [ 0.9587315529457798, 0.005962501576601609 ],
  [ 0.8799835293320759, 0.041557371975880164 ],
  [ 0.2877037483279361, 0.7579722486957656 ],
  [ 0.4981034461408851, 0.6805552990524384 ],
  [ 0.2785271855960343, 0.4624418653391791 ] ]


Also, here is code considering an area with a specified width and height:

var triples = [["<subject>", "<predicate>", "<object>"],["<subject-one>", "<predicate-two>", "<object-two>"]];
var accumulator = [];
var width = 400;
var height = 400;

for(var i = 0; i < triples.length; i++) {
 var pointstwo = triples[i].map(function(elem) {
    return [Math.random() * width, Math.random() * height];
 });
 Array.prototype.push.apply(accumulator, pointstwo);
}

console.log("The accumulator is");
console.log(accumulator);

Output:

The accumulator is
[ [ 275.6366907510565, 164.99802199836947 ],
  [ 137.25395893793467, 248.2564004659154 ],
  [ 168.47855923177502, 189.31625113698917 ],
  [ 208.77813503610395, 240.8037786584658 ],
  [ 156.17979915445898, 6.730482159676576 ],
  [ 370.5337310559533, 333.75239835510445 ] ]



No comments:

Post a Comment