Skip to content

Commit

Permalink
feature freeze 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MG-LSJ committed Jun 10, 2024
1 parent 2a7dc60 commit 88c6cae
Show file tree
Hide file tree
Showing 18 changed files with 681 additions and 233 deletions.
84 changes: 84 additions & 0 deletions lib/algorithms/bfs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import 'package:flutter/material.dart';
import 'package:visual_graphs/graph_editor/models/graph.dart';
import 'package:visual_graphs/helpers/queue.dart';

class Pair {
final Vertex v;
final Edge? e;

Pair(this.v, this.e);
}

class BreadthFirstSearch {
final Graph graph;
final Vertex start;

final QueueDS<Pair> queue = QueueDS();

final Set<Vertex> visited = {};
final Set<Vertex> seen = {};

BreadthFirstSearch({required this.graph, required this.start});

void search() async {
print("Starting BFS from ${start.label}");
await see(Pair(start, null));

while (queue.isNotEmpty) {
await Future.delayed(const Duration(seconds: 1));

final pair = queue.dequeue();
await visit(pair);

var neighbours = pair.v.neighbours;

for (final neighbour in neighbours.keys) {
if (!seen.contains(neighbour)) {
await see(Pair(neighbour, neighbours[neighbour]!.first));
}
}
}
}

Future see(Pair pair) async {
print("Seeing ${pair.v.label}");

queue.enqueue(pair);

seen.add(pair.v);

if (pair.e != null && pair.e?.component != null) {
pair.e?.component
?..color = Colors.orange
..hoverColor = Colors.orangeAccent
..hoverOut();

await Future.delayed(const Duration(milliseconds: 100));
}

pair.v.component
..color = Colors.orange
..hoverColor = Colors.orangeAccent
..onHoverExit();
}

Future visit(Pair pair) async {
print("Visiting ${pair.v.label} via ${pair.e}");

visited.add(pair.v);

if (pair.e != null && pair.e?.component != null) {
pair.e?.component
?..color = Colors.green
..hoverColor = Colors.greenAccent
..hoverOut();

await Future.delayed(const Duration(milliseconds: 100));
}

pair.v.component
..color = Colors.green
..hoverColor = Colors.greenAccent
..onHoverExit();
}
}
53 changes: 53 additions & 0 deletions lib/algorithms/dfs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:flutter/material.dart';
import 'package:visual_graphs/graph_editor/models/graph.dart';
import 'package:visual_graphs/helpers/stack.dart';

class DepthFirstSearch {
final Graph graph;
final Vertex start;
final StackDS<Vertex> stack = StackDS();

final Set<Vertex> visited = {};
final Set<Vertex> seen = {};

DepthFirstSearch({required this.graph, required this.start});

void search() async {
print("Starting DFS from ${start.label}");
see(start);

while (stack.isNotEmpty) {
await Future.delayed(const Duration(seconds: 1));

final vertex = stack.pop();
visit(vertex);

var neighbours = vertex.neighbours.keys.toList()
..sort((a, b) => a.id.compareTo(b.id));

await Future.delayed(const Duration(milliseconds: 100));
for (final neighbour in neighbours) {
if (!seen.contains(neighbour)) {
see(neighbour);
}
}
}
}

void see(Vertex vertex) {
print("Seeing ${vertex.label}");
stack.push(vertex);
seen.add(vertex);
vertex.component.paint = Paint()
..color = Colors.red
..style = PaintingStyle.fill;
}

void visit(Vertex vertex) {
print("Visiting ${vertex.label}");
visited.add(vertex);
vertex.component.paint = Paint()
..color = Colors.green
..style = PaintingStyle.fill;
}
}
4 changes: 2 additions & 2 deletions lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:visual_graphs/game_screen.dart';
import 'package:visual_graphs/graph_editor/graph_editor.dart';
import 'package:flutter/material.dart';

class App extends StatelessWidget {
Expand All @@ -7,7 +7,7 @@ class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: const GameScreen(),
home: GraphEditorWidget(),
title: "Visual Graphs",
theme: ThemeData(
useMaterial3: true,
Expand Down
118 changes: 0 additions & 118 deletions lib/game_screen.dart

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import 'dart:math' as math;
import 'dart:ui' as ui;
import 'package:visual_graphs/components/graph_game.dart';
import 'package:visual_graphs/models/graph.dart';
import 'package:visual_graphs/graph_editor/components/graph_game.dart';
import 'package:visual_graphs/graph_editor/models/graph.dart';
import 'package:flame/components.dart';
import 'package:flutter/material.dart';
import 'package:visual_graphs/widgets/edge_info_box.dart';
import 'package:visual_graphs/graph_editor/widgets/edge_info_box.dart';

class EdgeComponent extends ShapeComponent with HasGameRef<GraphGame> {
Edge edge;
Color color = Colors.white;
Color hoverColor = Colors.white;
Color _color = const Color(0x00000000);

Path path = Path();
double labelTextSize = 14;
List<Offset> pathPoints = [];

Color _paintColor = const Color(0x00000000);
final PaintingStyle _paintPaintingStyle = PaintingStyle.stroke;
double _paintStrokeWidth = 2;

EdgeComponent(this.edge) {
_color = color;
_paintColor = color;
anchor = Anchor.topLeft;
position = edge.from.component.position;
paint = Paint()
..color = _color
..style = PaintingStyle.stroke
..strokeWidth = 2;
}

@override
Expand Down Expand Up @@ -61,25 +61,27 @@ class EdgeComponent extends ShapeComponent with HasGameRef<GraphGame> {
}

void hoverIn() {
_color =
_paintColor =
gameRef.gameMode == GameMode.deleteComponent ? Colors.red : hoverColor;
paint
..color = _color
..strokeWidth = 3;
_paintStrokeWidth = 3;

labelTextSize = 16;
}

void hoverOut() {
_color = color;
paint
..color = _color
..strokeWidth = 2;
_paintColor = color;
_paintStrokeWidth = 2;

labelTextSize = 14;
}

// rendering
@override
void render(Canvas canvas) {
paint = Paint()
..color = _paintColor
..style = _paintPaintingStyle
..strokeWidth = _paintStrokeWidth;
if (edge.isSelfEdge) {
renderSelfEdge(canvas);
} else {
Expand Down Expand Up @@ -170,7 +172,7 @@ class EdgeComponent extends ShapeComponent with HasGameRef<GraphGame> {
(index + 1) * edge.from.component.radius * math.pow(0.975, index);
path = Path();

var center = gameRef.graph.center;
var center = gameRef.graph.geometricCenter;
var centerOffset = Offset(
center.x - edge.from.component.position.x,
center.y - edge.from.component.position.y,
Expand Down Expand Up @@ -232,7 +234,7 @@ class EdgeComponent extends ShapeComponent with HasGameRef<GraphGame> {
var paragraphFontSize = labelTextSize;
var fontSize = paragraphFontSize + 2;
var fontWeight = FontWeight.normal;
var fontColor = _color;
var fontColor = _paintColor;
var text = edge.weight.toString();

final paragraphBuilder = ui.ParagraphBuilder(
Expand Down
Loading

0 comments on commit 88c6cae

Please sign in to comment.