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?
Thanks in advanceCode: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; } } }


Reply With Quote



Bookmarks