Skip to content

Commit ac4da53

Browse files
committed
la til eksempler i graph
1 parent def0a16 commit ac4da53

13 files changed

+306
-390
lines changed

.idea/workspace.xml

Lines changed: 258 additions & 309 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Graphs/DfsBfs.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@
2525
<div id="buttonDiv" class="bottom-container left-bottom">
2626
<button id="dfs" class="my-button" onclick="startDfs(0);">DFS</button>
2727
<button id="bfs" class="my-button" onclick="startBfs(0);">BFS</button>
28+
<button id="reset" class="my-button" onclick="resetAll();">Reset</button>
29+
<br>
30+
<br>
2831
<button id="prev" class="my-button" onclick="manager.previous();">Previous</button>
2932
<button id="next" class="my-button" onclick="manager.next();">Next</button>
30-
<button id="reset" class="my-button" onclick="resetAll();">Reset</button>
33+
<button id="Test1" class="my-button" onclick="exampleGraphStar();">Example1</button>
34+
<button id="Test2" class="my-button" onclick="exampleGraphAllConnected();">Example2</button>
35+
3136
</div>
3237
<div id="bottomRight" class="bottom-container right-bottom">
3338
<ul id="visitedUL" class="insElements">

Graphs/css/style.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ div#queueUI {
2727

2828
.bottom-container {
2929
/* height: 40px; */
30+
text-align: center;
3031
float: left;
3132
margin-left: 10px;
3233
position: relative;
@@ -38,7 +39,7 @@ div#queueUI {
3839
padding-left: 30px;
3940
padding-top: 30px;
4041
padding-bottom: 15px;
41-
width: 20%;
42+
width: 25%;
4243
}
4344

4445
.right-bottom {
@@ -72,7 +73,7 @@ div#queueUI {
7273
font-family: Arial;
7374
font-size: 15px;
7475
font-weight: bold;
75-
padding: 6px 24px;
76+
padding: 6px 0px;
7677
text-decoration: none;
7778
text-shadow: 0px 1px 0px #1570cd;
7879
width: 100px;

Graphs/js/adjacencyList.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ function addEdgeToAdjList(from: number, to: number) {
5252
}
5353

5454
function getAdjListAsText(node: number): string {
55-
var returnString = "[ ";
56-
for (var i = 0; i < adjacencyList[node].length; i++) {
55+
let returnString = "[ ";
56+
for (let i = 0; i < adjacencyList[node].length; i++) {
5757
returnString += adjacencyList[node][i] + (i == adjacencyList[node].length - 1 ? " ]" : ", ");
5858
}
5959
return returnString;
6060
}
6161

6262
function resetAdjList() {
63-
adjacencyList = []
63+
adjacencyList = [];
6464
$("#adjacencyList div").each(function () {
6565
$(this).remove();
6666
});

Graphs/js/dfsBfsController.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function startBfs(startIndex: number) {
1818
bfsQueue.push(startIndex);
1919

2020
while (bfsQueue.length != 0) {
21-
let v: number = bfsQueue.shift();
21+
let v = bfsQueue.shift();
2222
popFromBfsQueue(v);
2323
visit(v);
2424

@@ -128,9 +128,9 @@ function visit(id: number) {
128128

129129
let backwards = function (v, curr) {
130130
return function () {
131-
$("#node" + v).css("background-color", "white")
132-
$("#node" + v).css("border", "6px solid black")
133-
$("#node" + curr).css("border", "6px solid rgb(16, 130, 219)")
131+
$("#node" + v).css("background-color", "white");
132+
$("#node" + v).css("border", "6px solid black");
133+
$("#node" + curr).css("border", "6px solid rgb(16, 130, 219)");
134134
$("#insElemNr" + v).html("<p>" + v + "</p><div> F </div>");
135135
$("#insElemNr" + v).removeClass("marked");
136136
};

Graphs/js/eventManager.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ var eventManager = /** @class */ (function () {
4545
this.paused = true;
4646
clearInterval(this.eventThread);
4747
};
48+
eventManager.prototype.clear = function () {
49+
clearInterval(this.eventThread);
50+
this.nextEvents = [];
51+
this.previousEvents = [];
52+
};
4853
return eventManager;
4954
}());
5055
var FrontendEvent = /** @class */ (function () {
@@ -56,15 +61,3 @@ var FrontendEvent = /** @class */ (function () {
5661
return FrontendEvent;
5762
}());
5863
var manager = new eventManager();
59-
/*
60-
/** How to add FrontendEvents to manager
61-
for(var i=0; i<10; i++) {
62-
var f = function(k) {
63-
return function() {console.log("Going forward, step " + k);};
64-
}(i);
65-
var b = function(k) {
66-
return function() {console.log("Going backward, step " + k);}
67-
}(i);
68-
manager.addEvent(new FrontendEvent(f,b));
69-
}
70-
*/

Graphs/js/eventManager.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ class eventManager {
5252
this.paused = true;
5353
clearInterval(this.eventThread);
5454
}
55+
56+
clear() {
57+
clearInterval(this.eventThread);
58+
this.nextEvents = [];
59+
this.previousEvents = [];
60+
}
5561
}
5662

5763
class FrontendEvent {
@@ -66,17 +72,4 @@ class FrontendEvent {
6672
}
6773
}
6874

69-
let manager: eventManager = new eventManager();
70-
71-
/*
72-
/** How to add FrontendEvents to manager
73-
for(var i=0; i<10; i++) {
74-
var f = function(k) {
75-
return function() {console.log("Going forward, step " + k);};
76-
}(i);
77-
var b = function(k) {
78-
return function() {console.log("Going backward, step " + k);}
79-
}(i);
80-
manager.addEvent(new FrontendEvent(f,b));
81-
}
82-
*/
75+
let manager: eventManager = new eventManager();

Graphs/js/graphStructureController.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,28 @@ function resetAll() {
2020
resetGraphUI();
2121
resetAdjList();
2222
removeVisitedArray();
23+
manager.clear();
2324
nodes = 0;
2425
edges = 0;
2526
manager = new eventManager();
2627
}
2728
function checkOverlap(x, y) {
28-
console.log("overlap check");
2929
var overlap = false;
3030
$("#graphUI").children().each(function () {
31-
console.log(this);
3231
if (this.id !== "edgeSvg") {
3332
var pos = $("#" + this.id).position();
34-
console.log(pos.left + " " + pos.top + " my: " + x + " " + y); // "this" is the current element in the loop
3533
if (pos.left - 35 < x && pos.left + 115 > x && pos.top - 35 < y && pos.top + 115 > y) {
3634
overlap = true;
3735
}
3836
}
3937
});
40-
console.log("overlap" + overlap);
4138
return overlap;
4239
}
4340
/*************************************************************** */
4441
/******************* Example Graphs ****************************/
4542
/*************************************************************** */
4643
function exampleGraphStar() {
44+
resetAll();
4745
graphUIClicked(287, 230);
4846
graphUIClicked(73, 98);
4947
graphUIClicked(266, 49);
@@ -63,6 +61,7 @@ function exampleGraphStar() {
6361
twoNodesClicked(7, 0);
6462
}
6563
function exampleGraphAllConnected() {
64+
resetAll();
6665
graphUIClicked(66, 71);
6766
graphUIClicked(337, 158);
6867
graphUIClicked(571, 64);

Graphs/js/graphStructureController.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@ function resetAll() {
2828
resetGraphUI();
2929
resetAdjList();
3030
removeVisitedArray();
31+
manager.clear();
3132
nodes = 0;
3233
edges = 0;
3334
manager = new eventManager();
3435
}
3536

3637
function checkOverlap(x: number, y: number) {
37-
console.log("overlap check");
3838
let overlap = false;
3939
$("#graphUI").children().each(function () {
40-
console.log(this);
4140
if (this.id !== "edgeSvg") {
4241
let pos = $("#" + this.id).position();
4342
if (pos.left - 35 < x && pos.left + 115 > x && pos.top - 35 < y && pos.top + 115 > y) {
@@ -50,11 +49,11 @@ function checkOverlap(x: number, y: number) {
5049

5150

5251
/*************************************************************** */
53-
5452
/******************* Example Graphs ****************************/
5553
/*************************************************************** */
5654

5755
function exampleGraphStar() {
56+
resetAll();
5857
graphUIClicked(287, 230);
5958
graphUIClicked(73, 98);
6059
graphUIClicked(266, 49);
@@ -75,15 +74,16 @@ function exampleGraphStar() {
7574
}
7675

7776
function exampleGraphAllConnected() {
77+
resetAll();
7878
graphUIClicked(66, 71);
7979
graphUIClicked(337, 158);
8080
graphUIClicked(571, 64);
81-
graphUIClicked(77, 278)
82-
graphUIClicked(233, 306)
83-
graphUIClicked(420, 303)
84-
graphUIClicked(437, 454)
85-
graphUIClicked(134, 437)
86-
graphUIClicked(577, 184)
81+
graphUIClicked(77, 278);
82+
graphUIClicked(233, 306);
83+
graphUIClicked(420, 303);
84+
graphUIClicked(437, 454);
85+
graphUIClicked(134, 437);
86+
graphUIClicked(577, 184);
8787
twoNodesClicked(0, 1);
8888
twoNodesClicked(2, 1);
8989
twoNodesClicked(2, 0);

Graphs/js/graphUI.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var edgeIdList = [[]];
44
/*********************** Click Handler *****************************/
55
/***************************************************************** */
66
var clickedId = -1; // If -1, no element has been clicked yet
7-
// TODO: Noder havner noen ganger over hverandre når man skal koble sammen noder
87
$("#graphUI").click(function (e) {
98
/** If <node> was clicked */
109
if (e.target.className == "nodeUI") {
@@ -137,7 +136,3 @@ function deselectTwoNodes(n1, n2) {
137136
$("#node" + n1).removeClass("selected");
138137
$("#node" + n2).removeClass("selected");
139138
}
140-
function openInfoWindow() {
141-
window.open("http://www.w3schools.com");
142-
}
143-
// <svg height="100%" width="100%"> <line x1="0" y1="0" x2="200" y2="200" style="stroke:rgb(255,0,0);stroke-width:2"></line></svg>

Graphs/js/graphUI.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ let edgeIdList: number[][] = [[]];
77
/*********************** Click Handler *****************************/
88
/***************************************************************** */
99
let clickedId: number = -1; // If -1, no element has been clicked yet
10-
// TODO: Noder havner noen ganger over hverandre når man skal koble sammen noder
1110
$("#graphUI").click(function (e: any) {
1211
/** If <node> was clicked */
1312
if (e.target.className == "nodeUI") {
@@ -44,7 +43,6 @@ $("#graphUI").click(function (e: any) {
4443
}
4544
});
4645

47-
4846
/***************************************************************** */
4947

5048
/*********************** Graph UI Functions ************************/
@@ -115,9 +113,7 @@ function resetGraphUI() {
115113
edgeIdList = [[]];
116114
}
117115

118-
119116
/***************************************************************** */
120-
121117
/********************* HELPER FUNCTIONS ****************************/
122118
/***************************************************************** */
123119

@@ -126,7 +122,7 @@ function getNormal(x1: number, y1: number, x2: number, y2: number) {
126122
return {x: -dy, y: dx};
127123
}
128124

129-
function getUnit(x, y) {
125+
function getUnit(x: number, y: number) {
130126
let length = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
131127
return {x: x / length, y: y / length};
132128
}
@@ -162,10 +158,4 @@ function deselectAllNodes() {
162158
function deselectTwoNodes(n1: number, n2: number) {
163159
$("#node" + n1).removeClass("selected");
164160
$("#node" + n2).removeClass("selected");
165-
}
166-
167-
function openInfoWindow() {
168-
window.open("http://www.w3schools.com");
169-
}
170-
171-
// <svg height="100%" width="100%"> <line x1="0" y1="0" x2="200" y2="200" style="stroke:rgb(255,0,0);stroke-width:2"></line></svg>
161+
}

Graphs/js/visitedArray.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,14 @@ function setInitialArray() {
4848
var arr = [];
4949
// Remove elements
5050
removeVisitedArray();
51-
$("#visitedUL").append("<p id='visitedText' class='visited-text'>visited</p>");
52-
$("#visitedUL").append("<img id='leftBracket' class='bracket' src='assets/square_left.png'/>");
51+
$("#visitedUL").append("<p id='visitedText' class='visited-text'>Visited:</p>");
52+
//$("#visitedUL").append("<img id='leftBracket' class='bracket' src='../assets/square_left.png'/>");
5353
for (var i = 0; i < nodes; i++) {
5454
$("#visitedUL").append("<li id='insElemNr" + i + "'><div>" + "F</div></li>");
5555
arr.push('F');
5656
$("#insElemNr" + i).prepend("<p id='ind" + i + "'>" + i + "</p>");
5757
}
58-
// place right bracket
59-
//<img id="rightBracket" class="bracket" src="assets/square_right.png"/>
60-
var posLeft = nodes * (20);
61-
//style='left="+ posLeft + "px'
62-
$("#visitedUL").append("<img id='rightBracket' class='bracket' src='assets/square_right.png'/>");
58+
//$("#visitedUL").append("<img id='rightBracket' class='bracket' src='../assets/square_right.png'/>");
6359
}
6460
function centerElements() {
6561
var arrayWidth = ((array.length - 1) * 85) + 50;

Graphs/js/visitedArray.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,16 @@ function setInitialArray() {
6161
// Remove elements
6262
removeVisitedArray();
6363

64-
$("#visitedUL").append("<p id='visitedText' class='visited-text'>visited</p>");
65-
$("#visitedUL").append("<img id='leftBracket' class='bracket' src='assets/square_left.png'/>");
64+
$("#visitedUL").append("<p id='visitedText' class='visited-text'>Visited:</p>");
65+
//$("#visitedUL").append("<img id='leftBracket' class='bracket' src='../assets/square_left.png'/>");
6666

6767
for (let i = 0; i < nodes; i++) {
6868
$("#visitedUL").append("<li id='insElemNr" + i + "'><div>" + "F</div></li>");
6969
arr.push('F');
7070
$("#insElemNr" + i).prepend("<p id='ind" + i + "'>" + i + "</p>");
7171
}
7272

73-
// place right bracket
74-
//<img id="rightBracket" class="bracket" src="assets/square_right.png"/>
75-
let posLeft = nodes * (20);
76-
//style='left="+ posLeft + "px'
77-
$("#visitedUL").append("<img id='rightBracket' class='bracket' src='assets/square_right.png'/>");
78-
73+
//$("#visitedUL").append("<img id='rightBracket' class='bracket' src='../assets/square_right.png'/>");
7974
}
8075

8176
function centerElements() {

0 commit comments

Comments
 (0)