GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — mxgraph/java/src/com/mxgraph/analysis/mxGraphGenerator.java at master · devgis/mxgraph Expand file tree/ Copy pathmxGraphGenerator.javaMore file actions 1551 lines (1346 loc) · 45 KB / Copy pathmxGraphGenerator.javaFile metadata and controls1551 lines (1346 loc) · 45 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 /** * Copyright (c) 2012, JGraph Ltd */ package com.mxgraph.analysis; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import com.mxgraph.costfunction.mxCostFunction; import com.mxgraph.costfunction.mxDoubleValCostFunction; import com.mxgraph.generatorfunction.mxGeneratorFunction; import com.mxgraph.generatorfunction.mxGeneratorRandomFunction; import com.mxgraph.model.mxGeometry; import com.mxgraph.model.mxIGraphModel; import com.mxgraph.view.mxCellState; import com.mxgraph.view.mxGraph; import com.mxgraph.view.mxGraphView; /** * @author Mate * */ public class mxGraphGenerator { private static final Logger log = Logger.getLogger(mxGraphGenerator.class.getName()); // cost function class that implements mxICostFunction // private mxGeneratorFunction generatorFunction = new mxGeneratorRandomFunction(0,1,2); // private mxGeneratorFunction generatorFunction = new mxGeneratorConstFunction(1.5); // private mxGeneratorFunction generatorFunction = new mxGeneratorRandomIntFunction(0, 20); private mxGeneratorFunction generatorFunction = null; private mxCostFunction costFunction = null; public mxGraphGenerator(mxGeneratorFunction generatorFunction, mxCostFunction costFunction) { if (generatorFunction != null) { this.generatorFunction = generatorFunction; } if (costFunction != null) { this.costFunction = costFunction; } else { this.costFunction = new mxDoubleValCostFunction(); } }; /** * @param aGraph * @param numVertexes * @return a null graph */ public void getNullGraph(mxAnalysisGraph aGraph, int numVertices) { if (numVertices < 0) { throw new IllegalArgumentException(); } mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); for (int i = 0; i < numVertices; i++) { graph.insertVertex(parent, null, new Integer(i).toString(), i * 50, 0, 25, 25); } }; /** * @param aGraph * @param numVertices number of vertices * @return A complete graph that has <b>numVertices</b> number of vertices */ public void getCompleteGraph(mxAnalysisGraph aGraph, int numVertices) { if (numVertices < 0) { throw new IllegalArgumentException(); } mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); Object[] vertices = new Object[numVertices]; for (int i = 0; i < numVertices; i++) { vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), i * 50, 0, 25, 25); } for (int i = 0; i < numVertices; i++) { Object vertex1 = vertices[i]; for (int j = 0; j < numVertices; j++) { Object vertex2 = vertices[j]; if (vertex1 != vertex2 && !mxGraphStructure.areConnected(aGraph, vertex1, vertex2)) { graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertex1, vertex2); } } } }; /** * @param aGraph * @param numRows - number of rows in the grid graph * @param numColumns - number of columns in the grid graph * @return Returns a <b>numColumns</b> x <b>numRows</b> grid graph */ public void getGridGraph(mxAnalysisGraph aGraph, int numColumns, int numRows) { if (numColumns < 0 || numRows < 0) { throw new IllegalArgumentException(); } mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); int numVertices = numColumns * numRows; Object[] vertices = new Object[numVertices]; for (int i = 0; i < numVertices; i++) { vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), 0, 0, 25, 25); } int vertexCount = 0; for (int j = 0; j < numRows; j++) { for (int i = 0; i < numColumns; i++) { Object currVertex = vertices[vertexCount]; if (i > 0) { // connect with previous x graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[vertexCount - 1], currVertex); } if (j > 0) { //connect with previous y graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[vertexCount - numColumns], currVertex); } vertexCount++; } } }; /** * Sets the physical spacing between vertices in a grid graph. This works for now only for a graph generated with mxGraphCreator.getGridGraph() only after creating the graph * @param aGraph * @param xSpacing - horizontal spacing between vertices * @param ySpacing - vertical spacing between vertices * @param numRows - number of rows in the grid graph * @param numColumns - number of columns in the grid graph */ public void setGridGraphSpacing(mxAnalysisGraph aGraph, double xSpacing, double ySpacing, int numColumns, int numRows) { mxGraph graph = aGraph.getGraph(); if (xSpacing < 0 || ySpacing < 0 || numColumns < 1 || numRows < 1) { throw new IllegalArgumentException(); } Object parent = graph.getDefaultParent(); Object[] vertices = aGraph.getChildVertices(parent); mxIGraphModel model = graph.getModel(); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numColumns; j++) { Object currVertex = vertices[i * numColumns + j]; mxGeometry geometry = model.getGeometry(currVertex); geometry.setX(j * xSpacing); geometry.setY(i * ySpacing); } } }; /** * @param aGraph * @param numVerticesGroup1 number of vertices in group 1 * @param numVerticesGroup2 number of vertices in group 2 * @return a bipartite graph with group 1 containing <b>numVerticesGroup1</b> vertices and group 2 containing <b>numVerticesGroup2</b> */ public void getBipartiteGraph(mxAnalysisGraph aGraph, int numVerticesGroup1, int numVerticesGroup2) { if (numVerticesGroup1 < 0 || numVerticesGroup2 < 0) { throw new IllegalArgumentException(); } int numVertices = numVerticesGroup1 + numVerticesGroup2; mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); Object[] vertices = new Object[numVertices]; for (int i = 0; i < numVertices; i++) { vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), 0, 0, 25, 25); } for (int i = 0; i < numVerticesGroup1; i++) { Object currVertex = vertices[i]; Object destVertex = vertices[getRandomInt(numVerticesGroup1, numVertices - 1)]; graph.insertEdge(parent, null, getNewEdgeValue(aGraph), currVertex, destVertex); } for (int j = 0; j < numVerticesGroup2; j++) { Object currVertex = vertices[numVerticesGroup1 + j]; int edgeNum = aGraph.getOpposites(aGraph.getEdges(currVertex, null, true, true, false, true), currVertex, true, true).length; if (edgeNum == 0) { Object destVertex = vertices[getRandomInt(0, numVerticesGroup1 - 1)]; graph.insertEdge(parent, null, getNewEdgeValue(aGraph), currVertex, destVertex); } } }; /** * Sets the physical spacing between vertices in a bipartite graph. This works for now only for a graph generated with mxGraphCreator.getBipartiteGraph() * only after creating the graph * @param aGraph * @param numVerticesGroup1 - number of vertices in group 1 * @param numVerticesGroup2 - number of vertices in group 2 * @param vertexSpacing - vertical spacing between vertices in the same group * @param groupSpacing - spacing between groups */ public void setBipartiteGraphSpacing(mxAnalysisGraph aGraph, int numVerticesGroup1, int numVerticesGroup2, double vertexSpacing, double groupSpacing) { if (numVerticesGroup1 < 0 || numVerticesGroup2 < 0) { throw new IllegalArgumentException(); } mxGraph graph = aGraph.getGraph(); double group1StartY = 0; double group2StartY = 0; Object parent = graph.getDefaultParent(); mxIGraphModel model = graph.getModel(); if (numVerticesGroup1 < numVerticesGroup2) { double centerYtimes2 = (numVerticesGroup2 * vertexSpacing); group1StartY = (centerYtimes2 - (numVerticesGroup1 * vertexSpacing)) / 2; } else { double centerYtimes2 = (numVerticesGroup1 * vertexSpacing); group2StartY = (centerYtimes2 - (numVerticesGroup2 * vertexSpacing)) / 2; } Object[] vertices = aGraph.getChildVertices(parent); // position vertexes for group 1 for (int i = 0; i < numVerticesGroup1; i++) { Object currVertex = vertices[i]; mxGeometry geometry = model.getGeometry(currVertex); geometry.setX(0); geometry.setY(group1StartY + i * vertexSpacing); } // position vertexes for group 2 for (int i = numVerticesGroup1; i < numVerticesGroup1 + numVerticesGroup2; i++) { Object currVertex = vertices[i]; mxGeometry geometry = model.getGeometry(currVertex); geometry.setX(groupSpacing); geometry.setY(group2StartY + (i - numVerticesGroup1) * vertexSpacing); } }; /** * @param aGraph * @param numVerticesGroup1 number of vertices in group 1 * @param numVerticesGroup2 number of vertices in group 2 * @return a bipartite graph with group 1 containing <b>numVerticesGroup1</b> vertices and group 2 containing <b>numVerticesGroup2</b> */ public void getCompleteBipartiteGraph(mxAnalysisGraph aGraph, int numVerticesGroup1, int numVerticesGroup2) { if (numVerticesGroup1 < 0 || numVerticesGroup2 < 0) { throw new IllegalArgumentException(); } int numVertices = numVerticesGroup1 + numVerticesGroup2; mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); Object[] vertices = new Object[numVertices]; for (int i = 0; i < numVertices; i++) { vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), 0, 0, 25, 25); } for (int i = 0; i < numVerticesGroup1; i++) { for (int j = numVerticesGroup1; j < numVertices; j++) { Object currVertex = vertices[i]; Object destVertex = vertices[j]; graph.insertEdge(parent, null, getNewEdgeValue(aGraph), currVertex, destVertex); } } }; /** * @param aGraph * @param xDim * @param yDim * @return a knight graph of size <b>xDim</b> x <b>yDim</b> * Note that the minimum size is 3x3 */ public void getKnightGraph(mxAnalysisGraph aGraph, int xDim, int yDim) { if (xDim < 3 || yDim < 3) { throw new IllegalArgumentException(); } int numVertices = xDim * yDim; mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); Object[] vertices = new Object[numVertices]; for (int i = 0; i < numVertices; i++) { vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), 0, 0, 25, 25); } //now we set up the starting conditions int[] currCoords = new int[2]; //the main loop for (int i = 0; i < (xDim * yDim); i++) { currCoords = getVertexGridCoords(xDim, yDim, i); Object[] neighborMoves = getKnightMoveVertexes(aGraph, xDim, yDim, currCoords[0], currCoords[1]); for (int j = 0; j < neighborMoves.length; j++) { // connect current with the possible move that has minimum number of its (possible moves) if (!mxGraphStructure.areConnected(aGraph, vertices[i], neighborMoves[j])) { graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[i], neighborMoves[j]); } // that vertex becomes the current vertex and we repeat until no possible moves } } }; /** * @param aGraph * @param xDim x dimension of chess-board, size starts from 1 * @param yDim y dimension of chess-board, size starts from 1 * @param xCoord x coordinate on the chess-board, coordinate starts from 1 * @param yCoord y coordinate on the chess-board, coordinate starts from 1 * @return a list of ALL vertexes which would be valid moves from the current position, regardless if they were visited or not * Note that both dimensions and both coordinates must be positive */ public Object[] getKnightMoveVertexes(mxAnalysisGraph aGraph, int xDim, int yDim, int xCoord, int yCoord) { if (xCoord > xDim || yCoord > yDim || xDim < 1 || yDim < 1 || xCoord < 1 || yCoord < 1) { throw new IllegalArgumentException(); } mxGraph graph = aGraph.getGraph(); Object[] vertices = aGraph.getChildVertices(graph.getDefaultParent()); //check all possible 8 locations //location 1 int currX = xCoord + 1; int currY = yCoord - 2; ArrayList<Object> possibleMoves = new ArrayList<Object>(); // check if in bounds Object currVertex; if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } //location 2 currX = xCoord + 2; currY = yCoord - 1; // check if in bounds if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } //location 3 currX = xCoord + 2; currY = yCoord + 1; // check if in bounds if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } //location 4 currX = xCoord + 1; currY = yCoord + 2; // check if in bounds if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } //location 5 currX = xCoord - 1; currY = yCoord + 2; // check if in bounds if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } //location 6 currX = xCoord - 2; currY = yCoord + 1; // check if in bounds if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } //location 7 currX = xCoord - 2; currY = yCoord - 1; // check if in bounds if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } //location 8 currX = xCoord - 1; currY = yCoord - 2; // check if in bounds if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } return possibleMoves.toArray(); }; /** * use this only with the grid graph, and various chess-board graphs, because of vertex ordering * @param xDim x dimension of chess-board, size starts from 1 * @param yDim y dimension of chess-board, size starts from 1 * @param value value of the vertex that needs coordinates returned * @return int[x,y] where x and y are the coordinates in the grid or chess-board * Note that both dimensions must be positive */ public int[] getVertexGridCoords(int xDim, int yDim, int value) { if (value > ((yDim * xDim) - 1) || xDim < 0 || yDim < 0 || value < 0) { throw new IllegalArgumentException(); } int yCoord = (int) Math.floor(value / xDim); int xCoord = (value - yCoord * xDim) + 1; yCoord += 1; int[] coords = new int[2]; coords[0] = xCoord; coords[1] = yCoord; return coords; }; /** * use this only with the grid graph and various chess-board graphs, because of vertex ordering * @param vertices * @param xDim x dimension of chess-board, size starts from 1 * @param yDim y dimension of chess-board, size starts from 1 * @param xCoord x coordinate on the chess-board, coordinate starts from 1 * @param yCoord y coordinate on the chess-board, coordinate starts from 1 * @return vertex on the desired coordinates. * Note that both dimensions and both coordinates must be positive */ private Object getVertexFromGrid(Object[] vertices, int xDim, int yDim, int xCoord, int yCoord) { if (xCoord > xDim || yCoord > yDim || xDim < 1 || yDim < 1 || xCoord < 1 || yCoord < 1) { throw new IllegalArgumentException(); } int value = (yCoord - 1) * xDim + xCoord - 1; return vertices[value]; }; /** * @param xDim * @param yDim * @param weights * Return a king graph of size <b>xDim</b> x <b>yDim</b> * Note that the minimum size is 4x4 */ public void getKingGraph(mxAnalysisGraph aGraph, int xDim, int yDim) { if (xDim < 2 || yDim < 2) { throw new IllegalArgumentException(); } int numVertices = xDim * yDim; mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); Object[] vertices = new Object[numVertices]; for (int i = 0; i < numVertices; i++) { vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), 0, 0, 25, 25); } //now we set up the starting conditions int[] currCoords = new int[2]; //the main loop for (int i = 0; i < (xDim * yDim); i++) { currCoords = getVertexGridCoords(xDim, yDim, i); Object[] neighborMoves = getKingMoveVertexes(aGraph, xDim, yDim, currCoords[0], currCoords[1]); for (int j = 0; j < neighborMoves.length; j++) { // connect current with the possible move that has minimum number of its (possible moves) if (!mxGraphStructure.areConnected(aGraph, vertices[i], neighborMoves[j])) { graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[i], neighborMoves[j]); } // that vertex becomes the current vertex and we repeat until no possible moves } } }; /** * @param aGraph * @param xDim x dimension of the chessboard * @param yDim y dimension of the chessboard * @param xCoord the current x position of the king * @param yCoord the current y position of the king * @return list of all possible moves of a king from the specified position * Note that both dimensions and both coordinates must be positive */ public Object[] getKingMoveVertexes(mxAnalysisGraph aGraph, int xDim, int yDim, int xCoord, int yCoord) { if (xDim < 0 || yDim < 0 || xCoord < 0 || yCoord < 0) { throw new IllegalArgumentException(); } mxGraph graph = aGraph.getGraph(); Object[] vertices = aGraph.getChildVertices(graph.getDefaultParent()); //check all possible 8 locations //location 1 int currX = xCoord + 1; int currY = yCoord - 1; ArrayList<Object> possibleMoves = new ArrayList<Object>(); // check if in bounds Object currVertex; if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } //location 2 currX = xCoord + 1; currY = yCoord; // check if in bounds if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } //location 3 currX = xCoord + 1; currY = yCoord + 1; // check if in bounds if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } //location 4 currX = xCoord; currY = yCoord + 1; // check if in bounds if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } //location 5 currX = xCoord - 1; currY = yCoord + 1; // check if in bounds if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } //location 6 currX = xCoord - 1; currY = yCoord; // check if in bounds if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } //location 7 currX = xCoord - 1; currY = yCoord + 1; // check if in bounds if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } //location 8 currX = xCoord; currY = yCoord - 1; // check if in bounds if (currX > 0 && currX <= xDim && currY > 0 && currY <= yDim) { currVertex = getVertexFromGrid(vertices, xDim, yDim, currX, currY); possibleMoves.add(currVertex); } return possibleMoves.toArray(); }; /** * @param aGraph * Returns a Petersen graph */ public void getPetersenGraph(mxAnalysisGraph aGraph) { mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); Object[] vertices = new Object[10]; for (int i = 0; i < 10; i++) { vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), 0, 0, 25, 25); } graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[0], vertices[2]); graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[0], vertices[8]); graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[0], vertices[9]); graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[1], vertices[2]); graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[1], vertices[5]); graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[1], vertices[7]); graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[2], vertices[4]); graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[3], vertices[4]); graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[3], vertices[7]); graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[3], vertices[9]); graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[4], vertices[6]); graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[5], vertices[6]); graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[5], vertices[9]); graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[6], vertices[8]); graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[7], vertices[8]); }; /** * @param aGraph * @param numVertices * Returns a path graph */ public void getPathGraph(mxAnalysisGraph aGraph, int numVertices) { if (numVertices < 0) { throw new IllegalArgumentException(); } mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); Object[] vertices = new Object[numVertices]; for (int i = 0; i < numVertices; i++) { vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), 0, 0, 25, 25); } for (int i = 0; i < numVertices - 1; i++) { graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[i], vertices[i + 1]); } }; /** * Sets the physical spacing between vertices in a path graph. This works for now only for a graph generated with mxGraphCreator.getPathGraph() * only after creating the graph * @param aGraph * @param spacing */ public void setPathGraphSpacing(mxAnalysisGraph aGraph, double spacing) { if (spacing < 0) { throw new IllegalArgumentException(); } mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); Object[] vertices = aGraph.getChildVertices(parent); mxIGraphModel model = graph.getModel(); for (int i = 0; i < vertices.length; i++) { Object currVertex = vertices[i]; mxGeometry geometry = model.getGeometry(currVertex); geometry.setX(0); geometry.setY(i * spacing); } }; /** * @param aGraph * @param numVertices * Returns a star graph * Note that minimum vertex number is 4 */ public void getStarGraph(mxAnalysisGraph aGraph, int numVertices) { if (numVertices < 4) { throw new IllegalArgumentException(); } mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); Object[] vertices = new Object[numVertices]; for (int i = 0; i < numVertices; i++) { vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), 0, 0, 25, 25); } int numVertexesInPerimeter = numVertices - 1; for (int i = 0; i < numVertexesInPerimeter; i++) { graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[numVertexesInPerimeter], vertices[i]); } }; /** * Sets the physical size of a star graph. This works for now only for a graph generated with mxGraphCreator.getStarGraph() and getWheelGraph() * @param aGraph * @param graphSize */ public void setStarGraphLayout(mxAnalysisGraph aGraph, double graphSize) { if (graphSize < 4) { throw new IllegalArgumentException(); } mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); Object[] vertices = aGraph.getChildVertices(parent); mxIGraphModel model = graph.getModel(); int vertexNum = vertices.length; double centerX = graphSize / 2f; double centerY = centerX; int numVertexesInPerimeter = vertexNum - 1; //create the circle for (int i = 0; i < numVertexesInPerimeter; i++) { //calc the position double x = 0; double y = 0; double currRatio = ((double) i / (double) numVertexesInPerimeter); currRatio = currRatio * 2; currRatio = currRatio * (double) Math.PI; x = Math.round(centerX + Math.round(graphSize * Math.sin(currRatio) / 2)); y = Math.round(centerY - Math.round(graphSize * Math.cos(currRatio) / 2)); Object currVertex = vertices[i]; mxGeometry geometry = model.getGeometry(currVertex); geometry.setX(x); geometry.setY(y); } mxGeometry geometry = model.getGeometry(vertices[vertexNum - 1]); geometry.setX(centerX); geometry.setY(centerY); }; /** * @param aGraph * @param numVertices * Returns a wheel graph. Note that numVertices has to be at least 4. */ public void getWheelGraph(mxAnalysisGraph aGraph, int numVertices) { if (numVertices < 4) { throw new IllegalArgumentException(); } mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); Object[] vertices = new Object[numVertices]; for (int i = 0; i < numVertices; i++) { vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), 0, 0, 25, 25); } int numVerticesInPerimeter = numVertices - 1; for (int i = 0; i < numVerticesInPerimeter; i++) { graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[numVerticesInPerimeter], vertices[i]); if (i < numVerticesInPerimeter - 1) { graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[i], vertices[i + 1]); } else { graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[i], vertices[0]); } } }; /** * @param aGraph * @param numBranches number of branches (minimum >= 2) * @param branchSize number of vertices in a single branch (minimum >= 2) * Returns a friendship windmill graph (aka Dutch windmill) */ public void getFriendshipWindmillGraph(mxAnalysisGraph aGraph, int numBranches, int branchSize) { if (numBranches < 2 || branchSize < 2) { throw new IllegalArgumentException(); } mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); int numVertices = numBranches * branchSize + 1; Object[] vertices = new Object[numVertices]; int vertexCount = 0; for (int i = 0; i < numBranches; i++) { for (int j = 0; j < branchSize; j++) { vertices[vertexCount] = graph.insertVertex(parent, null, new Integer(vertexCount).toString(), 0, 0, 25, 25); vertexCount++; } } vertices[numVertices - 1] = graph.insertVertex(parent, null, new Integer(numVertices - 1).toString(), 0, 0, 25, 25); //make the connections for (int i = 0; i < numBranches; i++) { Object oldVertex = vertices[numVertices - 1]; for (int j = 0; j < branchSize; j++) { Object currVertex = vertices[i * (branchSize) + j]; graph.insertEdge(parent, null, getNewEdgeValue(aGraph), oldVertex, currVertex); oldVertex = currVertex; } Object currVertex = vertices[numVertices - 1]; graph.insertEdge(parent, null, getNewEdgeValue(aGraph), oldVertex, currVertex); } }; /** * @param aGraph * @param numBranches - number of branches (minimum >= 2) * @param branchSize - number of vertices in a single branch (minimum >= 2) * Returns a windmill graph */ public void getWindmillGraph(mxAnalysisGraph aGraph, int numBranches, int branchSize) { if (numBranches < 2 || branchSize < 2) { throw new IllegalArgumentException(); } mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); int numVertices = numBranches * branchSize + 1; Object[] vertices = new Object[numVertices]; int vertexCount = 0; for (int i = 0; i < numBranches; i++) { for (int j = 0; j < branchSize; j++) { vertices[vertexCount] = graph.insertVertex(parent, null, new Integer(vertexCount).toString(), 0, 0, 25, 25); vertexCount++; } } vertices[numVertices - 1] = graph.insertVertex(parent, null, new Integer(numVertices - 1).toString(), 0, 0, 25, 25); Object centerVertex = vertices[numVertices - 1]; //make the connections for (int i = 0; i < numBranches; i++) { for (int j = 0; j < branchSize; j++) { Object vertex1 = vertices[i * (branchSize) + j]; if (!mxGraphStructure.areConnected(aGraph, centerVertex, vertex1)) { graph.insertEdge(parent, null, getNewEdgeValue(aGraph), centerVertex, vertex1); } for (int k = 0; k < branchSize; k++) { Object vertex2 = vertices[i * (branchSize) + k]; if (j != k && !mxGraphStructure.areConnected(aGraph, vertex1, vertex2)) { graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertex1, vertex2); } } } } }; //NOTE the inside code is delicate, so please change it only if you know what you're doing /** * Sets the layout of a windmill graph. Use this method only for graphs generated with mxGraphGenerator.getWindmillGraph() and getFriendshitWindmillGraph() * @param aGraph * @param numBranches * @param numVerticesInBranch * @param graphSize */ public void setWindmillGraphLayout(mxAnalysisGraph aGraph, int numBranches, int numVerticesInBranch, double graphSize) { if (graphSize < 0 || numBranches < 2 || numVerticesInBranch < 1) { throw new IllegalArgumentException(); } mxGraph graph = aGraph.getGraph(); Object parent = graph.getDefaultParent(); |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |