Home arrow static arrow Java Programming [Archive] - converting to whole number
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - converting to whole number
This topic has 13 replies on 1 page.

Posts:121
Registered: 12/16/03
converting to whole number  
Jun 25, 2004 12:09 PM



 
Hi

I am making a map editior for my game

I have a mouse event adapter to detect mouse clicks I can get the X,Y coordinates of the click positions. I have a multidimensional integer array which is 30x24 in size and contains which square on the map is what such as walls, floors, doors, etc

What i want to do is to detect where the mouse click is on the map (painted on the screen) and then change the number in the multidimensional array.

i tried one way using loops and IF statements but it didnt work to well so this is what i want to do...

---

I got the mouse click coordinates

I want to divide these coordinates into 16

and then take the decimal point and converting it back to Integer.

However i would like the number not to be rounded up or down.

---

I hope you understand what i mean
Thanks in advance
 

Posts:2,206
Registered: 8/15/02
Re: converting to whole number  
Jun 25, 2004 12:13 PM (reply 1 of 13)



 
I am not sure I understand. Any division can result in an irrational number. Consider decimal division. 10/3 is an irratinal number and can not be converted to an integer without some loss of precision.
 

Posts:121
Registered: 12/16/03
Re: converting to whole number  
Jun 25, 2004 12:21 PM (reply 2 of 13)



 
I dont mind about that

this is what i want to happen

X = 31

31 / 16 = 1.9

X = 1

X = X + 1

that will get the position of my mouse on the grid

+-+-+-+-+-+-+-+
       

+-+-+-+-+-+-+-+
| | * | | | | | | Star shows where the mouse is on the grid
+-+-+-+-+-+-+-+
| | | | | | | |
+-+-+-+-+-+-+-+

Understand now?
 

Posts:31,095
Registered: 4/30/99
Re: converting to whole number  
Jun 25, 2004 12:22 PM (reply 3 of 13)



 
Nope, don't understand what you mean.

However i would like the number not to be rounded up or down.

Okay. So suppose the result of the division is 11.375. You don't want that rounded up (to 12) and you don't want it rounded down (to 11) but you want an integer. You can't have all those wants.

PC²
 

Posts:2,206
Registered: 8/15/02
Re: converting to whole number  
Jun 25, 2004 12:23 PM (reply 4 of 13)



 
I dont mind about that

this is what i want to happen

X = 31

31 / 16 = 1.9

X = 1

X = X + 1

that will get the position of my mouse on the grid

+-+-+-+-+-+-+-+
       

+-+-+-+-+-+-+-+
| | * | | | | | | Star shows
where the mouse is on the grid
+-+-+-+-+-+-+-+
| | | | | | | |
+-+-+-+-+-+-+-+

Understand now?

Nope

 

Posts:121
Registered: 12/16/03
Re: converting to whole number  
Jun 25, 2004 12:24 PM (reply 5 of 13)



 
I want it rounded down

so i just want the whole number to appear

for example

11.8 becomes 11

2.3 becoms 2
 

Posts:121
Registered: 12/16/03
Re: converting to whole number  
Jun 25, 2004 12:26 PM (reply 6 of 13)



 
Ok, in a nutshell

i want a number that i got from a mouse coordinate such as m.getX()

and then convert it to an integer. but rounding it down
 

Posts:2,206
Registered: 8/15/02
Re: converting to whole number  
Jun 25, 2004 12:27 PM (reply 7 of 13)



 
so the following is ok
double x = 10.0/3.0;int i = (int) x;
 

Posts:121
Registered: 12/16/03
Re: converting to whole number  
Jun 25, 2004 12:29 PM (reply 8 of 13)



 
ok, that will do. thanks
 

Posts:147
Registered: 26.01.04
Re: converting to whole number  
Jun 25, 2004 12:32 PM (reply 9 of 13)



 
thats precisely what int does with floating point numbers.

float f1 = 1.8f;float f2 = 2.2f;System.out.println((int)f1);		//output: 1System.out.println((int)f2);		//output: 2System.out.println( ((int)f1 + (int)f2) ); 		//output: 3System.out.println( (f1 + f2) );		//output: 4
 

Posts:2,206
Registered: 8/15/02
Re: converting to whole number  
Jun 25, 2004 12:33 PM (reply 10 of 13)



 
Still confused what the OP meant by However i would like the number not to be rounded up or down.
 

Posts:121
Registered: 12/16/03
Re: converting to whole number  
Jun 25, 2004 12:37 PM (reply 11 of 13)



 
EvilEdna solution worked

it does exacly what i wanted and my program works perfect with it!

thanks.

Im sorry that i didnt explain it too well.
 

Posts:147
Registered: 26.01.04
Re: converting to whole number  
Jun 25, 2004 1:00 PM (reply 12 of 13)



 
unless that "snapping" to the grid is wanted, you could use the drawing abilities of Graphics2D and Point2D.Double...
 

Posts:121
Registered: 12/16/03
Re: converting to whole number  
Jun 25, 2004 1:08 PM (reply 13 of 13)



 
i didnt want snap to grid, the grid is in a array, you cant see it :)

this topic is closed
 
This topic has 13 replies on 1 page.