- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要可视化我正在处理的项目的节点树...数据结构如下所示:
构造函数:
function Tree(x,y,node){
this.root = node;
this.x = x;
this.y = y;
}
function Node(key,parent,data){
this.children = new Array();
this.key = key;
this.parent = parent;
this.data = data;
this.direction = ???;
}
每个children
数组可以容纳更多的节点,这样我就得到了一个Tree结构...x
和y
Tree
的值用于获取应绘制树根的位置,而 direction
Node
的属性(property)存储 Angular ,其中Node
应根据 parent
绘制 Angular ....
现在我的问题是:我需要一个函数 draw(context)
对于Tree.prototype
可以画出Tree
使用给定的 Canvas 上下文。
这里是一个例子,说明这样的 Tree
是如何实现的应该看起来像:
我实际上知道这样的算法如何工作,但我无法将其转换为代码......这里是:
每个节点都会推开其他节点。力取决于节点之间的距离及其级别(根节点为级别 0,它的子节点为级别 1,依此类推...)。
我想,经过几次迭代后,将会创建一棵漂亮的树......
非常感谢您,如果您尝试帮助我,甚至尝试创建这样的算法......
编辑:
这是我迄今为止尝试过的:
Tree.prototype.repulsion = function(){
if(this.root){
this.root.repulsion();
}
};
Node.prototype.repulsion = function(){
var force = {
x: 0,
y: 0
};
var pos = {
x: this.x,
y: this.y
};
var oldDirection = this.direction;
for(var i=0;i<nodes.length;i++){
var node = nodes[i];
if(node!=this){
var distance = Math.sqrt(Math.pow(pos.x-node.x,2)+Math.pow(pos.y-node.y,2));
var direction = Math.atan((this.y-node.y)/(this.x-node.x));
var magnitude = 1/Math.pow(distance,2);
if(pos.x<node.x){
direction *= -1;
force.x -= Math.cos(direction)*magnitude;
}else{
force.x += Math.cos(direction)*magnitude;
}
force.y += Math.sin(direction)*magnitude;
}
}
force.x *= repulsionFactor;
force.y *= repulsionFactor;
var newPos = {
x: pos.x+force.x,
y: pos.y+force.y
};
var newDirection = Math.atan((newPos.y-this.parent.y)/(newPos.x-this.parent.x));
if(force.x<0){
newDirection += Math.PI;
}
this.direction = newDirection;
this.direction %= 2*Math.PI;
for(var i=0;i<this.children.length;i++){
this.children[i].repulsion();
}
};
最佳答案
我实际上有了自己的想法!
var tree, width, height, clock, lastCalledTime, root, node,
repulsionFactor = 10000,
nodes = new Array();
function setup(){
tree = new Tree(200,200);
tree.repulsionFactor = 10000;
for(var i=0;i<5;i++){
tree.addRandomChild();
}
for(var i=0;i<10;i++){
tree.addRandomChild(1);
}
for(var i=0;i<15;i++){
tree.addRandomChild(2);
}
root = tree.root;
node = root.children[0];
var canvas = document.getElementById('canvas');
width = 400;
height = 400;
canvas.width = width;
canvas.height = height;
canvasSize = {x:window.innerWidth-5,y:window.innerHeight-5};
ctx = canvas.getContext('2d');
clock = requestAnimationFrame(main);
//clock = setInterval(main,200);
}
function main(){
if(!lastCalledTime) {
lastCalledTime = Date.now();
}
ctx.clearRect(0,0,width,height);
tree.repulsion();
tree.draw();
var delta = (Date.now() - lastCalledTime)/1000;
lastCalledTime = Date.now();
var fps = 1/delta;
tree.repulsionFactor*=0.99;
clock = requestAnimationFrame(main);
}
function Tree(x,y){
this.x = x;
this.y = y;
this.nodeColor = "gray";
this.lineColor = "black";
this.size = 20;
this.lineWidth = 3;
this.linkLength = 100;
this.nodes = new Array();
this.repulsionFactor = 10000;
this.root = new Node(this,this,"root");
}
Tree.prototype.addRandomChild = function(level=0){
var node = this.root;
for(var i=0;i<level;i++){
if(node.children.length>0){
var randIndex = Math.floor(node.children.length*Math.random());
node = node.children[randIndex];
}else{
return false;
}
}
node.addChild();
};
Tree.prototype.draw = function(){
this.root.draw();
}
Tree.prototype.repulsion = function(){
this.root.repulsion();
};
Tree.prototype.level = -1;
Tree.prototype.direction = 0;
function Node(parent,tree,key,data,direction){
this.children = new Array();
this.parent = parent;
this.tree = tree;
this.key = key;
this.data = data;
if(direction){
this.direction = direction;
}else{
this.direction = this.parent.direction+Math.random()/10;
}
this.tree.nodes.push(this);
}
Node.prototype.addChild = function(key,data){
this.children.push(new Node(this,this.tree,key,data));
};
Node.prototype.draw = function(){
ctx.fillStyle = this.nodeColor;
ctx.strokeStyle = this.lineColor;
ctx.lineWidth = this.lineWidth;
if(this.key!="root"){
ctx.beginPath();
ctx.moveTo(this.x,this.y);
ctx.lineTo(this.parent.x,this.parent.y);
ctx.stroke();
}
for(var i=0;i<this.children.length;i++){
this.children[i].draw();
}
ctx.beginPath();
ctx.arc(this.x,this.y,this.size,0,2*Math.PI,false);
ctx.fill();
ctx.stroke();
}
Node.prototype.repulsion = function(){
if(this.key!="root"){
var force = {
x: 0,
y: 0
};
var pos = {
x: this.x,
y: this.y
};
var nodes = this.tree.nodes;
for(var i=0;i<nodes.length;i++){
var node = nodes[i];
if(node!=this){
var distance = Math.sqrt(Math.pow(pos.x-node.x,2)+Math.pow(pos.y-node.y,2));
var direction = Math.atan2((pos.y-node.y),(pos.x-node.x));
var magnitude = 1/Math.pow(distance,2)/(node.level+1);
force.x += Math.cos(direction)*magnitude;
force.y += Math.sin(direction)*magnitude;
}
}
force.x *= this.tree.repulsionFactor;
force.y *= this.tree.repulsionFactor;
var newPos = {
x: pos.x+force.x,
y: pos.y+force.y
};
var newDirection = Math.atan2((newPos.y-this.parent.y),(newPos.x-this.parent.x));
this.direction = newDirection;
}else{
this.direction = this.parent.direction;
}
for(var i=0;i<this.children.length;i++){
this.children[i].repulsion();
}
};
Object.defineProperty(Node.prototype,"x",{
get: function x(){
if(this.key=="root"){
return this.parent.x;
}
return Math.cos(this.direction)*this.linkLength+this.parent.x;
}
});
Object.defineProperty(Node.prototype,"y",{
get: function y(){
if(this.key=="root"){
return this.parent.y;
}
return Math.sin(this.direction)*this.linkLength+this.parent.y;
}
});
Object.defineProperty(Node.prototype,"level",{
get: function level(){
return this.parent.level+1;
}
});
Object.defineProperty(Node.prototype,"nodeColor",{
get: function nodeColor(){
return this.parent.nodeColor;
}
});
Object.defineProperty(Node.prototype,"lineColor",{
get: function lineColor(){
return this.parent.lineColor;
}
});
Object.defineProperty(Node.prototype,"lineWidth",{
get: function lineWidth(){
return this.parent.lineWidth;
}
});
Object.defineProperty(Node.prototype,"size",{
get: function size(){
return this.parent.size*0.8;
}
});
Object.defineProperty(Node.prototype,"linkLength",{
get: function linkLength(){
return this.parent.linkLength*0.8;
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Node Tree</title>
</head>
<body onload="setup()">
<canvas id="canvas"></canvas>
</body>
</html>
出于某种原因,树有时会不断旋转,而且并不是每次我创建树时它都会展开......但希望您可以对此进行扩展!
关于javascript - 可视化节点树(斥力?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43044450/
我正在使用 JavaFX 8 创建一个应用程序。我使用拖/放动态更改网格 Pane 的内容。我希望每行或每行/列迭代 GridPane 内容。JavaFX 允许通过指定行和列在 GridPane 中添
我正在尝试将图像拖放到div上。图像没有被拖到div上并给出以下错误 Uncaught TypeError: Failed to execute 'appendChild' on 'Node': pa
我正在 android studio 中创建内部构建 AR 导航。我正在寻找一种方法将 anchor 与其他 anchor 或 anchor 节点/节点“连接”起来。我不确定使用哪一个。基于我将强制用
我在 Hive 上运行一些作业:首先是 4 节点,然后是 2 节点。令我惊讶的是,我的 2 节点性能比我的 4 节点更好。 首先,我在一个 4 节点(4 个事件节点)上运行查询,然后关闭 2 个节点(
我有 Node* current ,我在其中存储指向列表“顶部”当前节点的指针。当我将一个新节点设置为当前节点时,出现错误: '=' : cannot convert from 'CircularDo
我是 dcos Mesos 的新手,在本地 Ubuntu 机器上安装了 dc os。 我可以查看 dcos 仪表板。 但我无法使用 dcos node ssh --master-proxy --lea
在 JavaFX 中,是否有类似 setLayout(); 的东西?或 setBounds(); ? 例如,我想将按钮定位到我想要的位置。 最佳答案 JavaFX 场景图上的所有内容都是 Node .
我正在开发一个 JavaFX 应用程序,其中我开发的类(从 javafx.scene.Parent 扩展)是根据用户在 ListView 控件中单击的条目动态创建的。 只是要清楚这个节点,它不是使用像
我正在尝试为节点-边缘关系创建一个类图,因为它可以在有向图中找到。我想传达的是,Nodes 引用了 Edges,Edges 也引用了 Nodes。每个 Edge 都恰好需要两个 Node(源和目标)。
在mapreduce作业期间,单个任务将在随机节点上运行,是否有任何方法限制应在其中运行任务的节点? 最佳答案 Hadoop不会选择节点来随机运行任务。考虑到数据局部性,否则将有很多网络开销。 任务与
有什么区别: a) nodetool 重建 b) nodetool 修复 [-pr] 换句话来说,各个命令到底是做什么的? 最佳答案 nodetool重建:类似于引导过程(当您向集群添加新节点时),但
我已将第一个 OneToMany 关系添加到我的 hibernate 3.6.10 项目中。这是一个类: /** * */ package com.heavyweightsoftware.leal
是否有可能找到正在监听触发当前函数的事件的元素? 在下面的代码中,event.target 返回 #xScrollPane 和 event.currentTarget 和 event 的最低子节点.f
我正在尝试覆盖我数据库中的一些数据。结构很简单,就是: recipes { user_1{ recipe_1{data} recipe_2{data} } user_2{
我使用 setInterval 来运行该函数,但它会多次执行函数 2... 如何在输入中插入一个值后执行函数 第一个输入与其余输入的距离不同 如何在插入 val(tab 选项)后将插入从 1 个输入移
我不知道代码有什么问题,但在 visual studio 中不断收到这些错误消息。 Error 18 error C1903: unable to recover from previous e
我正在尝试从其类中获取 SharePoint 搜索导航节点的对象。 var nodes = $("div.ms-qSuggest-listItem"); 我正在获取节点对象,现在想要获取“_promp
D:\nodeP>node main.js module.js:327 抛出错误; ^ 错误:在 Function.Module 的 Function.Module._resolveFilename
struct node{ int key, prior, cnt, val; node *l, *r; node(){} node(int nkey) : key(nkey),
我有以下代码使用迭代器将项目插入双链表。这就是我们被要求这样做的方式。代码有效,但问题是我有 24 字节的绝对内存泄漏。 NodeIterator insert(NodeIterator & itrP
我是一名优秀的程序员,十分优秀!