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

[1202 byte] By [ByronTheOmnipotenta] at [2007-11-27 11:21:28]
# 1

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 > top of Java-index,Java Essentials,New To Java...
# 2

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'.

George123a at 2007-7-29 14:48:54 > top of Java-index,Java Essentials,New To Java...
# 3

> 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 > top of Java-index,Java Essentials,New To Java...
# 4

If thats so, he needs to specify that in his javadoc.

George123a at 2007-7-29 14:48:54 > top of Java-index,Java Essentials,New To Java...
# 5

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;

}

}

petes1234a at 2007-7-29 14:48:54 > top of Java-index,Java Essentials,New To Java...
# 6

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 > top of Java-index,Java Essentials,New To Java...
# 7

> 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

petes1234a at 2007-7-29 14:48:54 > top of Java-index,Java Essentials,New To Java...
# 8

> 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 > top of Java-index,Java Essentials,New To Java...
# 9

> ((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)

Navy_Codera at 2007-7-29 14:48:54 > top of Java-index,Java Essentials,New To Java...
# 10

> ((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

petes1234a at 2007-7-29 14:48:54 > top of Java-index,Java Essentials,New To Java...
# 11

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();

}

}

petes1234a at 2007-7-29 14:48:54 > top of Java-index,Java Essentials,New To Java...
# 12

> to fully spell it out jverd, run this code:

I suspect jverd is fully capable of understanding the code without running it...

Navy_Codera at 2007-7-29 14:48:54 > top of Java-index,Java Essentials,New To Java...
# 13

but is he fully embarrassed?

petes1234a at 2007-7-29 14:48:54 > top of Java-index,Java Essentials,New To Java...
# 14

> but is he fully embarrassed?

If the YouTube video with the goat didn't do it I doubt this will.

cotton.ma at 2007-7-29 14:48:54 > top of Java-index,Java Essentials,New To Java...
# 15

> but is he fully embarrassed?

I think I'm starting to feel a little coder-envy growing here ...

Navy_Codera at 2007-7-29 14:48:59 > top of Java-index,Java Essentials,New To Java...
# 16

> > ((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 > top of Java-index,Java Essentials,New To Java...
# 17

> but is he fully embarrassed?

No. Why would I be?

jverda at 2007-7-29 14:48:59 > top of Java-index,Java Essentials,New To Java...
# 18

> 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.

petes1234a at 2007-7-29 14:48:59 > top of Java-index,Java Essentials,New To Java...
# 19

> > 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 > top of Java-index,Java Essentials,New To Java...
# 20

> I wish I could blame the beer, but I haven't even had any

Blame the lack of beer then!

floundera at 2007-7-29 14:48:59 > top of Java-index,Java Essentials,New To Java...
# 21

> > 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 > top of Java-index,Java Essentials,New To Java...
# 22

reminds me of a terrible joke

http://www.jokesgallery.com/joke.php?joke=469&id=0

petes1234a at 2007-7-29 14:48:59 > top of Java-index,Java Essentials,New To Java...