Skip to content

Commit c3071cf

Browse files
Add version 1.0 files
1 parent db77222 commit c3071cf

15 files changed

+773
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
This code and its output are licensed under the
3+
Creative Commons Attribution-NonCommercial-NoDerivatives
4+
4.0 International (CC BY-NC-ND 4.0) License.
5+
https://creativecommons.org/licenses/by-nc-nd/4.0/
6+
*/
7+
8+
// Gradient Graphs
9+
// Edge Display Class
10+
// Author: Brittni Watkins
11+
12+
class EdgeDisplay {
13+
constructor(aPositionPercentage, bPositionPercentage, aColor, bColor) {
14+
this.aPositionPercentage = aPositionPercentage.copy();
15+
this.bPositionPercentage = bPositionPercentage.copy();
16+
this.aColor = aColor;
17+
this.bColor = bColor;
18+
this.aColorWithAlpha = new Color(aColor.getColor());
19+
this.aColorWithAlpha.setAlpha(100);
20+
this.bColorWithAlpha = new Color(bColor.getColor());
21+
this.bColorWithAlpha.setAlpha(100);
22+
this.displayCircle = true;
23+
this.displayLine = true;
24+
}
25+
26+
getCenterPercentage() {
27+
let xPercentage = (this.aPositionPercentage.x + this.bPositionPercentage.x) / 2;
28+
let yPercentage = (this.aPositionPercentage.y + this.bPositionPercentage.y) / 2;
29+
return createVector(xPercentage, yPercentage);
30+
}
31+
32+
getDistance() {
33+
let distance = dist(this.aPositionPercentage.x * width, this.aPositionPercentage.y * height, this.bPositionPercentage.x * width, this.bPositionPercentage.y * height);
34+
return distance;
35+
}
36+
37+
display() {
38+
if (this.displayLine) {
39+
this.displayGradientLine();
40+
}
41+
42+
if (this.displayCircle) {
43+
this.displayGradientCircle();
44+
}
45+
}
46+
47+
toggleDisplayLine() {
48+
this.displayLine = !this.displayLine;
49+
}
50+
51+
toggleDisplayCircle() {
52+
this.displayCircle = !this.displayCircle;
53+
}
54+
55+
displayGradientLine() {
56+
let numSegments = 10;
57+
let start = this.aPositionPercentage.copy();
58+
let end;
59+
60+
for (let i = 1; i <= numSegments; i++) {
61+
let colorPercentage = (i - 1) * (1.0 / numSegments);
62+
let percent = i * (1.0 / numSegments);
63+
end = p5.Vector.lerp(this.aPositionPercentage, this.bPositionPercentage, percent);
64+
stroke(lerpColor(this.aColor.getColor(), this.bColor.getColor(), colorPercentage));
65+
line(start.x * width, start.y * height, end.x * width, end.y * height);
66+
start.set(end);
67+
}
68+
}
69+
70+
displayGradientCircle() {
71+
let centerPercentage = this.getCenterPercentage();
72+
let distance = this.getDistance();
73+
let radius = distance / 2;
74+
let theta = 0;
75+
let numPoints = 50;
76+
beginShape();
77+
noStroke();
78+
79+
for (let i = 0; i < numPoints; i++) {
80+
let baseX = centerPercentage.x * width;
81+
let baseY = centerPercentage.y * height;
82+
let x = baseX + (cos(theta) * radius);
83+
let y = baseY + (sin(theta) * radius);
84+
let d = dist(this.aPositionPercentage.x * width, this.aPositionPercentage.y * height, x, y);
85+
let col = lerpColor(this.aColorWithAlpha.getColor(), this.bColorWithAlpha.getColor(), d / distance);
86+
fill(col);
87+
vertex(x, y);
88+
theta += TWO_PI / numPoints;
89+
}
90+
91+
endShape(CLOSE);
92+
}
93+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
This code and its output are licensed under the
3+
Creative Commons Attribution-NonCommercial-NoDerivatives
4+
4.0 International (CC BY-NC-ND 4.0) License.
5+
https://creativecommons.org/licenses/by-nc-nd/4.0/
6+
*/
7+
8+
// Graident Graphs
9+
// Graph Display Class
10+
// Author: Brittni Watkins
11+
12+
class GraphDisplay {
13+
constructor(graph, center, width, height, colorGenerators) {
14+
this.graph = graph;
15+
this.center = center.copy();
16+
this.width = width;
17+
this.height = height;
18+
this.nodeDisplays = new Map();
19+
this.edgeDisplays = [];
20+
this.colorGenerators = colorGenerators;
21+
this.buildNodeDisplays();
22+
this.buildEdgeDisplays();
23+
}
24+
25+
buildNodeDisplays() {
26+
let nodes = this.graph.getNodes();
27+
28+
nodes.forEach(node => {
29+
let position = this.getScreenPositionPercentage(node);
30+
let colorGeneratorIndex = randomInt(0, this.colorGenerators.length);
31+
let colorGenerator = this.colorGenerators[colorGeneratorIndex];
32+
let radius = min(width, height) / 100.0;
33+
let nodeDisplay = new NodeDisplay(position, radius, new Color(colorGenerator.getRandomColor()));
34+
this.nodeDisplays.set(node, nodeDisplay);
35+
});
36+
}
37+
38+
toggleDisplayLine() {
39+
this.edgeDisplays.forEach(edgeDisplay => {
40+
edgeDisplay.toggleDisplayLine();
41+
});
42+
}
43+
44+
toggleDisplayCircle() {
45+
this.edgeDisplays.forEach(edgeDisplay => {
46+
edgeDisplay.toggleDisplayCircle();
47+
});
48+
}
49+
50+
toggleDisplayNode() {
51+
this.nodeDisplays.forEach(nodeDisplay => {
52+
nodeDisplay.toggleDisplayNode();
53+
});
54+
}
55+
56+
buildEdgeDisplays() {
57+
let edges = this.graph.getEdges();
58+
59+
edges.forEach(edge => {
60+
let a = edge.getA();
61+
let b = edge.getB();
62+
let aPositionPercentage = this.getScreenPositionPercentage(a);
63+
let bPositionPercentage = this.getScreenPositionPercentage(b);
64+
let aDisplay = this.nodeDisplays.get(a);
65+
let bDisplay = this.nodeDisplays.get(b);
66+
let aColor = aDisplay.getColor();
67+
let bColor = bDisplay.getColor();
68+
let edgeDisplay = new EdgeDisplay(aPositionPercentage, bPositionPercentage, aColor, bColor);
69+
this.edgeDisplays.push(edgeDisplay);
70+
});
71+
}
72+
73+
getScreenPositionPercentage(node) {
74+
let position = node.getPosition();
75+
let scale = this.graph.getScale();
76+
let graphMin = scale.getMin();
77+
let graphMax = scale.getMax();
78+
let minScreenXPercentage = (this.center.x - (this.width / 2.0)) / width;
79+
let maxScreenXPercentage = (this.center.x + (this.width / 2.0)) / width;
80+
let minScreenYPercentage = (this.center.y - (this.height / 2.0)) / height;
81+
let maxScreenYPercentage = (this.center.y + (this.height / 2.0)) / height;
82+
let x = map(position.x, graphMin.x, graphMax.x, minScreenXPercentage, maxScreenXPercentage);
83+
let y = map(position.y, graphMin.y, graphMax.y, minScreenYPercentage, maxScreenYPercentage);
84+
return createVector(x, y);
85+
}
86+
87+
display() {
88+
this.edgeDisplays.forEach(edgeDisplay => {
89+
edgeDisplay.display();
90+
});
91+
92+
this.nodeDisplays.forEach(nodeDisplay => {
93+
nodeDisplay.display();
94+
});
95+
}
96+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
This code and its output are licensed under the
3+
Creative Commons Attribution-NonCommercial-NoDerivatives
4+
4.0 International (CC BY-NC-ND 4.0) License.
5+
https://creativecommons.org/licenses/by-nc-nd/4.0/
6+
*/
7+
8+
// Gradient Graphs
9+
// Node Display Class
10+
// Author: Brittni Watkins
11+
12+
class NodeDisplay {
13+
constructor(positionPercentage, diameter, color) {
14+
this.positionPercentage = positionPercentage.copy();
15+
this.diameter = diameter;
16+
this.color = color;
17+
this.displayNode = true;
18+
this.pointPercentages = [];
19+
this.buildPoints();
20+
}
21+
22+
buildPoints() {
23+
let theta = 0;
24+
let numPoints = 20;
25+
let radius = this.diameter / 2.0;
26+
27+
for (let i = 0; i < numPoints; i++) {
28+
let baseX = this.positionPercentage.x * width;
29+
let baseY = this.positionPercentage.y * height;
30+
let x = baseX + (cos(theta) * radius);
31+
let y = baseY + (sin(theta) * radius);
32+
let xPercent = x / width;
33+
let yPercent = y / height;
34+
let percentVector = createVector(xPercent, yPercent);
35+
this.pointPercentages.push(percentVector);
36+
theta += TWO_PI / numPoints;
37+
}
38+
}
39+
40+
getColor() {
41+
return this.color;
42+
}
43+
44+
toggleDisplayNode() {
45+
this.displayNode = !this.displayNode;
46+
}
47+
48+
display() {
49+
if (this.displayNode) {
50+
this.displayShape();
51+
}
52+
}
53+
54+
displayShape() {
55+
beginShape();
56+
fill(this.color.getColor());
57+
noStroke();
58+
59+
this.pointPercentages.forEach(point => {
60+
vertex(point.x * width, point.y * height);
61+
});
62+
63+
endShape(CLOSE);
64+
}
65+
}

src/main/graph/edge.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
This code and its output are licensed under the
3+
Creative Commons Attribution-NonCommercial-NoDerivatives
4+
4.0 International (CC BY-NC-ND 4.0) License.
5+
https://creativecommons.org/licenses/by-nc-nd/4.0/
6+
*/
7+
8+
// Gradient Graphs
9+
// Edge Class
10+
// Author: Brittni Watkins
11+
12+
class Edge {
13+
constructor(a, b) {
14+
this.a = a;
15+
this.b = b;
16+
}
17+
18+
getA() {
19+
return this.a;
20+
}
21+
22+
getB() {
23+
return this.b;
24+
}
25+
26+
isEqual(node) {
27+
let equals = false;
28+
29+
if (node !== null) {
30+
let haveSameNodes = (this.a === node.getA()) && (this.b === node.getB());
31+
let haveDifferentNodes = (this.a === node.getB()) && (this.b === node.getA());
32+
equals = haveSameNodes || haveDifferentNodes;
33+
}
34+
35+
return equals;
36+
}
37+
}

src/main/graph/gabriel_graph.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
This code and its output are licensed under the
3+
Creative Commons Attribution-NonCommercial-NoDerivatives
4+
4.0 International (CC BY-NC-ND 4.0) License.
5+
https://creativecommons.org/licenses/by-nc-nd/4.0/
6+
*/
7+
8+
// Gradient Graphs
9+
// Gabriel Graph Class
10+
// Author: Brittni Watkins
11+
12+
class GabrielGraph extends Graph {
13+
constructor(numNodes) {
14+
super(new Scale(createVector(-0.5, -0.5), createVector(0.5, 0.5)));
15+
this.buildNodes(numNodes);
16+
this.initializeAdjacencyLists();
17+
this.buildAdjacencyList();
18+
}
19+
20+
buildNodes(numNodes) {
21+
for (let i = 0; i < numNodes; i++) {
22+
let node = this.buildRandomNode();
23+
this.nodes.push(node);
24+
}
25+
}
26+
27+
buildAdjacencyList() {
28+
for (let i = 0; i < this.nodes.length; i++) {
29+
let a = this.nodes[i];
30+
31+
for (let j = i + 1; j < this.nodes.length; j++) {
32+
let b = this.nodes[j];
33+
let hasIntersectingNode = this.hasIntersectingNode(a, b);
34+
35+
if (!hasIntersectingNode) {
36+
let aAdjacency = this.adjacencyList.get(a);
37+
let bAdjacency = this.adjacencyList.get(b);
38+
aAdjacency.push(b);
39+
bAdjacency.push(a);
40+
}
41+
}
42+
}
43+
44+
this.buildCompactAdjacencyList();
45+
this.buildEdges();
46+
}
47+
48+
hasIntersectingNode(a, b) {
49+
let hasIntersect = false;
50+
let diameter = Node.getDistance(a, b);
51+
let radius = diameter / 2.0;
52+
let center = Node.getCenterPoint(a, b);
53+
54+
for (let i = 0; i < this.nodes.length; i++) {
55+
let node = this.nodes[i];
56+
57+
if (node !== a && node !== b) {
58+
let nodePosition = node.getPosition();
59+
let distFromCenter = dist(center.x, center.y, nodePosition.x, nodePosition.y);
60+
61+
if (distFromCenter < radius) {
62+
hasIntersect = true;
63+
break;
64+
}
65+
}
66+
}
67+
68+
return hasIntersect;
69+
}
70+
}

0 commit comments

Comments
 (0)