Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
Finding the shortest distance between a quadratic Bezier curve and point or rectangle

I am working on a simple whiteboard application where the drawings are represented by quadratic Bezier curves (using the JavaScript's CanvasPath.quadraticCurveTo function). I am trying to implement functionality so that an eraser tool or a selection tool are able to determine if they are touching a drawing.

To show what I'm talking about, in the following image is a red drawing and I need to be able to determine that the black rectangles and black point overlap with the area of the drawing. For debugging purposes I have added blue circles which are control points of the curve and the green line which is the same Bezier curve but with a much smaller width.

Quadratic Bezier curve with overlapping rectangles.

I have included my code which generates the Bezier curve:

        context.beginPath();
        context.moveTo(localPoints[0].x, localPoints[0].y);
        let i;
        for (i = 1; i < localPoints.length - 2; i++)
        {
            let xc = (localPoints[i].x + localPoints[i + 1].x) / 2;
            let yc = (localPoints[i].y + localPoints[i + 1].y) / 2;
            context.quadraticCurveTo(localPoints[i].x, localPoints[i].y, xc, yc);
        }
        // curve through the last two points
        context.quadraticCurveTo(localPoints[i].x, localPoints[i].y, localPoints[i + 1].x, localPoints[i + 1].y);
        context.stroke();

I have been able to find answers on how to determine if a line segment intersects a Bezier curve, but I have not been able to find how to determine if something does not intersect the actual curve but is close enough to be considered to overlap its "area". To do so, I figure that I need to just find the distance between the curve and the rectangle/point and then make sure than distance is less than the width used to draw the curve, but I am unsure on how to find that distance.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Some interesting articles/posts:

How to track coordinates on the quadraticCurve

https://coderedirect.com/questions/385964/nearest-point-on-a-quadratic-bezier-curve

And if it doesn't work maybe you can take a look at this library: https://pomax.github.io/bezierjs/

As suggested by Pomax in the comments the thing you're looking for is in the library and it looks like there is a proper explanation.

There is a live demo if you want to try it: https://pomax.github.io/bezierinfo/#projections
The source code of it is here: https://pomax.github.io/bezierinfo/chapters/projections/project.js

demo

To use it install it using the steps from GitHub: https://github.com/Pomax/bezierjs

Of course credit to Pomax for suggesting his library

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda