Graphs_dart/flutter/lib/curve_painter.dart

400 lines
12 KiB
Dart

import 'package:arrow_path/arrow_path.dart';
import 'package:graphs/src/graph.dart';
import 'package:flutter/material.dart';
import 'dart:math';
class CurvePainter extends CustomPainter {
CurvePainter({
Key? key,
required this.graphData,
required this.bfsPath,
required this.dfsStart,
required this.dfsAccessTable,
});
List<int>? bfsPath;
List<bool>? dfsAccessTable;
int? dfsStart;
Graphs graphData;
final double dotRad = 6;
final double lineWidth = 2;
final Color lineColor = Colors.black;
final double aboveHeight = 5;
double circleRad = 100;
final TextStyle textStyle = TextStyle(
color: Colors.green.shade900,
fontSize: 20,
);
void drawLine(Canvas canvas, Offset p1, Offset p2) {
Paint p = Paint();
p.color = lineColor;
p.strokeWidth = lineWidth;
canvas.drawLine(p1, p2, p);
}
void drawDot(Canvas canvas, Offset p1) {
var p = Paint();
p.color = Colors.yellow.shade900;
p.strokeWidth = lineWidth + 2;
canvas.drawCircle(p1, dotRad, p);
}
void drawSelfConnect(Canvas canvas, Offset p1) {
var p = Paint();
p.color = lineColor;
p.strokeWidth = lineWidth;
p.style = PaintingStyle.stroke;
canvas.drawCircle(Offset(p1.dx + dotRad + 20, p1.dy), dotRad + 20, p);
}
TextSpan _getTextSpan(String s) => TextSpan(text: s, style: textStyle);
TextPainter _getTextPainter(String s) => TextPainter(
text: _getTextSpan(s),
textDirection: TextDirection.ltr,
textAlign: TextAlign.center);
void drawDotNames(Canvas canvas, Offset place, String s) {
var textPainter = _getTextPainter(s);
textPainter.layout();
textPainter.paint(
canvas,
Offset((place.dx - textPainter.width),
(place.dy - textPainter.height) - aboveHeight));
}
void drawAboveText(Canvas canvas, Offset size, String s) {
var textPainter = _getTextPainter(s);
textPainter.layout();
textPainter.paint(
canvas,
Offset((size.dx - textPainter.width),
(size.dy - textPainter.height) - aboveHeight));
}
int getHighInputConnections() {
if (graphData.getDots().length != 1 && graphData.getDots().length <= 3) {
return -1;
}
int higest = -1;
for (var i in graphData.getDots()) {
if (i.getL().length > higest) higest = i.num;
}
return higest;
}
Map<int, Offset> getDotPos(int dotsAm, Size size, [int? exclude]) {
Map<int, Offset> off = <int, Offset>{};
var width = size.width / 2;
var height = size.height / 2;
int add = 0;
int h = getHighInputConnections();
for (int i = 0; i < dotsAm; i++) {
if ((i + 1) != h) {
double x = cos(2 * pi * (i - add) / (dotsAm - add)) * circleRad + width;
double y =
sin(2 * pi * (i - add) / (dotsAm - add)) * circleRad + height;
off[i + 1] = Offset(x, y);
} else if ((i + 1) == h) {
off[i + 1] = Offset(width + 2, height - 2);
add = 1;
h = 0;
} else {
print("GetDotPos error");
}
//print(off.length);
}
//print(off);
return off;
}
void drawHArrow(Canvas canvas, Size size, Offset from, Offset to,
[bool doubleSided = false]) {
Path path;
// The arrows usually looks better with rounded caps.
Paint paint = Paint()
..color = Colors.black
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round
..strokeWidth = lineWidth;
var length = sqrt((to.dx - from.dx) * (to.dx - from.dx) +
(to.dy - from.dy) * (to.dy - from.dy));
/// Draw a single arrow.
path = Path();
path.moveTo(from.dx, from.dy);
path.relativeCubicTo(
0,
0,
-(from.dx + to.dx) / (length * 2) - 40,
-(from.dy + to.dy) / (length * 2) - 40,
to.dx - from.dx,
to.dy - from.dy);
path =
ArrowPath.make(path: path, isDoubleSided: doubleSided, tipLength: 16);
canvas.drawPath(path, paint);
}
void drawHighArrow(Canvas canvas, Size size, Offset from, Offset to,
[bool doubleSided = false]) {
Path path;
// The arrows usually looks better with rounded caps.
Paint paint = Paint()
..color = Colors.black
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round
..strokeWidth = lineWidth;
var length = sqrt((to.dx - from.dx) * (to.dx - from.dx) +
(to.dy - from.dy) * (to.dy - from.dy));
/// Draw a single arrow.
path = Path();
path.moveTo(from.dx, from.dy);
path.relativeCubicTo(
0,
0,
(from.dx + to.dx) / (length * 3) + 40,
(from.dy + to.dy) / (length * 2) + 40,
to.dx - from.dx,
to.dy - from.dy);
path = ArrowPath.make(
path: path,
isDoubleSided: doubleSided,
tipLength: 13,
isAdjusted: false);
canvas.drawPath(path, paint);
}
void drawConnections(
Canvas canvas, Size size, List<Dot> dots, Map<int, Offset> off) {
for (var i in dots) {
var list = i.getL();
var beg = off[i.num];
for (var d in list.keys) {
if (d == i.num) {
drawSelfConnect(canvas, off[d]!);
} else {
if (graphData.getDoubleSidedBool()) {
if (d > i.num) {
drawHArrow(canvas, size, beg!, off[d]!, false);
if (graphData.getUseLengthBool()) {
drawDotNames(
canvas,
Offset((off[d]!.dx + beg.dx) / 2 - 18,
(off[d]!.dy + beg.dy) / 2 - 18),
i.getL()[d].toString());
}
} else {
drawHighArrow(canvas, size, beg!, off[d]!, false);
if (graphData.getUseLengthBool()) {
drawDotNames(
canvas,
Offset((off[d]!.dx + beg.dx) / 2 + 30,
(off[d]!.dy + beg.dy) / 2 + 30),
i.getL()[d].toString());
}
}
} else {
drawLine(canvas, beg!, off[d]!);
if (graphData.getUseLengthBool()) {
drawDotNames(
canvas,
Offset((off[d]!.dx + beg.dx) / 2, (off[d]!.dy + beg.dy) / 2),
i.getL()[d].toString());
}
}
}
}
}
}
@override
void paint(Canvas canvas, Size size) {
if (size.width > size.height) {
circleRad = size.height / 3;
} else {
circleRad = size.width / 3;
}
//var paint = Paint();
//drawLine(canvas, Offset(0, size.height / 2),
//Offset(size.width, size.height / 2));
//gr = getGraph();
//int higest = getHighConnections();
//if (higest > -1) {
var off = getDotPos(graphData.getDotAmount(), size); //, higest);
//off[higest] = Offset(size.width / 2, size.height / 2);
for (int i in off.keys) {
drawDot(canvas, off[i]!);
drawDotNames(
canvas, off[i]!, "${graphData.getDots()[i - 1].getName()}:[$i]");
}
//var g = gr.getNoRepeatDots();
//print(g);
var g = graphData.getDots();
drawConnections(canvas, size, g, off);
//pringArr(canvas, size);
//drawArrow(canvas, Offset(size.width / 2, size.height / 2),
// Offset(size.width / 2 + 50, size.height / 2 + 200));
//}
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
/*void _drawTextAt(String txt, Offset position, Canvas canvas) {
final textPainter = getTextPainter(txt);
textPainter.layout(minWidth: 0, maxWidth: 0);
Offset drawPosition =
Offset(position.dx, position.dy - (textPainter.height / 2));
textPainter.paint(canvas, drawPosition);
}*/
}
void pringArr(Canvas canvas, Size size) {
TextSpan textSpan;
TextPainter textPainter;
Path path;
// The arrows usually looks better with rounded caps.
Paint paint = Paint()
..color = Colors.black
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round
..strokeWidth = 3.0;
/// Draw a single arrow.
path = Path();
path.moveTo(size.width * 0.25, size.height * 0.10);
path.relativeCubicTo(0, 0, size.width * 0.25, 50, size.width * 0.5, 0);
path = ArrowPath.make(path: path);
canvas.drawPath(path, paint..color = Colors.blue);
textSpan = const TextSpan(
text: 'Single arrow',
style: TextStyle(color: Colors.blue),
);
textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
textPainter.layout(minWidth: size.width);
textPainter.paint(canvas, Offset(0, size.height * 0.06));
/*
/// Draw a double sided arrow.
path = Path();
path.moveTo(size.width * 0.25, size.height * 0.2);
path.relativeCubicTo(0, 0, size.width * 0.25, 50, size.width * 0.5, 0);
path = ArrowPath.make(path: path, isDoubleSided: true);
canvas.drawPath(path, paint..color = Colors.cyan);
textSpan = const TextSpan(
text: 'Double sided arrow',
style: TextStyle(color: Colors.cyan),
);
textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
textPainter.layout(minWidth: size.width);
textPainter.paint(canvas, Offset(0, size.height * 0.16));
/// Use complex path.
path = Path();
path.moveTo(size.width * 0.25, size.height * 0.3);
path.relativeCubicTo(0, 0, size.width * 0.25, 0, size.width * 0.5, 50);
path.relativeCubicTo(0, 0, -size.width * 0.25, 50, -size.width * 0.5, 50);
//path.relativeCubicTo(0, 0, size.width * 0.125, 10, size.width * 0.25, -10);
path = ArrowPath.make(path: path, isDoubleSided: true);
canvas.drawPath(path, paint..color = Colors.blue);
textSpan = const TextSpan(
text: 'Complex path',
style: TextStyle(color: Colors.blue),
);
textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
textPainter.layout(minWidth: size.width);
textPainter.paint(canvas, Offset(0, size.height * 0.28));
/// Draw several arrows on the same path.
path = Path();
path.moveTo(size.width * 0.25, size.height * 0.53);
path.relativeCubicTo(0, 0, size.width * 0.25, 50, size.width * 0.5, 50);
path = ArrowPath.make(path: path);
path.relativeCubicTo(0, 0, -size.width * 0.25, 0, -size.width * 0.5, 50);
path = ArrowPath.make(path: path);
Path subPath = Path();
subPath.moveTo(size.width * 0.375, size.height * 0.53 + 100);
subPath.relativeCubicTo(0, 0, size.width * 0.125, 10, size.width * 0.25, -10);
subPath = ArrowPath.make(path: subPath, isDoubleSided: true);
path.addPath(subPath, Offset.zero);
canvas.drawPath(path, paint..color = Colors.cyan);
textSpan = const TextSpan(
text: 'Several arrows on the same path',
style: TextStyle(color: Colors.cyan),
);
textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
textPainter.layout(minWidth: size.width);
textPainter.paint(canvas, Offset(0, size.height * 0.49));
/// Adjusted
path = Path();
path.moveTo(size.width * 0.1, size.height * 0.8);
path.relativeCubicTo(0, 0, size.width * 0.3, 50, size.width * 0.25, 75);
path = ArrowPath.make(path: path, isAdjusted: true);
canvas.drawPath(path, paint..color = Colors.blue);
textSpan = const TextSpan(
text: 'Adjusted',
style: TextStyle(color: Colors.blue),
);
textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,
);
textPainter.layout();
textPainter.paint(canvas, Offset(size.width * 0.2, size.height * 0.77));
/// Non adjusted.
path = Path();
path.moveTo(size.width * 0.6, size.height * 0.8);
path.relativeCubicTo(0, 0, size.width * 0.3, 50, size.width * 0.25, 75);
path = ArrowPath.make(path: path, isAdjusted: false);
canvas.drawPath(path, paint..color = Colors.blue);
textSpan = const TextSpan(
text: 'Non adjusted',
style: TextStyle(color: Colors.blue),
);
textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,
);
textPainter.layout();
textPainter.paint(canvas, Offset(size.width * 0.65, size.height * 0.77));*/
}