Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
Get distance between two SVG paths javascript

I have two SVG paths, a and b, that I want to find the distance between. Distance varies along each point, so in my case, I want to find the smallest distance between these two paths. Is there a way to do this using javascript?

path {
  fill: none;
}
<svg xmlns="http://www.w3.org/2000/svg">
  <path d="M0,0 Q30,30,0,60" stroke="black"></path>
  <path d="M70,0 Q30,30,70,60" stroke="black"></path>
  <path d="M15,30 L50,30" stroke="red"></path>
</svg>

In this example, I define the longest path to be the red line.

As an addition, is there any way to find the direction that this is pointing? For example, if the distance method is path1.distance(path2), could i get the distance as represented by a vector from the point on path1, to the point on path2? If this is not in the scope of your answer, that is fine.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  • Iterate over path a calling getPointAtLength on path a

  • Within the above loop, iterate over path b calling getPointAtLength on path b

  • Use pythagoras theorem to determine the distance between the points in the inner and outer loops. You have the vector because you have the pairs of points.

  • Whatever is the smallest distance is your answer.

  • If you increase the granularity of iteration your answer will be more accurate but take longer.

  • Note that if your paths are parallel lines then there may be an infinite number of nearest points.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda