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

193
Visualizações
Finding distance and direction between point and a polygon in graph

I have a graph where I have a polygon and a point. The graph is developed using a JS library called canvasjs. I am trying to find out the direction and distance a point is outside the polygon. I have managed to know if the point is inside or outside the polygon but struggling to find out the direction and distance if a point is outside the polygon. Or, how much to add or subtract the distance and in what direction so that the point falls inside the polygon.

The logic that I used to know if the point is inside or outside the polygon is mentioned below so that it can help others.

function area($x1, $y1, $x2, $y2, $x3, $y3)
{
     return abs(($x1 * ($y2 - $y3) + $x2 * ($y3 - $y1) + $x3 * ($y1 - $y2)) / 2.0);
}
                  
                
function check($x1, $y1, $x2, $y2, $x3,$y3, $x4, $y4, $x, $y)
{
    // Calculate area of rectangle ABCD 
    $A = area($x1, $y1, $x2, $y2, $x3, $y3) + area($x1, $y1, $x4, $y4, $x3, $y3);
                  
   // Calculate area of triangle PAB 
    $A1 = area($x, $y, $x1, $y1, $x2, $y2);
                  
   // Calculate area of triangle PBC 
   $A2 = area($x, $y, $x2, $y2, $x3, $y3);
                  
   // Calculate area of triangle PCD
   $A3 = area($x, $y, $x3, $y3, $x4, $y4);
                  
    // Calculate area of triangle PAD 
    $A4 = area($x, $y, $x1, $y1, $x4, $y4);
                  
    // Check if sum of A1, A2, 
    // A3 and A4  is same as A 
    return ($A == $A1 + $A2 + $A3 + $A4);
  }
                
                
  if (check(661, 2000, 50, 2000, 50, 775, 280, 775, $x, $y))
  {
     echo "yes";
  }
  else
  {
     echo "no";
  }

Please comment if I need to add more information to my question so that is much clear.

enter image description here

about 4 years ago · Juan Pablo Isaza
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