javascript - 如何在屏幕上将对象分组到网络中?

标签 javascript algorithm artificial-intelligence

我正在模拟思考和内存过程,我有各种图片(牛、飞机、橙子)需要在屏幕上以类似思维导图的方式显示。每个对象也与其他三个对象相连,并且它需要看起来像一张网。

我可以使用什么算法?另外,我正在使用 JS,但伪代码或解释也很好。

最佳答案

  • 首先将您的数据放入可用结构中:
  • 按顺序绘制节点,同时绘制 ID 大于当前节点的兄弟节点。
  • 您需要围绕当前节点以 120 度偏移绘制您的 sibling 以制作您的网(3 个 sibling ,360/3 = 120)。

一些 JavaScript 来说明。

// initialize your data
var nodes = {
    1: {src: "imageA", siblings: [2,3,4]},
    2: {src: "imageB", siblings: [1,5,6]},
    3: {src: "imageC", siblings: [1,7,8]},
    4: {src: "imageD", siblings: [1,9,10]},
    5: {src: "imageE", siblings: [2]},
    6: {src: "imageF", siblings: [2]},
    7: {src: "imageG", siblings: [3]},
    8: {src: "imageH", siblings: [3]},
    9: {src: "imageI", siblings: [4]},
    10: {src: "imageJ", siblings: [4]},
}

// initialize some constats we will use
var DIST = 200; //pixel distance between images
var IMGW = 64; // image width
var IMGH = 64; // image height
var SCX = 400; // center screen x position
var SCY = 400; // center screen y position
var DEGSSTART = 90; // starting degrees offset
var DEGFLIP = 180; // add to flip the direction
var DEGSTEP = 120; // circle 360 / 3
// note: if you have more than 3 siblings change DEGSTEP appropriately

// the main function
function drawWeb(nodes, id, cx, cy, prevDeg) {
 var node = nodes[id];
 // draw the current node/image
 var xOff = cx - (IMGW / 2);
 var yOff = cy - (IMGH / 2);
 drawImage(node.src, Math.round(xOff), Math.round(yOff));
 // draw the siblings recursively
 var newDeg = prevDeg + DEGFLIP + DEGSTEP; 
 for(var i=0; i<node.siblings.length; i++) {
  var newId = node.siblings[i];
  if(newId > id) {
   // convert to radians and calc new location
   var rad = newDeg * Math.PI / 180;
   var newCX = cx + DIST * Math.cos(rad);
   var newCY = cy + DIST * Math.sin(rad);
   drawWeb(nodes, newId, newCX, newCY, newDeg);
   newDeg += DEGSTEP;
  }
 }
}

// the draw function you can customize
// use jquery or some other method
function drawImage(src, x, y) {
 // fill in code to put an image on your screen
 console.log(src + ': ' + x + ', ' + y);
}

// test
drawWeb(nodes, 1, SCX, SCY, 90);

关于javascript - 如何在屏幕上将对象分组到网络中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7738426/

相关文章:

javascript - 使用 Bing SDS 进行地理围栏

javascript - MVC 更新部分 View onchange 下拉列表

algorithm - 很明显地找到一个 32 位数字的最高位集的索引,没有循环

Javascript 元素类

javascript - AngularJs UI 输入前导字符匹配

algorithm - 红眼消除算法

algorithm - 如何连接阿拉伯字母组成单词

actionscript-3 - 赛车游戏中的AI对手汽车逻辑

machine-learning - 乘法滤波器或更标准的加法加权

matlab - 从信号中分离独立部分的最佳方法是什么?