Skip to content

Commit

Permalink
fix: fix center aesthetic rule
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Jun 3, 2022
1 parent e455a18 commit e415c4e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 38 deletions.
11 changes: 2 additions & 9 deletions rust/crates/tidy-tree/src/layout/basic_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct BoundingBox {
pub relative_x: Coord,
/// node y position relative to its parent
pub relative_y: Coord,
/// bounding box shift relative to the node
/// bounding box left position
pub shift_x: Coord,
}

Expand Down Expand Up @@ -79,23 +79,16 @@ impl BasicLayout {
total_width += (n - 1.) * self.peer_margin;
let mut relative_x = 0.;
let mut max_height = 0.;
let mut mid_x: Vec<Coord> = vec![];
let n = children.len();
for (i, child) in children.iter_mut().enumerate() {
child.meta.relative_y = node.height + self.parent_child_margin;
relative_x += -child.meta.shift_x;
child.meta.relative_x = relative_x;
if i == (n - 1) / 2 {
mid_x.push(relative_x);
}
if i == n / 2 {
mid_x.push(relative_x);
}
relative_x += child.meta.total_width + child.meta.shift_x + self.peer_margin;
max_height = Float::max(child.meta.total_height, max_height);
}

let shift_x = -(mid_x[0] + mid_x[1]) / 2.;
let shift_x = -total_width / 2.;
for child in children.iter_mut() {
child.meta.relative_x += shift_x;
}
Expand Down
24 changes: 3 additions & 21 deletions rust/crates/tidy-tree/tests/aesthetic_rules.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Debug, ptr::NonNull};
use std::ptr::NonNull;

use tidy_tree::{geometry::*, Layout, Node};

Expand Down Expand Up @@ -71,27 +71,9 @@ pub fn assert_no_crossed_lines(root: &Node) {
});
}

pub fn assert_parent_visually_centered(root: &Node) {
pub fn assert_parent_centered(root: &Node) {
root.pre_order_traversal(|node| {
let n = node.children.len();
if n == 0 {
return;
}

let middle = if n % 2 == 0 {
let m = n / 2;
let a = &node.children[m - 1];
let b = &node.children[m];
(a.x + b.x) / 2.
} else {
node.children[n / 2].x
};
assert!(
(node.x - middle).abs() < 1e-6,
"parent node is not centered {} {}",
node.x,
middle
);
assert!((node.meta.shift_x + node.meta.total_width / 2.).abs() < 1e-6);
});
}

Expand Down
4 changes: 2 additions & 2 deletions rust/crates/tidy-tree/tests/layout_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Debug, ptr::NonNull};
use std::ptr::NonNull;

mod aesthetic_rules;
use rand::prelude::*;
Expand All @@ -14,7 +14,7 @@ pub fn test_layout(layout: &mut dyn Layout) {
aesthetic_rules::assert_symmetric(&tree, layout);
aesthetic_rules::check_nodes_order(&tree);
aesthetic_rules::check_y_position_in_same_level(&tree);
aesthetic_rules::assert_parent_visually_centered(&tree);
aesthetic_rules::assert_parent_centered(&tree);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Renderer extends Disposable {
const g = new Group();
this.render.add(g);
g.setPosition([this.render.getWidth() / 2, 12]);
g.setScale([1.2, 1.2]);
g.setScale([0.4, 0.4]);
visit(root, (node) => {
const rect = new Rect({
shape: {
Expand Down
10 changes: 5 additions & 5 deletions src/stories/Tidy.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ export const TidyLayout = ({ root, layoutType, ...props }: Props) => {
};

TidyLayout.args = {
root: createTree(100) as Node,
root: createTree(200) as Node,
};

function createNode(): Node {
return {
id: (Math.random() * 1e9) | 0,
height: 20 * Math.random() + 10,
width: 20 * Math.random() + 10,
height: 10 * Math.random() + 10,
width: 10 * Math.random() + 10,
x: 0,
y: 0,
children: [],
Expand All @@ -45,8 +45,8 @@ function createTree(num: number): Node {
parent.children.push(child);
child.parentId = parent.id;
arr.push(child);
if (parent.children.length > 5) {
arr.splice(parentIndex, 1);
if (arr.length > 10) {
arr.shift();
}
}

Expand Down

0 comments on commit e415c4e

Please sign in to comment.