Home > Mobile >  How to get mouse coordinates in a diagonal line in python?
How to get mouse coordinates in a diagonal line in python?

Time:01-30

I have been struggling a bit to get this maths right. I have this image to demonstrate

The whole rectangle is a tile, and I have the position of the mouse inside of this tile. But I want to know when the mouse goes over the red, yellow, green or blue areas.

I know if I do if mouse_x < tile_width/2 and mouse_y < tile_height/2 I can get the coordinates of inside of the pink lines, but I want to know only if the mouse is in the red area.

CodePudding user response:

Let h and w be the height and width of the pink rectangle respectively.

The red area is defined by the inequation:

mouse_y < (h / w) * (w - mouse_x)

The blue area is defined by:

mouse_y > (h / w) * mouse_x   h

The yellow area:

mouse_y < (h / w) * mouse_x - h

Can you figure out the green one?

CodePudding user response:

That's more a math question than programming question... Geogebra

The four lines are f, g, h and i. The red area is above f, so the formula is

-3x   4y   12 > 0

Now, x and y are the mouse coordinates, 3 is tile_height/2 and 4 is tile_width/2, 12 is tile_width*tile_height/4. Thus the formula should be

-tile_height/2*mouse_x   tile_width/2*mouse_y   tile_width*tile_height/4 > 0

Like that you can continue for all 4 lines and check whether the mouse is on red, yellow, blue or green.

  •  Tags:  
  • Related