Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 63701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:40:26+00:00 2026-05-10T18:40:26+00:00

I have 3 points (A, B and X) and a distance (d). I need

  • 0

I have 3 points (A, B and X) and a distance (d). I need to make a function that tests if point X is closer than distance d to any point on the line segment AB.

The question is firstly, is my solution correct and then to come up with a better (faster) solution.

My first pass is as follows

AX = X-A BX = X-B AB = A-B      // closer than d to A (done squared to avoid needing to compute the sqrt in mag) If d^2 > AX.mag^2  return true      // closer than d to B If d^2 > BX.mag^2 return true      // 'beyond'  B If (dot(BX,AB) < 0) return false      // 'beyond'  A If (dot(AX,AB) > 0) return false      // find component of BX perpendicular to AB Return (BX.mag)^2 - (dot(AB,BX)/AB.mag)^2 < d^2 

This code will end up being run for a large set of P’s and a large set of A/B/d triplets with the intent of finding all P’s that pass for at least one A/B/d so I suspect that there is a way to reduce overall the cost based on that but I haven’t looked into that yet.

(BTW: I am aware that some reordering, some temporary values and some algebraic identities could decrease the cost of the above. I just omitted them for clarity.)


EDIT: this is a 2D problem (but solution that generalizes to 3D would be cool

Edit: On further reflection, I expect the hit rate to be around 50%. The X point can be formed in a nested hierarchy so I expect to be able to prune large subtrees as all-pass and all-fail. The A/B/d limiting the triplets will be more of a trick.

Edit: d is in the same order of magnitude as AB.


edit: Artelius posted a nice solution. I’m not sure I understand exactly what he’s getting at as I got off on a tangent before I fully understood it. Anyway another thought came to mind as a result:

  • First Artelius’ bit, pre-cacluate a matrix that will place AB centered ate the origin and aligned with the X-axis. (2 adds, 4 muls and 2 adds)
  • fold it all into the 1st quadrant (2 abs)
  • scale in X&Y to make the central portion of the zone into a unit square (2 mul)
  • test if the point is in that square (2 test) is so quit
  • test the end cap (go back to the unscaled values
    • translate in x to place the end at the origin (1 add)
    • square and add (2 mul, 1 add)
    • compare to d^2 (1 cmp)

I’m fairly sure this beats my solution.

(if nothing better comes along sone Artelius gets the ‘prize’ 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:40:26+00:00Added an answer on May 10, 2026 at 6:40 pm

    If your set of (A,B,d) in fixed, you can calculate a pair of matrices for each to translate the co-ordinate system, so that the line AB becomes the X axis, and the midpoint of AB is the origin.

    I think this is a simple way to construct the matrices:

    trans = - ((A + B) / 2)        // translate midpoint of AB to origin  rot.col1 = AB / AB.mag         // unit vector in AB direction                          0 -1     rot.col2 = rot.col1 * (      ) // unit vector perp to AB                         1  0   rot = rot.inverse()            // but it needs to be done in reverse 

    Then you just take each X and do rot * (X + trans).

    The region in question is actually symmetric in both the x and y axes now, so you can take the absolute value of the x co-ordinate, and of the y co-ordinate.

    Then you just need to check:

    y < d && x < AB.mag/2            //'along' the line segment || (x - AB.mag/2)^2 + y^2 < d^2  // the 'end cap'. 

    You can do another trick; the matrix can scale down by a factor of AB.mag/2 (remember the matrices are only calculated once per (A,B) meaning that it’s better that finding them is slower, than the actual check itself). This means your check becomes:

    y < 2*d/AB.mag && x < 1 || (x - 1)^2 + y^2 < (2*d/AB.mag)^2 

    Having replaced two instances of AB.mag/2 with the constant 1, it might be a touch faster. And of course you can precalculate 2*d/AB.mag and (2*d/AB.mag)^2 as well.

    Whether this will work out faster than other methods depends on the inputs you give it. But if the length of AB is long compared to d, I think it will turn out considerably faster than the method you posted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 81k
  • Answers 81k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This article shows you can do it with socket and… May 11, 2026 at 4:34 pm
  • Editorial Team
    Editorial Team added an answer try this way: def save = { def post =… May 11, 2026 at 4:34 pm
  • Editorial Team
    Editorial Team added an answer I've got a batch file for signing. Not sure how… May 11, 2026 at 4:34 pm

Related Questions

I have three X/Y points that form a parabola. I simply need to calculate
I have the following table: <table> <tr> <td style=height: 7px; width: 7px> A1 </td>
I have two unsorted lists and I need to produce another list which is
What's a good algorithm to solve this problem? I have three groups of people

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.