To determine if a point lies on a line [duplicate] - java

This question already has answers here:
Check is a point (x,y) is between two points drawn on a straight line
(11 answers)
Closed 9 years ago.
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

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 !

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);
}

Related

Imagine slicing a polygon like a pizza haphazardly, how would you find the largest slice?

I have a polygon that have been divided up with multiple lines, creating new smaller polygons that make up the whole. How would I go about finding the slice with the largest area?
Imagine something like this: sliced polygon green points are vertices, lines intersect creating more vertices, looking for the largest yellow marked area.
I figured this could be solved by generating a directed graph in order to define each unique shape. I can't come up with a way to link all vertices correctly though.
All vertices and edge lengths are either given or calculated with these methods.
public static double calcDistanceBetweenPoints(Point a, Point b){
return Math.sqrt((b.y - a.y) * (b.y - a.y) + (b.x - a.x) * (b.x - a.x));
}
public static Point findIntersection(Point A, Point B, Point C, Point D){
// Line AB represented as a1x + b1y = c1
double a1 = B.y - A.y;
double b1 = A.x - B.x;
double c1 = a1*(A.x) + b1*(A.y);
// Line CD represented as a2x + b2y = c2
double a2 = D.y - C.y;
double b2 = C.x - D.x;
double c2 = a2*(C.x)+ b2*(C.y);
double determinant = a1*b2 - a2*b1;
if (determinant == 0)
{
// The lines are parallel
return new Point(Double.MAX_VALUE, Double.MAX_VALUE);
}
else
{
double x = (b2*c1 - b1*c2)/determinant;
double y = (a1*c2 - a2*c1)/determinant;
return new Point(x, y);
}
}
Data collected from the System.in input stream looks something like this:
n m
x1 y1
x.. y...
xn yn
x11 y11 x21 y21
x.. y.. x.. y..
x1m y1m x2m y2m
So I get all the starting and ending points for each line from the input.
Thanks for the help

Math.sqrt one works one doesn't why? [duplicate]

This question already has answers here:
What does the ^ operator do in Java?
(19 answers)
Closed 4 years ago.
Add the following method to the Point class:
public double distance(Point other)
Returns the distance between the current Point object and the given other Point object. The distance between two points is equal to the square root of the sum of the squares of the differences of their x- and y-coordinates. In other words, the distance between two points (x1, y1) and (x2, y2) can be expressed as the square root of (x2 - x1)2 + (y2 - y1)2. Two points with the same (x, y) coordinates should return a distance of 0.0.
public class Point {
int x;
int y;
// // your code goes here
}
This works:
public double distance(Point other){
return Math.sqrt((this.x-other.x)*(this.x-other.x) + (this.y-other.y)*(this.y-other.y));
}
This doesn't:
public double distance(Point other){
return Math.sqrt((this.x-other.x)^2 + (this.y-other.y)^2);
}
May I ask why? TY
To get this out of the list of unanswered questions and because I think it is not a typo:
^ is not "power-of", it is XOR.

Find points on triangle

I have 3 vertices of a triangle and I'm trying to find all the integer points that lie on the inside and ON THE SIDES of the triangle. I've tried numerous methods and they all succeed in finding the inside points, but fail in finding the points on the sides of the triangle. Currently I'm using barycentric coordinates:
private static boolean pointInTriangle(int[] p, int[] c1, int[] c2, int[] c3){
float alpha = ((c2[1] - c3[1])*(p[0] - c3[0]) + (c3[0] - c2[0])*(p[1] - c3[1])) /
((c2[1] - c3[1])*(c1[0] - c3[0]) + (c3[0] - c2[0])*(c1[1] - c3[1]));
float beta = ((c3[1] - c1[1])*(p[0] - c3[0]) + (c1[0] - c3[0])*(p[1] - c3[1])) /
((c2[1] - c3[1])*(c1[0] - c3[0]) + (c3[0] - c2[0])*(c1[1] - c3[1]));
float gamma = 1.0f - alpha - beta;
return ( (alpha>=0.0f) && (beta>=0.0f) && (gamma>=0.0f) );
For example, for vertices (0,0),(0,10),(10,10) this does find (10,8) but it also finds (11,8) which is not correct.
Can somebody help me?
Thanks in advance!
Use the code you alreay have to find if a position is inside the triangle. Then for the other part, if a point is on the line or not..
I would do it like this..
Check by calculating the distance between 2 vertices at a time.
Lets say we have vertices a, b and c. And the point p.
Check if p is on the line between a and b.
This can be done by measuring the distance between a -> p and p -> b.
If those two distances equals the distance of a -> b then it is on the line. If p should be off the line the distance will be longer.
Here is a method to caluclate distance (pythagoran teorem):
private static double GetDistance(double x1, double y1, double x2, double y2)
{
double a = Math.abs(x1-x2);
double b = Math.abs(y1-y2);
return Math.sqrt(a * a + b * b);
}

Calculate a point which is perpendicular to a line

I have two points store in two variable, which forms a line. I want to find a point which is perpendicular to that line from one end point in that line.
Suppose I have two points P1(x1,y1) and P2(x2,y2) then i want to find a third point P3 such that line(P1-P2) is perpendicular to line(P2,P3) and intersect at P2.
First, the angle:
public static double angle (double x1, double y1, double x2, double y2) {
double xdiff = x1 - x2;
double ydiff = y1 - y2;
//double tan = xdiff / ydiff;
double atan = Math.atan2(ydiff, xdiff);
return atan;
}
To get the perpendicular, you must add PI/2 to the angle of the line defined by your two points.
Once you have that angle, the formula is:
x = interceptPt.x + sin(perp_angle) * distance;
y = interceptPt.y + cos(perp_angle) * distance;
If you want to use Java I can recommend to use JTS. Create a LineSegment and use the pointAlongOffset method. Given Points p1 and p2 the code would look like that:
// create LineSegment
LineSegment ls = new LineSegment(p1.getX(), p1.getY(), p2.getX(), p2.getY());
// perpendicular distance to line
double offsetDistance = 10;
// calculate Point right to start point
Coordinate startRight = ls.pointAlongOffset(0, offsetDistance);
// calculate Point left to start point
Coordinate startLeft = ls.pointAlongOffset(0, -offsetDistance);
// calculate Point right to end point
Coordinate endRight = ls.pointAlongOffset(1, offsetDistance);
// calculate Point left to end point
Coordinate endLeft = ls.pointAlongOffset(1, -offsetDistance);
ControlAltDel is already answered but he did a mistake, replaced cos to sin
x = interceptPt.x + cos(angle + 90) * distance;
y = interceptPt.y + sin(angle + 90) * distance;
x,y is point away from (interceptPt.x,interceptPt.y) at (distance) .
(interceptPt.x,interceptPt.y) is point in your line where perpendicular start to drawn.
angle = your line angle with horizontal axis
I got the answer at http://jsfiddle.net/eLxcB/2/
// Start and end point
var startX = 120
var startY = 150
var endX = 180
var endY = 130
R.circle(startX,startY,2);
// Calculate how far above or below the control point should be
var centrePointX = startX
var centrePointY = startY;
// Calculate slopes and Y intersects
var lineSlope = (endY - startY) / (endX - startX);
var perpendicularSlope = -1 / lineSlope;
var yIntersect = centrePointY - (centrePointX * perpendicularSlope);
// Draw a line between the two original points
R.path('M '+startX+' '+startY+', L '+endX+' '+endY);
// Plot some test points to show the perpendicular line has been found
R.circle(100, (perpendicularSlope * 100) + yIntersect, 2);
You can store your points in vec2d, then use some mathematical equations to get the perpendicular point.
vec2d getPerpendicularPoint(vec2d A, vec2d B, float distance)
{
vec2d M = (A + B) / 2;
vec2d p = A - B;
vec2d n = (-p.y, p.x);
int norm_length = sqrt((n.x * n.x) + (n.y * n.y));
n.x /= norm_length;
n.y /= norm_length;
return (M + (distance * n));
}

determinant of 3 points in java

I am creating a java application. I need to calculate the determinant of three point. I have calculated it:
static int determinant(Point point1, Point point2, Point point3) {
int x1 = point1.x;
int x2 = point2.x;
int x3 = point3.x;
int y1 = point1.y;
int y2 = point2.y;
int y3 = point3.y;
return (x1 * y2) + (x3 * y1) + (x2 * y3) - (x3 * y2) - (x2 * y1)
- (x1 * y3);
}
(I am not good in math)But, I found the following when I searched about it:
public int ccw(int p1, int p2, int p3)
{
return (xPoints[p2] - xPoints[p1])*(yPoints[p3] - yPoints[p1]) - (yPoints[p2] - yPoints[p1])*(xPoints[p3] - xPoints[p1]);
}
which one is the correct method? If the first method is the correct one, what does the second method do?
both equations are the same. there is no mathematical difference between the two.
someone better at math than I could probably give you the mathematic proof. I chose to go the practical route, implemented in python, because I can
def fun1(p1,p2,p3) :
print (p1[0]*p2[1])+(p3[0]*p1[1])+(p2[0]*p3[1]) - (p3[0]*p2[1]) - (p2[0]*
p1[1]) - (p1[0]*p3[1])
def fun2(p1,p2,p3) :
print (p2[0]-p1[0])*(p3[1]-p1[1])-(p2[1] - p1[1])*(p3[0]-p1[0])
>>> fun1((0,0),(9,1),(3,2))
15
>>> fun2((0,0),(9,1),(3,2))
15
>>> fun2((33,5),(4,6),(8,1))
141
>>> fun1((33,5),(4,6),(8,1))
141
for any 3 points, fun1 and fun2 will yield the same result.
The second version assumes that the points are defined in two one-dimensional arrays:
int [] xPoints;
int [] yPoints;
xPoints[p] holds the x position of point p
yPoints[p] holds the y position of point p
I haven't done the math, but perhaps the two methods actually calculate the same thing.

Categories