Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Determine if two points are on the same side of a line

Posted on 2014-03-01
3
Medium Priority
?
555 Views
Last Modified: 2014-03-01
I have two points representing a line:
33.792262, -84.305002
33.739889, -84.289553
I also have two points A and B:
A
33.748739, -84.354355
B
33.767649, -84.273760
What I need is a javascript function that will give true/false return if both points are on the same side of the line. If any or both points are on the line then it should return true.
I found this http://j.mp/1of7JUe solution but I need it written as javascript function and I myself don't know javascript programming.

Thank you.
0
Comment
Question by:pplb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
Pravin Asar earned 2000 total points
ID: 39898129
<script type="text/javascript">
          function Point2D (x,y) {
                this.x = x;
                this.y = y;
                return this;
          };
          
          function Vector2D(p1,p2) {
                this.x = p2.x-p1.x;
                this.y = p2.y-p1.y;
                return this;
          };
          
          function VectorMath() {
                this.dotProduct =function (v1,v2) { var dot= v1.x*v2.x+ v1.y*v2.y; return dot; };
                this.normalVector = function (v1) { t=v1.x; v1.x=v1.y; v1.y=-t; return v1; };
          };
          
          // Define Points
          p1 = new Point2D(33.792262, -84.305002);
          p2 = new Point2D(33.739889, -84.289553);
          p3 = new Point2D(33.748739, -84.354355);
          p4 = new Point2D(33.767649, -84.273760);
          // Define Vector for Base line
          v1 = new Vector2D(p1,p2);
            // Get Normal Vector
          v1 = (new VectorMath()).normalVector(v1);
            // Define Vector 1st Point to check
          v2 = new Vector2D(p1,p3);
            // Define Vector 1st Point to check
          v3 = new Vector2D(p1,p4);
          dot1 = (new VectorMath()).dotProduct (v1,v2);
          // Can you get normalized value
          dot1 = dot1 /Math.abs(dot1);
          dot2 = (new VectorMath()).dotProduct (v1,v3);
          // Can you get normalized value
            dot2 = dot2 /Math.abs(dot2);
            // Same Sign (Points on Same side) or different sign (Points on different side)
          if (dot1 == dot2) { alert ('Points on Same Side');} else { alert ('Points are not on same side');}
    </script>
0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 39898130
You may look at

http://sylvester.jcoglan.com/docs.html

This is a good library.
0
 

Author Closing Comment

by:pplb
ID: 39898176
Thank you.
0

Featured Post

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question