better way to do this?
publicboolean withinDistance(int absX,int absY,int pointX,int pointY){
int x = 0, y = 0;
if (pointX > absX) x = pointX - absX;
if (pointX < absX) x = absX - pointX;
if (pointY > absY) y = pointY - absY;
if (pointY < absY) y = absY - pointY;
if ( (x == 1 || y == 1 || (x + y == 1))
returntrue;
returnfalse;
}
its a method that i wrote to find out if something is within 1 space of another object on a grid. it works partly, but not all the time. is ther any better way to accomplish this?
Message was edited by:
ByronTheOmnipotent
Well, if it doesn't always work, then obviously there must be a better way.
You need to think through your logic a little more.
In what cases does it work/not work? Find the pattern.
jverda at 2007-7-29 14:48:54 >

Your equation doesnt really determine the distance between two points in two dimentional space. Try this: (Note the use of javadoc!!!)
/**
* Return true if distance between two points is less than or equal to
* argument 'maxDistance"
* @param x1,y1= the location of the first point
* @param x2,y2= the location of the second point
* @param maxDistance= the max distance the points can be apart
*/
public boolean withinDistance(int x1 int y1,
int x2, int y2,
int maxDistance) {
int distanceBetweenPoints=
squareRoot((x1 -x2)^2 +(y1-y2)^2 );
return (maxDistance - distanceBetweenPoints)>=0;
}
Java has a Math package for square roots and taking the square of a number. Use it for squareRoot, and ^2 above. Example: Math.squroot().
Look up 'java Math API' for the functions.
Lastly, in the future, please post specific questions in the title rather than 'can you help me' so a person knowledgeable in the area of your problem will see your post. Instead post something link 'distance between two points problem'.
> Your equation doesnt really determine the distance
> between two points in two dimentional space.
I suspect he doesn't actually want literally "within one unit." He seems to want "no more than one x unit and one y unit away."
jverda at 2007-7-29 14:48:54 >

If thats so, he needs to specify that in his javadoc.
You don't need squareroots or anything that fancy if you are looking to see if two points are within one grid distance of antoher. all you need:
{
if ((pointX - absX >= -1) &&
(pointX - absX <= 1) &&
(pointY - absY >= -1) &&
(pointY - absY <= 1))
{
return true;
}
else
{
return false;
}
}
Mightn't it have been better to give him hints that he could use to work through it himself, rather than just giving him full code?
Not that that code will work.
jverda at 2007-7-29 14:48:54 >

> Mightn't it have been better to give him hints that
> he could use to work through it himself, rather than
> just giving him full code?
true. I guess my rationale was that he was given faulty info previously.
by the way, how will my method fail (besides not having the method signature at the top)?
Message was edited by:
petes1234
> Mightn't it have been better to give him hints that
> he could use to work through it himself, rather than
> just giving him full code?
>
> Not that that code will work.
((pointX - absX >= -1) && (pointX - absX <= 1)
Can you tell me under what conditions a given quantity will be both <= 1 and >= 1?
jverda at 2007-7-29 14:48:54 >

> ((pointX - absX >= -1) && (pointX - absX <=
> 1)
> Can you tell me under what conditions a given
> quantity will be both <= 1 and >= 1?
Might want to take a second look at that one ... ;-) (<=1 && >=-1)
> ((pointX - absX >= -1) && (pointX - absX <=
> 1)
> Can you tell me under what conditions a given
> quantity will be both <= 1 and >= 1?
Look closely. You copied it wrong. It will tell you 9 conditions:
locations relative to point1, point 2 at:
-1, -1
-1, 0
-1, 1
0, -1
0, 0
0, 1
1, -1
1, 0
1, 1
to fully spell it out jverd, run this code:
import java.awt.Point;
public class Fubar
{
private Point myPoint = new Point(5, -5);
private boolean withinDist(Point p1, Point p2)
{
if ((p1.x - p2.x >= -1) &&
(p1.x - p2.x <= 1) &&
(p1.y - p2.y >= -1)&&
(p1.y - p2.y <= 1))
{
return true;
}
else
{
return false;
}
}
public void checkPoints()
{
for (int i = -10; i < 10; i++)
{
for (int j = -10; j < 10; j++)
{
Point p = new Point(i, j);
if (withinDist(p, myPoint))
{
System.out.println(p);
}
}
}
}
public static void main(String[] args)
{
Fubar f = new Fubar();
f.checkPoints();
}
}
> to fully spell it out jverd, run this code:
I suspect jverd is fully capable of understanding the code without running it...
but is he fully embarrassed?
> but is he fully embarrassed?
If the YouTube video with the goat didn't do it I doubt this will.
> but is he fully embarrassed?
I think I'm starting to feel a little coder-envy growing here ...
> > ((pointX - absX >= -1) && (pointX - absX <=
> > 1)
> > Can you tell me under what conditions a given
> > quantity will be both <= 1 and >= 1?
>
> Might want to take a second look at that one ... ;-)
> (<=1 && >=-1)
Ah, yes. My bad. I wish I could blame the beer, but I haven't even had any. :-(
Stared at the **** thing over and over...
jverda at 2007-7-29 14:48:59 >

> but is he fully embarrassed?
No. Why would I be?
jverda at 2007-7-29 14:48:59 >

> No. Why would I be?
You're right. Nothing here, but the goat, now that may be another matter. I 'bout fell out of my chair with that one.
> > No. Why would I be?
>
> You're right. Nothing here, but the goat, now that
> may be another matter. I 'bout fell out of my chair
> with that one.
I already told you: I thought the goat was a prostitute. Sheesh.
jverda at 2007-7-29 14:48:59 >

> I wish I could blame the beer, but I haven't even had any
Blame the lack of beer then!
> > I wish I could blame the beer, but I haven't even
> had any
>
> Blame the lack of beer then!
Of course! That must be it.
jverda at 2007-7-29 14:48:59 >

reminds me of a terrible joke
http://www.jokesgallery.com/joke.php?joke=469&id=0