Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.qml:
- import QtQuick 2.9
- import QtQuick.Window 2.2
- import QtQuick.Controls 2.4
- Window {
- visible: true
- width: 640
- height: 640
- title: qsTr("graph")
- Graph {
- id: graph
- x: 8
- y: 8
- width: 621
- height: 397
- }
- Button {
- id: button
- x: 165
- y: 418
- width: 150
- height: 40
- text: qsTr("wyczyść")
- onClicked: clear()
- }
- Button {
- id: button1
- x: 8
- y: 418
- width: 150
- text: qsTr("wynik")
- onClicked: result()
- }
- ScrollView {
- id: scrollView
- x: 8
- y: 503
- width: 178
- height: 125
- Text {
- id: text1
- anchors.fill: parent
- font.pixelSize: 12
- }
- }
- TextInput {
- id: textInput
- x: 283
- y: 495
- width: 53
- height: 16
- text: "0"
- selectionColor: "#008069"
- font.pixelSize: 12
- }
- TextInput {
- id: textInput1
- x: 398
- y: 495
- width: 53
- height: 16
- text: "0"
- font.pixelSize: 12
- }
- Label {
- id: label
- x: 8
- y: 469
- width: 166
- height: 20
- text: qsTr("Lista sąsiedztwa:")
- verticalAlignment: Text.AlignVCenter
- horizontalAlignment: Text.AlignLeft
- }
- Label {
- id: label1
- x: 238
- y: 469
- width: 213
- height: 20
- text: qsTr("Przeszukiwanie:")
- verticalAlignment: Text.AlignVCenter
- horizontalAlignment: Text.AlignLeft
- }
- ScrollView {
- id: scrollView1
- x: 238
- y: 523
- width: 391
- height: 105
- Text {
- id: text4
- anchors.fill: parent
- font.pixelSize: 12
- }
- }
- Label {
- id: label2
- x: 238
- y: 495
- width: 34
- height: 15
- text: qsTr("start:")
- verticalAlignment: Text.AlignTop
- horizontalAlignment: Text.AlignLeft
- }
- Label {
- id: label3
- x: 342
- y: 495
- width: 43
- height: 15
- text: qsTr("koniec:")
- horizontalAlignment: Text.AlignLeft
- }
- Button {
- id: button3
- x: 321
- y: 418
- width: 150
- text: qsTr("graf skierowany")
- onClicked: changeGraphType()
- }
- Grid {
- id: grid
- x: 8
- y: 5
- width: 621
- height: 629
- }
- function clear() {
- graph.clear();
- text1.text = '';
- text4.text = '';
- }
- function result() {
- if (graph.vertex.length === 0) return;
- var text = '';
- for (var i = 0; i < graph.al.length; i++) {
- text += i + ': ';
- for (var j = 0; j < graph.al[i].length; j++) {
- text += graph.al[i][j] + ' ';
- }
- text += '\n';
- }
- text1.text = text;
- var start = parseInt(textInput.text);
- var text2 = '';
- text2 += 'BFS: ';
- text2 += graph.bfs(start) + '\n\n';
- text2 += 'DFS: ';
- text2 += graph.dfs(start) + '\n\n';
- text2 += 'Najkrótsza ścieżka: ';
- text2 += graph.pathBFS(parseInt(textInput.text), parseInt(textInput1.text));
- text2 += '\n';
- text2 += 'Ścieżka: ';
- text2 += graph.pathDFS(parseInt(textInput.text), parseInt(textInput1.text));
- text4.text = text2;
- }
- function changeGraphType() {
- if (button3.text === 'graf skierowany') {
- button3.text = 'graf nieskierowany';
- graph.setDirected(false);
- }
- else {
- button3.text = 'graf skierowany';
- graph.setDirected(true);
- }
- }
- }
- // Graph.qml:
- import QtQuick 2.0
- Item {
- id: graph
- Rectangle {
- id: area
- anchors.fill: parent
- color: "#cfdfef"
- }
- property variant vertex: [];
- property variant edge: [];
- property variant al: [];
- property int size: 0;
- property int numVertex: 0;
- property int prevEdge: -1;
- property int lastEdge: -1;
- property bool directed: true;
- MouseArea {
- id: graphMouseArea
- anchors.fill: parent
- onClicked: {
- if (!selectVertex(mouseX, mouseY)) addVertex(mouseX, mouseY);
- }
- }
- function selectVertex(x, y) {
- for (var i = 0; i < vertex.length; i++) {
- if ((x >= vertex[i].x && x <= vertex[i].x + vertex[i].width) &&
- (y >= vertex[i].y && y <= vertex[i].y + vertex[i].height))
- {
- if (!vertex[i].selected) {
- vertex[i].select();
- if (prevEdge === -1) {
- prevEdge = i;
- return true;
- }
- else {
- lastEdge = i;
- addEdge(prevEdge, lastEdge);
- vertex[lastEdge].unselect();
- vertex[prevEdge].unselect();
- prevEdge = -1;
- lastEdge = -1;
- return true;
- }
- }
- else {
- vertex[i].unselect();
- prevEdge = -1;
- return true;
- }
- }
- }
- if (prevEdge !== -1) return true;
- return false;
- }
- function addVertex(x, y) {
- x -= 15;
- y -= 15;
- vertex.push(Qt.createQmlObject('Vertex {x: ' + x + '; y: ' + y + '; number: ' + numVertex + '}', graph));
- al.push([]);
- numVertex += 1;
- }
- function addEdge(i, j) {
- if (edgeExist(i, j)) return;
- var x1 = vertex[i].x + vertex[i].width/2;
- var y1 = vertex[i].y + vertex[i].height/2;
- var x2 = vertex[j].x + vertex[j].width/2;
- var y2 = vertex[j].y + vertex[j].height/2;
- var x = x2-x1;
- var y = y2-y1;
- var angle = Math.atan(Math.abs(y)/Math.abs(x));
- if (x < 0 && y >= 0) angle = Math.PI - angle;
- else if (x < 0 && y < 0) angle = Math.PI + angle;
- else if (x >= 0 && y < 0) angle = 2.0*Math.PI - angle;
- if (directed) {
- edge.push(Qt.createQmlObject('Edge {sx: ' + x1 + '; sy: ' + y1 + '; ex: ' + x2 + '; ey: ' + y2 + '; angle: ' + angle + '}', graph));
- al[i].unshift(j);
- }
- else {
- edge.push(Qt.createQmlObject('UEdge {sx: ' + x1 + '; sy: ' + y1 + '; ex: ' + x2 + '; ey: ' + y2 + '; angle: ' + angle + '}', graph));
- al[i].unshift(j);
- al[j].unshift(i);
- }
- }
- function edgeExist(i, j) {
- for (var l = 0; l < al[i].length; l++) {
- if (al[i][l] === j) return true;
- }
- return false;
- }
- function clear() {
- for (var i = 0; i < vertex.length; i++) vertex[i].destroy();
- for (var i = 0; i < edge.length; i++) edge[i].destroy();
- vertex = [];
- edge = [];
- al = [];
- size = 0;
- numVertex = 0;
- prevEdge = -1;
- lastEdge = -1;
- }
- function bfs(start) {
- var text = '';
- var visited = [];
- for (var i = 0; i < al.length; i++) visited.push(false);
- visited[start] = true;
- var queue = [];
- queue.push(start);
- while (queue.length !== 0) {
- var p = queue.shift();
- text += p + ' ';
- for (var i = 0; i < al[p].length; i++) {
- if (!visited[al[p][i]]) {
- visited[al[p][i]] = true;
- queue.push(al[p][i]);
- }
- }
- }
- return text;
- }
- function dfs(start) {
- var text = '';
- var visited = [];
- for (var i = 0; i < al.length; i++) visited.push(false);
- visited[start] = true;
- var stack = [];
- stack.push(start);
- while (stack.length !== 0) {
- var p = stack.pop();
- text += p + ' ';
- for (var i = 0; i < al[p].length; i++) {
- if (!visited[al[p][i]]) {
- visited[al[p][i]] = true;
- stack.push(al[p][i]);
- }
- }
- }
- return text;
- }
- function pathBFS(start, end) {
- var text = '';
- var visited = [];
- var p = [];
- var found = false;
- for (var i = 0; i < al.length; i++) visited.push(false);
- visited[start] = true;
- p[start] = -1;
- var queue = [];
- queue.push(start);
- while (queue.length !== 0) {
- var v = queue.shift();
- if (v === end) {
- found = true;
- break;
- }
- for (var i = 0; i < al[v].length; i++) {
- if (!visited[al[v][i]]) {
- p[al[v][i]] = v;
- visited[al[v][i]] = true;
- queue.push(al[v][i]);
- }
- }
- }
- var t = [];
- if (!found) text = "Nie ma połączenia między punktami: " + start + " " + end;
- else {
- while (v > -1) {
- t.push(v);
- v = p[v];
- }
- }
- t.reverse();
- for (var i = 0; i < t.length; i++) text += t[i] + ' ';
- return text;
- }
- function pathDFS(start, end) {
- var text = '';
- var visited = [];
- var p = [];
- var found = false;
- for (var i = 0; i < al.length; i++) visited.push(false);
- visited[start] = true;
- p[start] = -1;
- var stack = [];
- stack.push(start);
- while (stack.length !== 0) {
- var v = stack.pop();
- if (v === end) {
- found = true;
- break;
- }
- for (var i = 0; i < al[v].length; i++) {
- if (!visited[al[v][i]]) {
- p[al[v][i]] = v;
- visited[al[v][i]] = true;
- stack.push(al[v][i]);
- }
- }
- }
- var t = [];
- if (!found) text = "Nie ma połączenia między punktami: " + start + " " + end;
- else {
- while (v > -1) {
- t.push(v);
- v = p[v];
- }
- }
- t.reverse();
- for (var i = 0; i < t.length; i++) text += t[i] + ' ';
- return text;
- }
- function setDirected(dir) {
- directed = dir;
- for (var i = 0; i < edge.length; i++) edge[i].destroy();
- edge = [];
- var cal = [];
- for (var i = 0; i < al.length; i++) {
- cal[i] = [];
- for (var j = 0; j < al[i].length; j++) {
- cal[i][j] = al[i][j];
- }
- }
- al = [];
- for (var k = 0; k < cal.length; k++) {
- al.push([]);
- }
- for (var l = 0; l < cal.length; l++) {
- for (var m = 0; m < cal[l].length; m++) {
- addEdge(l, cal[l][m]);
- }
- }
- }
- }
- // Vertex.qml:
- import QtQuick 2.0
- Rectangle {
- id: root
- property int number
- property color borderColor: "black"
- property bool selected: false
- signal activated()
- signal unactivated()
- width: 30;
- height: 30;
- border.width: 4;
- border.color: borderColor;
- color: "transparent";
- radius: width*0.5;
- Text {
- color: "red"
- x: root.width/2 - width/2
- y: root.height/2 - height/2
- text: number
- }
- function select() {
- selected = true;
- borderColor = "red";
- }
- function unselect() {
- selected = false;
- borderColor = "black";
- }
- }
- // Edge.qml:
- import QtQuick 2.0
- import QtQuick.Shapes 1.11
- Shape {
- id:root
- property int sx: 0
- property int sy: 0
- property int ex: 0
- property int ey: 0
- property real angle: 0.0
- property real a: 0.0
- property bool directed: true
- ShapePath {
- strokeColor: "black"
- strokeWidth: 2
- startX: sx; startY: sy;
- PathLine { x: ex; y: ey }
- }
- ShapePath {
- strokeColor: "black"
- strokeWidth: 2
- startX: ex; startY: ey;
- PathLine {
- x: ex - 10*Math.cos(a+angle)
- y: ey - 10*Math.sin(a+angle)
- }
- }
- ShapePath {
- strokeColor: "black"
- strokeWidth: 2
- startX: ex; startY: ey;
- PathLine {
- x: ex - 10*Math.cos(a-angle)
- y: ey + 10*Math.sin(a-angle)
- }
- }
- Component.onCompleted: {
- a = (Math.PI*30.0)/180.0;
- sx += 15*Math.cos(angle);
- sy += 15*Math.sin(angle);
- ex -= 15*Math.cos(angle);
- ey -= 15*Math.sin(angle);
- }
- }
- // UEdge.qml:
- import QtQuick 2.0
- import QtQuick.Shapes 1.11
- Shape {
- id:root
- property int sx: 0
- property int sy: 0
- property int ex: 0
- property int ey: 0
- property real angle: 0.0
- ShapePath {
- strokeColor: "black"
- strokeWidth: 2
- startX: sx; startY: sy;
- PathLine { x: ex; y: ey }
- }
- Component.onCompleted: {
- sx += 15*Math.cos(angle);
- sy += 15*Math.sin(angle);
- ex -= 15*Math.cos(angle);
- ey -= 15*Math.sin(angle);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement