fix direction dependent thichness

This commit is contained in:
lukas-heiligenbrunner 2022-11-03 11:22:38 +01:00
parent 019cd2dad0
commit 348886b5bb
3 changed files with 30 additions and 17 deletions

View File

@ -31,20 +31,26 @@ class PaintController extends ChangeNotifier {
}
double _calcAngleDependentWidth(Point pt1, Point pt2, double basetickness) {
double dx = pt2.point.dx - pt1.point.dx;
double dy = pt2.point.dy - pt1.point.dy;
final delta = pt2.point - pt1.point;
final normalizedDelta =
delta / sqrt(delta.dx * delta.dx + delta.dy * delta.dy);
// todo those deltas to small to get an accurate direction!
double alpha = atan(dx / dy);
// alpha has range from 0 - 2pi
// we want 0.5 -1;
alpha /= (2 * pi * 2);
double alpha = asin(normalizedDelta.dy);
// range [-pi,pi]
alpha += (3 * pi / 4);
// range [0,inf]
alpha = alpha % (2 * pi);
// range [0,2pi]
alpha -= pi;
// range [-pi,pi]
alpha = alpha.abs();
// range [0,pi]
alpha /= pi;
// range [0,1]
alpha += .5;
// range [.5,1.5]
basetickness * alpha;
return basetickness;
return basetickness * alpha;
}
void pointDownEvent(Offset offset, PointerDownEvent e) async {
@ -121,9 +127,11 @@ class PaintController extends ChangeNotifier {
if (pts.last.point == offset) return;
double newWidth = _calcTiltedWidth(5.0, event.tilt);
if (strokes.last.points.length > 1) {
if (pts.length > 1) {
newWidth = _calcAngleDependentWidth(
pts.last, pts[pts.length - 2], newWidth);
pts.last,
pts[pts.length - (pts.length > 5 ? 5 : pts.length - 1)],
newWidth);
}
Point p = Point(offset, newWidth);

View File

@ -26,7 +26,7 @@ class FileChangeNotifier extends ChangeNotifier {
return NoteTileData(name, filename, lastmodified);
}).toList();
dta.sort(
(a, b) => a.lastModified.isAfter(b.lastModified) ? -1 : 1,
(a, b) => a.lastModified.compareTo(b.lastModified),
);
notifyListeners();
tiledata = dta;

View File

@ -1,5 +1,6 @@
import 'dart:io';
import 'package:flutter/widgets.dart';
import 'package:sqflite/sqflite.dart';
import 'path.dart';
@ -19,7 +20,7 @@ class NoteFile {
_db = await openDatabase(
path,
onCreate: (db, version) async {
Batch batch = db.batch();
final batch = db.batch();
batch.execute('DROP TABLE IF EXISTS strokes;');
batch.execute('DROP TABLE IF EXISTS points;');
@ -43,7 +44,11 @@ class NoteFile {
Future<void> close() async {
// shrink the db file size
if (_db.isOpen) {
await _db.execute('VACUUM');
await _db.close();
} else {
debugPrint('db file unexpectedly closed before shrinking');
}
}
}