Decide if a circle intersects with an infinite line - java

So, I need to check if a circle is intersected by a line algebraically. I've attempted to do this by taking a perpendicular line to the infinite that passes through the center of the circle. I then measure the perpendicular against the radius of the circle, and it states that the line does not intersect if d > r.
import java.util.Scanner;
public class LineCircle_Intersection {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double p1x, p2x, p1y, p2y, cx, cy, r;
System.out.print("Enter p1x: ");
p1x = in.nextDouble();
System.out.print("Enter p1y: ");
p1y = in.nextDouble();
System.out.print("Enter p2x: ");
p2x = in.nextDouble();
System.out.print("Enter p2y: ");
p2y = in.nextDouble();
System.out.print("Enter cx: ");
cx = in.nextDouble();
System.out.print("Enter cy: ");
cy = in.nextDouble();
System.out.print("Enter r: ");
r = in.nextDouble();
double m = (p2y - p1y) / (p2x - p1x);
double pem = -1 / m;
double pey = pem + p1y; // pe = perpendicular line (used E instead of L because lowercase l looks too much like 1)
double pex = (pey - p1y) / pem;
double d = Math.sqrt((pex - cx) * (pex - cx) + (pey - cy) * (pey - cy));
if (d <= r) {
if (d == r) {
System.out.println("Line intersects the circle at one point.");
} else {
System.out.println("Line intersects the circle at two points.");
}
} else if (m == 1) {
if (d <= r) // There's a problem in this area. I'm not sure what, or how to fix it.
{
if (d == r) {
System.out.println("The line intersects the circle at one point.");
} else {
System.out.println("Line intersects the circle at two points.");
}
} else {
System.out.println("Line does not intersect the circle.");
}
} else {
System.out.println("Else."); //This says "Else" for testing purposes.
}
}
}
Here's where things start to go wrong. There are several points that can be input that clearly should intersect or not intersect, but the program frequently says otherwise.
Will be working on this for a few hours, so if I solve it before someone else I'll post an update and how I solved it.

The principle is correct, but I won't verify your computations.
In short:
boolean intersects=false, is_tangent=false;
double p2p1x=p2x-p1x, p2p1y=p2y-p1y;
double p1p2DistSq=p2p1x*p2p1x+p2p1y*p2p1y;
if(p1p2DistSq > 1e-12) { // well-behaved line
double p1cx=p1x-cx, p1cy=p1y-cy;
double crossprod=p2p1x*p1cy-p2p1y*p1cx;
double distCenterToLineSquare=crossprod*crossprod/p1p2DistSq;
double rSquare=r*r;
intersects = (distCenterToLineSquare <= rSquare); // r is radius
// for practical purposes, if the relative error of
// (r-dist) is 1e-6 of r, we might consider the line as tangent.
is_tangent = Math.abs(distCenterToLineSquare - rSquare)/rSquare < 1e-12;
} // cowardly refusing to deal with ill-configured lines
In details:
Preliminary (and take this as an easy way to deduce the distance between a point and a line if you don't have internet at the moment)
The cross-product between two vectors
{ax, ay} x {bx, by} = |a|*|b|*sin(angle_between_dir_a_and_b)
Also this cross-product is (ax*by-ay*bx)
Now, assume a line passing through P1 with direction defined by the unitary vector {ux, uy}. The distance of a point {cx. cy} to this line will be
dist=sin(alpha)*|P1-C|
where |P1-C| is the distance between C and P1 and alpha is the angle between the direction {ux, uy} and the direction of the line {P1,C}. Let's denote with {vx,vy} the unitary direction of {P1,C} line. In this case, since u and v are unitary (|u|=|v|=1)
sin(alpha)=ux*vy-uy*vx
Thus
dist=(ux*vy-uy*vx)*|P1-C|
Plugging in the vx, vy using
vx=(P1x-Cx)/|P1-C| // it's a unitary vector
vy=(P1y-Cy)/|P1-C|
results in
dist=ux*(P1y-Cy)-uy*(P1x-Cx)
Now, the only thing that remains, is ux and uy. Since your line is defined by P1 and P2
ux=(P2x-P1x)/|P1-P2|
uy=(P2y-P1y)/|P1-P2|
(with |P1-P2| again, the distance between P1 and P2)
dist=( (P2x-P1x)*(P1y-Cy)-(P2y-P1y)*(P1x-Cx) )/ |P1-P2|
Ok, |P1-P2| requires an sqrt evaluation and we can get rid of it by comparing your dist^2 with the radius^2
The 'line intersect circle' then becomes
double p1cx=p1x-cx, p1cy=p1y-cy;
double p2p1x=p2x-p1x, p2p1y=p2y-p1y;
double crossprod=p2p1x*p1cy-p2p1y*p1cx;
double distCenterToLineSquare=crossprod*crossprod/(p2p1x*p2p1x+p2p1y*p2p1y);
boolean intersects= (distCenterToLineSquare <= r*r); // r is radius

Related

How to make a polygon attach to a point at a 3 o'clock position

I'm working on a project and I have most of it done but I'm having trouble seeing how to get the coordinates to line up. I'm stuck and I'm not sure how to get a point to be at 3 o'clock and I'm stuck. I've tried finding examples but all I see is polygons that don't need to lineup with anything. Any help?
The instructions: Suppose an n-sided regular polygon is centered at (0, 0) with one point at the 3 o’clock position, as shown in Figure 5.4. Write a program that prompts the user to enter the number of the sides, the radius of the bounding circle of a polygon, and displays the coordinates of the corner points on the polygon.
import java.util.Scanner;
public class Polygon {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of sides: ");
int sides = input.nextInt();
System.out.print("Enter the radius of the bounding circle: ");
double radius = input.nextDouble();
input.close();
System.out.println("The coordinates of the points on the polygon are");
for (int i = 0; i < sides; i++) {
double x = radius * java.lang.Math.sin(2.0 * java.lang.Math.PI / sides * i);
double y = radius * java.lang.Math.cos(2.0 * java.lang.Math.PI / sides * i);
System.out.print("(");
System.out.printf("%.2f", x);
System.out.print(" ");
System.out.printf("%.2f",y);
System.out.print(")");
System.out.println();
}
}
}
You need to switch your sin and cos expressions. The the first point of your polygon will then always lie at (radius, 0), i.e. aligned with the 3-o'clock position.
double x = radius * java.lang.Math.cos(2.0 * java.lang.Math.PI / sides * i);
double y = radius * java.lang.Math.sin(2.0 * java.lang.Math.PI / sides * i);

Defining height of triangle in coordinate system

I'm writing exercise program with user input of three points in coordinate system, so it get's you length of all sides of given triangle, so as perimeter and area. My goal is to define height of given triangle, so that i can tell if input points equals to straight line in coo-system, triangle doesnt exist, becouse height=0, also to define area of triangle.So how can i define height by given only A, B and C? Any suggestions for typing my code to look better is always welcome! :)
So, my code looks like this:
import java.text.DecimalFormat;
import java.util.Scanner;
public class MainTriangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double aX;
double aY;
double bX;
double bY;
double cX;
double cY;
System.out.println("-Distance between 3 points in coordinate system-");
System.out.println();
System.out.print("point A(x):");
aX = sc.nextDouble();
System.out.print("point A(y):");
aY = sc.nextDouble();
System.out.print("point B(x):");
bX = sc.nextDouble();
System.out.print("point B(y):");
bY = sc.nextDouble();
System.out.print("point C(x):");
cX = sc.nextDouble();
System.out.print("point C(y):");
cY = sc.nextDouble();
System.out.println();
sc.close();
double dX = bX - aX;
double dY = bY - aY;
double eX = cX - bX;
double eY = cY - bY;
double fX = cX - aX;
double fY = cY - aY;
double sD = Math.pow(dX, 2) + Math.pow(dY, 2);
double d = Math.sqrt(sD); // length of d (from point A to point B)
double sE = Math.pow(eX, 2) + Math.pow(eY, 2);
double e = Math.sqrt(sE); // length of e (from point B to point C)
double sF = Math.pow(fX, 2) + Math.pow(fY, 2);
double f = Math.sqrt(sF); // length of f (from point C to point A)
System.out.println("Distance from point A to point B is "+new DecimalFormat("##.##").format(d));
System.out.println();
System.out.println("Distance from point B to point C is "+new DecimalFormat("##.##").format(e));
System.out.println();
System.out.println("Distance from point C to point A is "+new DecimalFormat("##.##").format(f));
System.out.println();
double p = d+e+f; // (perameter)
System.out.println("Perameter of triangle ABC is "+new DecimalFormat("##.##").format(p));
}
}
double p = d + e + f;
double area = Math.sqrt( p * (p-d) * (p-e) * (p-f) );
double heightD = (2 * area) / d; //height of edge d
according to question; d, e and f are edges of the triangle. you can calculate area by knowing an edge and height of that edge or by knowing 3 edges lenghts. If you know all edges lenghts, you can get height of an edge
You want to find height of the triangle based on the given points, right? Every triangle has three heights, so you can calculate them using Heron's formula.
For any triangle with sides a, b, c and semiperimeter s = (a + b + c) / 2 the altitude from side a is given by: ha = (2*sqrt(s*(s-a)*(s-b)*(s-c)))/a
Note that a,b,c here are the lengths of the sides of the triangle.
Then you can check if any of the heights are zero.
The height of a 'real' triangle depends on which side is on the floor. But you are looking for a non-triangle. i.e. a non-triangle who's three points lie on a straight line.
To work this out you can simply work out the gradient of two of the 'sides'. If they are the same then you know you have a 'non-triangle'
gradient1 = (y2 -y1)/(x2 - x1)
gradient2 = (y3 -y1)/(x3 - x1)
If gradient1 == gradient2 then you have a 'non-triangle'. As double type arithmetic can lead to slight difference you might want to be able to tolerate minor difference in gradient (e.g. if the difference is < 0.0001 then it's a non-triangle)
Update: Probably also need to cater for divide by zero error too!
The height of the triangle is existent between three units.
The A) Base-A
The B) Base-B
The H) Height-C
A^2 + B^2 = C^2

If statement not doing what I want it to

My if statement isn't working when I try to get the direction of a vector in my program.
The problem is when I input a vector of <-1, 1>
I get 45° instead of 125°
Source code
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String[] args)
{
//Decloration
Scanner in = new Scanner(System.in);
int rty;
double v1;
double v2;
double dir;
double mag;
double V1;
double V2;
do {
//Input
System.out.println("Please input x");
v1 = in.nextDouble();
System.out.println("Please input y");
v2 = in.nextDouble();
//Calculating
dir = Math.toDegrees( Math.atan(v2/v1) );
if (v1 >= 0) {
if (v2 < 0) {
dir+=270;
}
} else {
if (v2 < 0) {
dir+=180;
} else {
dir+=90;
}
}
mag = Math.sqrt((v1*v1)+(v2*v2));
V1 = v1/mag;
V2 = v2/mag;
//Output
System.out.println("The unit vector is <" + V1 +", " + V2 + ">");
System.out.println("The magnitude is " + mag);
System.out.println("The direction is " + dir);
System.out.println("\n\nRty?");
//Quit
rty = in.nextInt();
System.out.println("\n");
dir = 0;
} while (rty == 1);
}
}
Does anyone know what's wrong?
Your logic is incorrect for all cases where at least one of v1 and v2 is negative. The atan method Javadocs state that:
the returned angle is in the range -pi/2 through pi/2
So if a value v1 is 0 or positive and v2 is negative, you'll wind up with -45 and you should add 360 degrees so that an input of (1, -1) is 325 degrees.
If a value v1 is negative and v2 is positive, you'll wind up with the same value as the above case, but you want the opposite direction. Add 180 degrees here.
If both v1 and v2 are negative, the degrees value you'll get is 45, so adding 180 here is correct.
Here is the updated logic:
if (v1 >= 0) {
if (v2 < 0) {
dir += 360;
}
} else {
dir += 180;
}
This will get you the correct value of 135 degrees, not 125, for (-1, 1).
However, getting the range of degrees (or originally radians) across the entire range of -pi to +pi is the job of Math.atan2. It takes polar coordinates, which is what v1 and v2 are.
... computing an arc tangent of y/x in the range of -pi to pi.
dir = Math.toDegrees(Math.atan2(v2, v1));
After converting to degrees, you will still want to add 360 degrees if the dir is negative. The logic is simpler:
if (dir < 0) {
dir += 360;
}
From docs, atan(double a)
Returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2.
From docs, atan2(double a)
Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). This method computes the phase theta by computing an arc tangent of y/x in the range of -pi to pi.
The use of atan was scaling your answer down by pi to get it into the range of the values returned by the function. Switching to atan2 would solve that since the cartesian coordinates are converted to polar coordinates. You can obviously then use the toDegrees function.
Math.toDegrees( Math.atan(v2/v1) returns -45 and then you're adding 90, so it will give 45.
For your if clauses this should work:
Math.toDegrees(Math.atan(Math.abs(v2/v1))

Flat, 3D triangle, made out of voxels

I have a problem that I can't seem to get a working algorithm for, I've been trying to days and get so close but yet so far.
I want to draw a triangle defined by 3 points (p0, p1, p2). This triangle can be any shape, size, and orientation. The triangle must also be filled inside.
Here's a few things I've tried and why they've failed:
1
Drawing lines along the triangle from side to side
Failed because the triangle would have holes and would not be flat due to the awkwardness of drawing lines across the angled surface with changing locations
2
Iterate for an area and test if the point falls past the plane parallel to the triangle and 3 other planes projected onto the XY, ZY, and XZ plane that cover the area of the triangle
Failed because for certain triangles (that have very close sides) there would be unpredictable results, e.g. voxels floating around not connected to anything
3
Iterate for an area along the sides of the triangle (line algorithm) and test to see if a point goes past a parallel plane
Failed because drawing a line from p0 to p1 is not the same as a line from p1 to p0 and any attempt to rearrange either doesn't help, or causes more problems. Asymmetry is the problem with this one.
This is all with the intent of making polygons and flat surfaces. 3 has given me the most success and makes accurate triangles, but when I try to connect these together everything falls apart and I get issues with things not connecting, asymmetry, etc. I believe 3 will work with some tweaking but I'm just worn out from trying to make this work for so long and need help.
There's a lot of small details in my algorithms that aren't really relevant so I left them out. For number 3 it might be a problem with my implementation and not the algorithm itself. If you want code I'll try and clean it up enough to be understandable, it will take me a few minutes though. But I'm looking for algorithms that are known to work. I can't seem to find any voxel shape making algorithms anywhere, I've been doing everything from scratch.
EDIT:
Here's the third attempt. It's a mess, but I tried to clean it up.
// Point3i is a class I made, however the Vector3fs you'll see are from lwjgl
public void drawTriangle (Point3i r0, Point3i r1, Point3i r2)
{
// Util is a class I made with some useful stuff inside
// Starting values for iteration
int sx = (int) Util.min(r0.x, r1.x, r2.x);
int sy = (int) Util.min(r0.y, r1.y, r2.y);
int sz = (int) Util.min(r0.z, r1.z, r2.z);
// Ending values for iteration
int ex = (int) Util.max(r0.x, r1.x, r2.x);
int ey = (int) Util.max(r0.y, r1.y, r2.y);
int ez = (int) Util.max(r0.z, r1.z, r2.z);
// Side lengths
float l0 = Util.distance(r0.x, r1.x, r0.y, r1.y, r0.z, r1.z);
float l1 = Util.distance(r2.x, r1.x, r2.y, r1.y, r2.z, r1.z);
float l2 = Util.distance(r0.x, r2.x, r0.y, r2.y, r0.z, r2.z);
// Calculate the normal vector
Vector3f nn = new Vector3f(r1.x - r0.x, r1.y - r0.y, r1.z - r0.z);
Vector3f n = new Vector3f(r2.x - r0.x, r2.y - r0.y, r2.z - r0.z);
Vector3f.cross(nn, n, n);
// Determines which direction we increment for
int iz = n.z >= 0 ? 1 : -1;
int iy = n.y >= 0 ? 1 : -1;
int ix = n.x >= 0 ? 1 : -1;
// Reorganize for the direction of iteration
if (iz < 0) {
int tmp = sz;
sz = ez;
ez = tmp;
}
if (iy < 0) {
int tmp = sy;
sy = ey;
ey = tmp;
}
if (ix < 0) {
int tmp = sx;
sx = ex;
ex = tmp;
}
// We're we want to iterate over the end vars so we change the value
// by their incrementors/decrementors
ex += ix;
ey += iy;
ez += iz;
// Maximum length
float lmax = Util.max(l0, l1, l2);
// This is a class I made which manually iterates over a line, I already
// know that this class is working
GeneratorLine3d g0, g1, g2;
// This is a vector for the longest side
Vector3f v = new Vector3f();
// make the generators
if (lmax == l0) {
v.x = r1.x - r0.x;
v.y = r1.y - r0.y;
v.z = r1.z - r0.z;
g0 = new GeneratorLine3d(r0, r1);
g1 = new GeneratorLine3d(r0, r2);
g2 = new GeneratorLine3d(r2, r1);
}
else if (lmax == l1) {
v.x = r1.x - r2.x;
v.y = r1.y - r2.y;
v.z = r1.z - r2.z;
g0 = new GeneratorLine3d(r2, r1);
g1 = new GeneratorLine3d(r2, r0);
g2 = new GeneratorLine3d(r0, r1);
}
else {
v.x = r2.x - r0.x;
v.y = r2.y - r0.y;
v.z = r2.z - r0.z;
g0 = new GeneratorLine3d(r0, r2);
g1 = new GeneratorLine3d(r0, r1);
g2 = new GeneratorLine3d(r1, r2);
}
// Absolute values for the normal
float anx = Math.abs(n.x);
float any = Math.abs(n.y);
float anz = Math.abs(n.z);
int i, o;
int si, so;
int ii, io;
int ei, eo;
boolean maxx, maxy, maxz,
midy, midz, midx,
minx, miny, minz;
maxx = maxy = maxz =
midy = midz = midx =
minx = miny = minz = false;
// Absolute values for the longest side vector
float rnx = Math.abs(v.x);
float rny = Math.abs(v.y);
float rnz = Math.abs(v.z);
int rmid = Util.max(rnx, rny, rnz);
if (rmid == rnz) midz = true;
else if (rmid == rny) midy = true;
midx = !midz && !midy;
// Determine the inner and outer loop directions
if (midz) {
if (any > anx)
{
maxy = true;
si = sy;
ii = iy;
ei = ey;
}
else {
maxx = true;
si = sx;
ii = ix;
ei = ex;
}
}
else {
if (anz > anx) {
maxz = true;
si = sz;
ii = iz;
ei = ez;
}
else {
maxx = true;
si = sx;
ii = ix;
ei = ex;
}
}
if (!midz && !maxz) {
minz = true;
so = sz;
eo = ez;
}
else if (!midy && !maxy) {
miny = true;
so = sy;
eo = ey;
}
else {
minx = true;
so = sx;
eo = ex;
}
// GeneratorLine3d is iterable
Point3i p1;
for (Point3i p0 : g0) {
// Make sure the two 'mid' coordinate correspond for the area inside the triangle
if (midz)
do p1 = g1.hasNext() ? g1.next() : g2.next();
while (p1.z != p0.z);
else if (midy)
do p1 = g1.hasNext() ? g1.next() : g2.next();
while (p1.y != p0.y);
else
do p1 = g1.hasNext() ? g1.next() : g2.next();
while (p1.x != p0.x);
eo = (minx ? p0.x : miny ? p0.y : p0.z);
so = (minx ? p1.x : miny ? p1.y : p1.z);
io = eo - so >= 0 ? 1 : -1;
for (o = so; o != eo; o += io) {
for (i = si; i != ei; i += ii) {
int x = maxx ? i : midx ? p0.x : o;
int y = maxy ? i : midy ? p0.y : o;
int z = maxz ? i : midz ? p0.z : o;
// isPassing tests to see if a point goes past a plane
// I know it's working, so no code
// voxels is a member that is an arraylist of Point3i
if (isPassing(x, y, z, r0, n.x, n.y, n.z)) {
voxels.add(new Point3i(x, y, z));
break;
}
}
}
}
}
You could use something like Besenham's line algorithm, but extended into three dimensions. The two main ideas we want to take from it are:
rotate the initial line so its slope isn't too steep.
for any given x value, find an integer value that is closest to the ideal y value.
Just as Bresenham's algorithm prevents gaps by performing an initial rotation, we'll avoid holes by performing two initial rotations.
Get the normal vector and point that represent the plane your triangle lies on. Hint: use the cross product of (line from p0 to p1) and (line from p0 to p2) for the vector, and use any of your corner points for the point.
You want the plane to be sufficiently not-steep, to avoid holes. You must satisfy these conditions:
-1 >= norm.x / norm.y >= 1
-1 >= norm.z / norm.y >= 1
Rotate your normal vector and initial points 90 degrees about the x axis and 90 degrees about the z axis until these conditions are satisfied. I'm not sure how to do this in the fewest number of rotations, but I'm fairly sure you can satisfy these conditions for any plane.
Create a function f(x,z) which represents the plane your rotated triangle now lies on. It should return the Y value of any pair of X and Z values.
Project your triangle onto the XZ plane (i.e., set all the y values to 0), and use your favorite 2d triangle drawing algorithm to get a collection of x-and-z coordinates.
For each pixel value from step 4, pass the x and z values into your function f(x,z) from step 3. Round the result to the nearest integer, and store the x, y, and z values as a voxel somewhere.
If you performed any rotations in step 2, perform the opposite of those rotations in reverse order on your voxel collection.
Start with a function that checks for triangle/voxel intersection. Now you can scan a volume and find the voxels that intersect the triangle - these are the ones you're interested in. This is a lousy algorithm but is also a regression test for anything else you try. This test is easy to implement using SAT (separating axis theorem) and considering the triangle a degenerate volume (1 face, 3 edges) and considering the voxels symmetry (only 3 face normals).
I use octtrees, so my preferred method is to test a triangle against a large voxel and figure out which of the 8 child octants it intersects. Then use recursion on the intersected children until the desired level of subdivision is attained. Hint: at most 6 of the children can be intersected by the triangle and often fewer than that. This is tricky but will produce the same results as the first method but much quicker.
Rasterization in 3d is probably fastest, but IMHO is even harder to guarantee no holes in all cases. Again, use the first method for comparison.

Snap point to a line

I have two GPS coordinates which link together to make a line. I also have a GPS point which is near to, but never exactly on, the line. My question is, how do I find the nearest point along the line to the given point?
Game Dev has an answer to this, it is in C++ but it should be easy to port over. Which CarlG has kindly done (hopefully he does not mind me reposting):
public static Point2D nearestPointOnLine(double ax, double ay, double bx, double by, double px, double py,
boolean clampToSegment, Point2D dest) {
// Thanks StackOverflow!
// https://stackoverflow.com/questions/1459368/snap-point-to-a-line-java
if (dest == null) {
dest = new Point2D.Double();
}
double apx = px - ax;
double apy = py - ay;
double abx = bx - ax;
double aby = by - ay;
double ab2 = abx * abx + aby * aby;
double ap_ab = apx * abx + apy * aby;
double t = ap_ab / ab2;
if (clampToSegment) {
if (t < 0) {
t = 0;
} else if (t > 1) {
t = 1;
}
}
dest.setLocation(ax + abx * t, ay + aby * t);
return dest;
}
Try this:
ratio = (((x1-x0)^2+(y1-y0)^2)*((x2-x1)^2 + (y2-y1)^2) - ((x2-x1)(y1-y0) - (x1-x0)(y2-y1))^2)^0.5
-----------------------------------------------------------------------------------------
((x2-x1)^2 + (y2-y1)^2)
xc = x1 + (x2-x1)*ratio;
yc = y1 + (y2-y1)*ratio;
Where:
x1,y1 = point#1 on the line
x2,y2 = point#2 on the line
x0,y0 = Another point near the line
xc,yx = The nearest point of x0,y0 on the line
ratio = is the ratio of distance of x1,y1 to xc,yc and distance of x1,y1 to x2,y2
^2 = square
^0.5 = square root
The formular is derived after we find the distant from point x0,y0 to line (x1,y1 -> x2,y3).
See here
I've test this code here (this particular one I gave you above) but I've used it similar method years ago and it work so you may try.
You can use JTS for that.
Create a LineSegment (your line)
Create a Coordinate (the point you want to snap to the line)
Get Point on the line by using the closestPoint method
Very simple code example:
// create Line: P1(0,0) - P2(0,10)
LineSegment ls = new LineSegment(0, 0, 0, 10);
// create Point: P3(5,5)
Coordinate c = new Coordinate(5, 5);
// create snapped Point: P4(0,5)
Coordinate snappedPoint = ls.closestPoint(c);

Categories