Encontre o ponto de controle para o quadraticbeziersegment quando o início, o final e o 1 ponto deitado ao longo do bezier em interpolação c# - quadraticbezier de 3 pontos

StackOverflow https://stackoverflow.com/questions/2320956

  •  22-09-2019
  •  | 
  •  

Pergunta

Isso é semelhante a uma pergunta anterior que fiz sobre o cúbico Bezier. Eu tenho um ponto de partida, um ponto de extremidade e um ponto que deve estar ao longo de um Bezier quadrático. Dados esses três pontos, quero ser capaz de desenhar um quadraticbeziersegment no WPF, mas preciso do valor de controle único (no quadraticbeziersegment, é o ponto1) para desenhá -lo.

Existe um cálculo ou meios pelos quais eu posso determinar esse valor e, assim, desenhar meu quadraticbezier?

Obrigado!

Foi útil?

Solução

O melhor ajuste quadrático é mais simples que o melhor ajuste cúbico. Aqui está algum código:

static class DrawingUtility
{
    static void bez3pts1(double x0, double y0, double x3, double y3, double x2, double y2, out double x1, out double y1)
    {
        // find chord lengths
        double c1 = Math.Sqrt((x3 - x0) * (x3 - x0) + (y3 - y0) * (y3 - y0));
        double c2 = Math.Sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
        // guess "best" t
        double t = c1 / (c1 + c2);
        // quadratic Bezier is B(t) = (1-t)^2*P0 + 2*t*(1-t)*P1 + t^2*P2
        // solving gives P1 = [B(t) - (1-t)^2*P0 - t^2*P2] / [2*t*(1-t)] where P3 is B(t)
        x1 = (x3 - (1 - t) * (1 - t) * x0 - t * t * x2) / (2 * t * (1 - t));
        y1 = (y3 - (1 - t) * (1 - t) * y0 - t * t * y2) / (2 * t * (1 - t));
    }

    // pass in a PathFigure and it will append a QuadraticBezierSegment connecting the previous point to int1 and endPt
    static public void QuadraticBezierFromIntersection(PathFigure path, Point startPt, Point int1, Point endPt)
    {
        double x1, y1;
        bez3pts1(startPt.X, startPt.Y, int1.X, int1.Y, endPt.X, endPt.Y, out x1, out y1);
        path.Segments.Add(new QuadraticBezierSegment { Point1 = new Point(x1, y1), Point2 = endPt } );
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top