Guest User

Mike's Graphic Tree

a guest
Jan 6th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. public class GraphicTree {
  2.  
  3. private Tree Tree;
  4. private GraphicsContext GC;
  5.  
  6. public GraphicTree(Tree Tree, Canvas Canvas) {
  7. this.Tree = Tree;
  8. this.GC = Canvas.getGraphicsContext2D();
  9. }
  10.  
  11. public void Draw() {
  12. DrawVerticies(Tree.Root, 1, StartX, StartY);
  13. DrawEdges(Tree.Root)
  14. }
  15.  
  16. private double GetDistanceBetweenVerticies(double[][] Positions) {
  17. return Math.sqrt(
  18. Math.pow(Positions[1][0] - Positions[0][0], 2)
  19. + Math.pow(Positions[1][1] - Positions[0][1], 2)
  20. );
  21. }
  22.  
  23. private double[] GetLineOffsets(double[][] Positions) {
  24. double Ratio = 30f / GetDistanceBetweenVerticies(Positions);
  25. return new double[]{
  26. Ratio * (Positions[1][0] - Positions[0][0]),
  27. Ratio * (Positions[1][1] - Positions[0][1])
  28. };
  29. }
  30.  
  31. private void DrawVerticies(Node Node, double Level, double x, double y) {
  32. 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
  33. GC.strokeOval(x, y, 60, 60); // r = 30
  34.  
  35. Level++;
  36. if (Node.Left != null) {
  37. DrawVerticies(Node.Left, Level, x - ScreenSize * (1 / Math.pow(Level, 2)), y + 100);
  38. }
  39. if (Node.Right != null) {
  40. DrawVerticies(Node.Right, Level, x + ScreenSize * (1 / Math.pow(Level, 2)), y + 100);
  41. }
  42. }
  43.  
  44. private void DrawEdges(Node Node) {
  45. if (Node != null) {
  46. if (Node.Left != null) {
  47. double[] Offsets = GetLineOffsets(new double[][]{Node.Positions, Node.Left.Positions});
  48. GC.strokeLine(
  49. Positions[0][0] + Offsets[0] + 30,
  50. Positions[0][1] + Offsets[1] + 30,
  51. Positions[1][0] - Offsets[0] + 30,
  52. Positions[1][1] - Offsets[1] + 30
  53. );
  54. DrawEdges(Node.Left);
  55. }
  56. if (Node.Right != null) {
  57. double[] Offsets = GetLineOffsets(new double[][]{Node.Positions, Node.Right.Positions});
  58. GC.strokeLine(
  59. Positions[0][0] + Offsets[0] + 30,
  60. Positions[0][1] + Offsets[1] + 30,
  61. Positions[1][0] - Offsets[0] + 30,
  62. Positions[1][1] - Offsets[1] + 30
  63. );
  64. DrawEdges(Node.Right);
  65. }
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment