Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!