Skip to content

Commit 48edd27

Browse files
committed
Added visualization of self loops.
1 parent 3908adc commit 48edd27

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graph-creator",
3-
"version": "0.2.2",
3+
"version": "0.3.3",
44
"main": "index.html",
55
"window": {
66
"height": 1024,

src/creator.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ function getEdgeType(node1Ind, node2Ind){
126126
//The maximum number of edges of type n
127127
const LIMn = 1;
128128

129+
//If the edge is a self loop
130+
if(node1Ind === node2Ind){
131+
132+
//Decrement until we find a type with no edges
133+
var typeFound = false;
134+
var n = -1;
135+
136+
while(!typeFound){
137+
if(getNumOfType(n, node1Ind, node2Ind)< LIMn){
138+
return n;
139+
}
140+
141+
n--;
142+
}
143+
}
144+
145+
//Increment until we find a type with no edges
129146
var typeFound = false;
130147
var n = 0;
131148

@@ -215,6 +232,24 @@ function drawEdge(node1, node2, type){
215232
ctx.moveTo(node1.xPos, node1.yPos);
216233
ctx.lineTo(node2.xPos, node2.yPos);
217234
}
235+
//A type less than 0 is a self loop
236+
else if(type < 0){
237+
var radiusX = 10,
238+
radiusY = -15 * Math.floor(type/2);
239+
240+
var x = node1.xPos;
241+
242+
//Change whether the self loop is on top or on bottom depending on an even or odd type
243+
if(type % 2 === 0){
244+
var y = node1.yPos + radiusY;
245+
}
246+
else{
247+
var y = node1.yPos - radiusY;
248+
}
249+
250+
251+
ctx.ellipse(x, y, radiusX, radiusY, 0, 0, 2*Math.PI);
252+
}
218253
//A type of n > 0 is a ellipse edge
219254
else{
220255
//Specify the arguments from https://developer.mozilla.org/en/docs/Web/API/CanvasRenderingContext2D/ellipse

0 commit comments

Comments
 (0)