1

I am trying to find if a particular point lies on a line. What I have tried is . I find the slope of the line .Then I find the slope of the point with the initial coordinates of the line, say x1 and y1. IF both the slopes are equal then the point lies on the line .But because I am using double or even with float I get values with five decimal places and the slope are never equal. the code is as follows.

line.slope == (yTouch-line.yTouch1)/(xTouch-line.xTouch1)

Hi I am not able to add a comment.Can you please tell me what is an error delta

Akshay
  • 73
  • 2
  • 16

2 Answers2

1

Assume the coordinates of the point to be tested are (x,y)

If you know the equation (say y=mx+c) then just substitute x and y in and check for equality. eg is (3,1) on y=2x-5 ?

1 = 2(3)-5 = 1 so it is on the line

If you don't know the equation but have two points (x1,y1) and (x2,y2) then first calculate the slope m= (y2-y1)/(x2-x1) and then use the y-y1=m(x-x1) form of the equation of a line and check for equality again.

e.g. is the point (4,4) on the line thru (2,3) and (5,4) ?

m=(4-3)/(5-2)=1/3

the equation is y-3=(1/3)(x-2)

and ....well it isnt

Alternatively, just get a 14 year old in your country to explain it to you. In my experience 14 year olds (think they ) know everything !

IanB
  • 3,489
  • 1
  • 20
  • 24
0

Hey go with geometric terms, If your points are (x1, y1) and (x2, y2), and you want to find the point (x3, y3) that is n units away from point 2:

d = sqrt((x2-x1)^2 + (y2 - y1)^2) #distance
r = n / d #segment ratio

x3 = r * x2 + (1 - r) * x1 #find point that divides the segment
y3 = r * y2 + (1 - r) * y1 #into the ratio (1-r):r

Here's link! Here is solution

public static void checkForLineInaPoint(Double x1, Double y1, Double x2, Double y2)
{
    Double m = getSlope(x1,y1,x2,y2);
    Double c = getConstant(x1,y1,x2,y2,m);
    Double x3= new Double(10.001);
    Double y3= new Double(10.001);
    if(checkPointLiesonLine(x3,y3,m,c))
        System.out.print("Yes");
    else
        System.out.print("No");
}

private static boolean checkPointLiesonLine(Double x3, Double y3, Double m, Double c)
{
    Double temp = m*x3+c;
    return temp.compareTo(y3)==0;
}

private static Double getConstant(Double x1, Double y1, Double x2, Double y2, Double m)
{       
    return y1-m*x1;
}

private static Double getSlope(Double x1, Double y1, Double x2, Double y2)
{
    return (y2-y1)/(x2-x1);
}
Community
  • 1
  • 1
Selvaraj
  • 714
  • 2
  • 6
  • 14
  • 2
    I don't understand how this can help OP solve his problem `to find if a particular point lies on a line`..can you explain? – boxed__l Aug 05 '13 at 14:08
  • you have two points of line. And another point to find whether is in that line or not. rite? if you fine line.scope(in mathematical say 'm'). the line equation is `y - y1 = m(x - x1)`, substitute your third point in (x,y) in above equation, if LHS=RHS then the point in that line. Otherwise false. – Selvaraj Aug 05 '13 at 14:14
  • If u still understand then go by this link. [link](http://www.mathwarehouse.com/algebra/linear_equation/write-equation/equation-of-line-given-two-points.php) And make a simple program. :-> – Selvaraj Aug 05 '13 at 14:21