Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Grid Detection

Author
Message
D a r k
20
Years of Service
User Offline
Joined: 21st Jun 2004
Location: In ur fridge, eatin ur foodz.
Posted: 21st Aug 2007 08:32
Alright, I'm working on this 2D Grid. I have no problem drawing the grid. My question is, How can I go about detecting when the mouse is placed inside a certain grid square?

My grid is 30 x 25, Each grid space is approx. 16 pixels in width and height. So If the user has the mouse inside grid space (0,0) (Top left) how can I detect this?

Thanks in advance.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 21st Aug 2007 12:45
use a for loop and multiply the area by 16

Your signature has been erased by a mod because it was rubbish.
Comrade Robski
19
Years of Service
User Offline
Joined: 12th Sep 2005
Location:
Posted: 21st Aug 2007 15:19 Edited at: 21st Aug 2007 15:19
Try this in your loop. gridx and gridy give the 2d grid coordinates of the mouse



Because gridx and gridy are integer variables, they are always rounded down. So if mx is from 0 to 15, the gridx = 0. If mx is from 16 to 31, then gridx = 1 and so on....
Pixelator
17
Years of Service
User Offline
Joined: 8th Jul 2007
Location: here
Posted: 21st Aug 2007 17:55 Edited at: 21st Aug 2007 17:57
Use an array that stores the x position of each point on the grid and another that stores the y position of each point on the grid. Then check to see if mousex() and mousey() = values stored in the arrays.

(Y=M*X+B)=(X=(Y-B)/M)=(B=Y-M*X)=(M=Y-B/X)
and if you can figure out what it means i will give you a cookie.
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 21st Aug 2007 19:13
If the mouse is inside the grid:

1. Take the mouse X position* and divide it by the X cell size (16). Store it in GX.

2. Take the mouse Y position* and divide it by the Y cell size (16). Store it in GY.

Multiply GY by the number of cells across (30) and add GX.

This will give you the cell number.

* If the grid isn't at 0,0 on the screen, you have to subtract the offset values from the mouse positions before dividing.

TDK_Man

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 21st Aug 2007 19:23
Quote: "use a for loop and multiply the area by 16 "

oops I meant divide
CR and TDK are on the right path.
Ignore Pixelator

Your signature has been erased by a mod because it was rubbish.
Pixelator
17
Years of Service
User Offline
Joined: 8th Jul 2007
Location: here
Posted: 21st Aug 2007 20:55

what did i do????????????????


(Y=M*X+B)=(X=(Y-B)/M)=(B=Y-M*X)=(M=Y-B/X)
and if you can figure out what it means i will give you a cookie.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 21st Aug 2007 22:03
@pixelator
lol, sorry. Your way was just very wasteful

Your signature has been erased by a mod because it was rubbish.
Pixelator
17
Years of Service
User Offline
Joined: 8th Jul 2007
Location: here
Posted: 21st Aug 2007 22:58 Edited at: 21st Aug 2007 22:59
true

[i just tried to make an example and i accidentilly pasted another probram into it and so i hit undo and the code was so larke DBC closed out

A^2+b^2=c^2, but D^2-A^2/g^2*t-r-o-u-b-l-e = w+i+t+h / a*(c*a/p^i+t+o+l)/T+rhyming+with/P+f/o*r^(p-o-o/l)

That equation has a quote from a movie in it. Find it for a cookie

Login to post a reply

Server time is: 2025-05-30 14:03:24
Your offset time is: 2025-05-30 14:03:24