Question:
How would I implement Pythagoras' theorum in Java to calculate distance between the user's mouse click and...
hippyJu
2007-03-07 03:13:49 UTC
various objects within the associated applet, to display the name of the object that the click is nearest to, for example?

I think all the objects' x and y coordinates have to be stored in arrays, to be calculated against with the coordinates of the x,y of the mouse clicks?
Three answers:
robcraine
2007-03-07 03:40:39 UTC
I'm no java expert, so I can't help you with the details, but that seems about right. You'll certainly need a list of all though objects positions, so computing root[(x1-x2)^2 + (y1-y2)^2] and picking the minimum shouldn't be too much extra effort.



One possible alternative would be to map out the screen beforehand, and say that if the mouse click is in *this* region then *this* is the closest object. That could get a bit messy of you've got lots of randomly distributed objects, but if you have only a few, or they are in some sort of regular grid, it might be an option.



If the objects are in a regular grid, then it would be easy enough to work out which object is closest without calculating the relevent distances.



One final thing to consider is whether your objects are points, circles or some other shape - in the first two cases you're fine, just say the object is at the point, or the centre of the circle. Otherwise things might get complicated - consider a long thin rectangle. Then clicking at the end would be a long way from the 'centre' of the rectangle, but is clearly close to the body of the rectangle.
FedUp
2007-03-07 03:32:27 UTC
calculate the square of the distance between the point and each of the objects.

(x-a)^2 + (y-b)^2 where (x,y) is the coordinate of the point and (a,b) for the object. whichever has the minimum value for this expression is the nearest object.
?
2016-12-05 13:54:10 UTC
5/9 of the radius of the Earth. that is assuming that factors are chosen randomly in accordance to the following set of guidelines: We take a dice which precisely encloses the Earth and choose factors uniformly from interior that dice. If the point chosen is outside the Earth, it truly is rejected and this procedure is repeated until eventually a level interior or on the earth is selected. that is the first aspect. This technique is repeated interior an identical thanks to pick the 2d aspect.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...