DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108

    Angry 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());
    			if(temporary.getDistance(p.addPoints(mOffset))<north)
    			{
    				north = temporary.getDistance(p.addPoints(mOffset));
    			}
    		}
    		//Check distance from the south edge
    		for(long x=(this.width/2*-1); x<(this.width/2);x++)
    		{
    			temporary.setPoint((long)x,bLeft.getY());
    			if(temporary.getDistance(p.addPoints(mOffset))<south)
    			{
    				south = temporary.getDistance(p.addPoints(mOffset));
    			}
    		}
    		//Check distance from the west edge
    		for(long x=(this.height/2*-1); x<(this.height/2);x++)
    		{
    			temporary.setPoint(tLeft.getX(),x);
    			if(temporary.getDistance(p.addPoints(mOffset))<west)
    			{
    				west = temporary.getDistance(p.addPoints(mOffset));
    			}
    		}
    		//Check distance from the east edge
    		for(long x=(this.height/2*-1); x<(this.height/2);x++)
    		{
    			temporary.setPoint(tRight.getX(),x);
    			if(temporary.getDistance(p.addPoints(mOffset))<east)
    			{
    				east = temporary.getDistance(p.addPoints(mOffset));
    			}
    		}
    		
    		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;
    		}
    	}
    }
    Thanks in advance

  2. #2
    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){
      //quadrant 1
      xdiff = tRight.getX - check.getX;
      ydiff = tRight.getY - check.getY;
      return (xdiff < ydiff ? xdiff : ydiff)
    }else if(check.getX < 0 && check.getY > 0){
      //quadrant 2
      xdiff = (tLeft.getX - check.getX) * -1; //get absolute value
      ydiff = tRight.getY - check.getY;
      return (xdiff < ydiff ? xdiff : ydiff)
    }else if(...){
      //quadrant 3
    }else if(...){
      //quadrant 4
    }else{
     //handle cases where it's on x-axis or y-axis or both (0,0)
    }

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

Similar Threads

  1. Replies: 9
    Last Post: 09-20-2005, 01:16 PM
  2. Replies: 0
    Last Post: 08-08-2005, 09:46 AM
  3. Replies: 1
    Last Post: 09-01-2002, 08:52 AM
  4. Sample Sites.
    By Murray Foxcroft in forum Web
    Replies: 5
    Last Post: 11-02-2000, 02:42 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links