DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Need serious help, Finding the distance from a point to a line

1. Registered User
Join Date
Oct 2004
Location
Chicago
Posts
108

## Need serious help, Finding the distance from a point to a line

Does anyone know how I could figure out the distance from a point inside a rectangle to the closest edge of the rectangle.

This is for a program in which you have virtual boats moving on a virtual lake. However I am having some major problems with stopping the boats from running off of the edge.

I have written some code here, but i don't think it works the right way.

Does anyone have any suggestions?

Code:
```public class Rectangle
{
private long width;
private long height;
private Point tLeft, tRight, bLeft, bRight;

public Rectangle(long width, long height)
{
this.width 	= width;
this.height = height;
this.tLeft 	= new Point((width/2)*-1,(height/2));
this.tRight = new Point((width/2), (height/2));
this.bLeft 	= new Point(((width/2)*-1),((height/2)*-1));
this.bRight = new Point((width/2),((height/2)*-1));
}

public long isNearEdge(Point p, Point mOffset)
{
long north 	= this.height;
long south 	= this.height;
long east 	= this.width;
long west 	= this.width;

Point temporary = new Point();

//Check distance form the north edge
for(long x=(this.width/2*-1); x<(this.width/2);x++)
{
temporary.setPoint((long)x,tLeft.getY());
{
}
}
//Check distance from the south edge
for(long x=(this.width/2*-1); x<(this.width/2);x++)
{
temporary.setPoint((long)x,bLeft.getY());
{
}
}
//Check distance from the west edge
for(long x=(this.height/2*-1); x<(this.height/2);x++)
{
temporary.setPoint(tLeft.getX(),x);
{
}
}
//Check distance from the east edge
for(long x=(this.height/2*-1); x<(this.height/2);x++)
{
temporary.setPoint(tRight.getX(),x);
{
}
}

long temp1 = 0;
long temp2 = 0;

if(north>south)
{
temp1 = north;
}
else
{
temp1 = south;
}

if(west>east)
{
temp2 = west;
}
else
{
temp2 = east;
}

if(temp1>temp2)
{
return temp1;
}
else
{
return temp2;
}
}
}```

2. Registered User
Join Date
Oct 2005
Posts
107
For loops are unecessary.
Code:
```Point check = p.addPoint(mOffset);
// to check if inside rectangle
if(check.getX() > tRight.getX() || check.getX < tLeft.getX() || check.getY() > tRight.getY() || check.getY() < bRight.getY()){
//check is outside rectangle assuming rectangle is level
}else{
//check is inside
}```
Now if you wanted to return the distance to the closest edge it would get trickier. But you would not need to check the distance from every point on the line. You could put this code where you know the check is inside the rectangle. I'd break it up into quadrants and using the properties of a rectangle check which edge it's closest too.
Code:
```//since you have the origin (0,0) at the center
if(check.getX > 0 && check.getY > 0){
xdiff = tRight.getX - check.getX;
ydiff = tRight.getY - check.getY;
return (xdiff < ydiff ? xdiff : ydiff)
}else if(check.getX < 0 && check.getY > 0){
xdiff = (tLeft.getX - check.getX) * -1; //get absolute value
ydiff = tRight.getY - check.getY;
return (xdiff < ydiff ? xdiff : ydiff)
}else if(...){
}else if(...){
}else{
//handle cases where it's on x-axis or y-axis or both (0,0)
}```

3. Registered User
Join Date
Oct 2004
Location
Chicago
Posts
108
Thanks alot!!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts

 FAQ Latest Articles Java .NET XML Database Enterprise