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

  • SEARCH
  • Home
  • 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 8372255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:22:56+00:00 2026-06-09T14:22:56+00:00

How would I write this function? Any examples appreciated function isPointBetweenPoints(currPoint, point1, point2):Boolean {

  • 0

How would I write this function? Any examples appreciated

function isPointBetweenPoints(currPoint, point1, point2):Boolean {

    var currX = currPoint.x;
    var currY = currPoint.y;

    var p1X = point1.x;
    var p1y = point1.y;

    var p2X = point2.x;
    var p2y = point2.y;

    //here I'm stuck
}
  • 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. Editorial Team
    Editorial Team
    2026-06-09T14:22:58+00:00Added an answer on June 9, 2026 at 2:22 pm

    Assuming that point1 and point2 are different, first you check whether the point lies on the line. For that you simply need a “cross-product” of vectors point1 -> currPoint and point1 -> point2.

    dxc = currPoint.x - point1.x;
    dyc = currPoint.y - point1.y;
    
    dxl = point2.x - point1.x;
    dyl = point2.y - point1.y;
    
    cross = dxc * dyl - dyc * dxl;
    

    Your point lies on the line if and only if cross is equal to zero.

    if (cross != 0)
      return false;
    

    Now, as you know that the point does lie on the line, it is time to check whether it lies between the original points. This can be easily done by comparing the x coordinates, if the line is “more horizontal than vertical”, or y coordinates otherwise

    if (abs(dxl) >= abs(dyl))
      return dxl > 0 ? 
        point1.x <= currPoint.x && currPoint.x <= point2.x :
        point2.x <= currPoint.x && currPoint.x <= point1.x;
    else
      return dyl > 0 ? 
        point1.y <= currPoint.y && currPoint.y <= point2.y :
        point2.y <= currPoint.y && currPoint.y <= point1.y;
    

    Note that the above algorithm if entirely integral if the input data is integral, i.e. it requires no floating-point calculations for integer input. Beware of potential overflow when calculating cross though.

    P.S. This algorithm is absolutely precise, meaning that it will reject points that lie very close to the line but not precisely on the line. Sometimes this is not what’s needed. But that’s a different story.

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

Sidebar

Related Questions

would someone please write this code: this.Loaded += (s, e) => this.loaded = true;
what would be the shortest way to write this? if(strpos($haystack, $needle)!==false){ $len = strpos($haystack,
I would like to know how to write this inside my hittest if statement:
This is as far as I've gotten. What code would I write to save
Not sure this is possible, but looking to write a script that would return
Do you know any library that will help doing that? I would write a
I can't seem to get my head around this, any help would be greatly
I would like to make qqplots through a list of dataframes. I wrote this
In Perl, I would write: $x = abbbc; $x =~ s/(b+)/z/; print Replaced $1
Would you write something like: enum XYZ_TYPE {X=1, Y=2, Z=3}; I saw it and

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.