Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GraphicTree {
- private Tree Tree;
- private GraphicsContext GC;
- public GraphicTree(Tree Tree, Canvas Canvas) {
- this.Tree = Tree;
- this.GC = Canvas.getGraphicsContext2D();
- }
- public void Draw() {
- DrawVerticies(Tree.Root, 1, StartX, StartY);
- DrawEdges(Tree.Root)
- }
- private double GetDistanceBetweenVerticies(double[][] Positions) {
- return Math.sqrt(
- Math.pow(Positions[1][0] - Positions[0][0], 2)
- + Math.pow(Positions[1][1] - Positions[0][1], 2)
- );
- }
- private double[] GetLineOffsets(double[][] Positions) {
- double Ratio = 30f / GetDistanceBetweenVerticies(Positions);
- return new double[]{
- Ratio * (Positions[1][0] - Positions[0][0]),
- Ratio * (Positions[1][1] - Positions[0][1])
- };
- }
- private void DrawVerticies(Node Node, double Level, double x, double y) {
- GC.strokeText(Node.Element, x + a, y + b); //a and b are some offset. Use trial and error to calc or use text width properties
- GC.strokeOval(x, y, 60, 60); // r = 30
- Level++;
- if (Node.Left != null) {
- DrawVerticies(Node.Left, Level, x - ScreenSize * (1 / Math.pow(Level, 2)), y + 100);
- }
- if (Node.Right != null) {
- DrawVerticies(Node.Right, Level, x + ScreenSize * (1 / Math.pow(Level, 2)), y + 100);
- }
- }
- private void DrawEdges(Node Node) {
- if (Node != null) {
- if (Node.Left != null) {
- double[] Offsets = GetLineOffsets(new double[][]{Node.Positions, Node.Left.Positions});
- GC.strokeLine(
- Positions[0][0] + Offsets[0] + 30,
- Positions[0][1] + Offsets[1] + 30,
- Positions[1][0] - Offsets[0] + 30,
- Positions[1][1] - Offsets[1] + 30
- );
- DrawEdges(Node.Left);
- }
- if (Node.Right != null) {
- double[] Offsets = GetLineOffsets(new double[][]{Node.Positions, Node.Right.Positions});
- GC.strokeLine(
- Positions[0][0] + Offsets[0] + 30,
- Positions[0][1] + Offsets[1] + 30,
- Positions[1][0] - Offsets[0] + 30,
- Positions[1][1] - Offsets[1] + 30
- );
- DrawEdges(Node.Right);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment