dart - 路径 quadraticBezierTo : curve pass the control point

标签 dart flutter bezier

我有一个看起来像这样的 CustomPainter:

class MyPainter extends CustomPainter {
  Offset left, top, right, bottom;

  MyPainter({this.left, this.top, this.right, this.bottom});

  @override
  void paint(Canvas canvas, Size size) {
    Paint pp = Paint()
      ..color = Colors.blue
      ..strokeCap = StrokeCap.round
      ..strokeWidth = 10;

    Paint p = Paint()
      ..color = Colors.red
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2;

    Path ph = Path();

    ph.moveTo(left.dx, left.dy);
    ph.quadraticBezierTo(top.dx, top.dy, right.dx, right.dy);

    canvas.drawPoints(PointMode.points, [left, top, right, bottom], pp);
    canvas.drawPath(ph, p);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

这是结果:https://imgur.com/a/m4i6WEA

但是我想要曲线,通过控制点。像这样:https://imgur.com/a/cumbAVz

我该怎么做?!

最佳答案

需要根据需要的点top计算内部控制点P1坐标

首先,粗略评估那个点的参数t。让它为 1/2(如果 top 位于曲线中间附近)。使用二次贝塞尔公式:

P(t)  = P0 * (1-t)^2 + 2 * P1 * t * (1-t) + P2 * t^2
top {for t=1/2} = P0 * 1/4 + P1 * 1/2 + P2 * 1/4
P1 = 2 * top - (P0 + P2) / 2

在组件中:

P1.dx = 2 * top.dx - (left.dx + right.dx) / 2
P1.dy = 2 * top.dy - (left.dy + right.dy) / 2

最后

 ph.quadraticBezierTo(P1.dx, P1.dy, right.dx, right.dy);

关于dart - 路径 quadraticBezierTo : curve pass the control point,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54319537/

相关文章:

function - 如何在 Dart 中的数值数据表值之间进行插值(实际上是任何语言)

flutter - 如何从Flutter中的Future <String>返回字符串

google-maps - Flutter:带有图像叠加的 map

ios - 使用贝塞尔曲线绘制螺旋

javascript - 滚动页面时弯曲的图像路径

ios - UITableViewCell 中的 mask 图像

database - FutureBuilder和ListView.builder

Dart 有什么方法可以从 stream.listen() 调用 onDone 回调,其中流来自 StreamController

image - 如何在 dart 的 canvas2d 中缓存稍后要绘制的图像?

flutter - CupertinoTabBar 导航