How to get mouse coordinates in a diagonal line in python?

Viewed 171

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.

3 Answers

That's more a math question than programming question... GeoGebra can help.

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.

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?

i = input("Enter q to quit .")
print("Enter your numbers in the form of floating point numbers .\n")
while i!= 'q':
    import math
    try:
        x = float(input("\nEnter x coordinate of mouse: "))+(0.1)
        y = float(input("Enter y coordinate of mouse : "))+(0.1)
        x1 = float(input("Enter x coordinate of vertex of the rectangle closer to the origin \n(say x1) : "))+(0.1)
        y1 = float(input("Enter y coordinate of vertex of the rectangle closer to the origin \n(say  y1) : "))+(0.1)
        a1 = float(input("What is the length(width) of the rectangle ? "))+(0.1)
        x2 = (x1+a1)
        a2 = float(input("What is the height of the rectangle ? "))+(0.1)
        y2 = (y1+a2)
    except:
        print("Input correct values to proceed .")
        continue
    
    def f1():
        return x+y
    def f2():
        return (y2-y1)/2
    def f3():
        i1 = math.pow((x-x1), 2)
        i2 = math.pow((y-y1), 2)
        i3 = math.sqrt(i1+i2)
        return i3
    if f1() == (y1+y2)/2 and x>=x1 and x<=(x1+x2)/2:
        print("Mouse is on the hyportnes of the blue area .")
    elif x>=x1 and x<=(x1+x2)/2 and y == y1:
        print("Mouse on the x axis of the blue area .")
    elif y>=y1 and y<=(y1+y2)/2 and x == x1:
        print("Mouse on the y axis of the blue area .")
    elif f3()<f2() and f1()<(y1+y2)/2 and x>=x1 and x<=(x1+x2)/2 and y>=y1 and y<=(y1+y2)/2:
        print("Mouse is in the blue area .")
    
    def f4():
        return x-((x1+x2)/2)
    def f5():
        i1 = math.pow((x-x2), 2)
        i2 = math.pow((y-y1), 2)
        i3 = math.sqrt(i1+i2)
        return i3
    if f4() == (y-y1) and x>=(x1+x2)/2 and x<=x2:
        print("Mouse is on the hyportnes of the green area .")
    elif x>=(x1+x2)/2 and x<=x2 and y == y1:
        print("Mouse on the x axis of the green area .")
    elif y>=y1 and y<=(y1+y2)/2 and x == x1:
        print("Mouse on the y axis of the green area .")
    elif f5()<f2() and f4()<(y-y1) and x>=(x1+x2)/2 and x<=x2 and y>=y1 and y<=(y1+y2)/2:
        print("Mouse is in the green area .")
    
    def f6():
        return -(x-((x1+x2)/2))
    def f7():
        i1 = math.pow((x-x2), 2)
        i2 = math.pow((y-y2), 2)
        i3 = math.sqrt(i1+i2)
        return i3
    if f6() == (y-y2) and x>=(x1+x2)/2 and x<=x2:
        print("Mouse is on the hyportnes of the yellow area .")
    elif x>=(x1+x2)/2 and x<=x2 and y == y2:
        print("Mouse on the x axis of the yellow area .")
    elif y>=(y1+y2)/2 and y<=y2 and x == x2:
        print("Mouse on the y axis of the yellow area .")
    elif f7()<f2() and f6()>(y-y2) and x>=(x1+x2)/2 and x<=x2 and y>=(y1+y2)/2 and y<=y2:
        print("Mouse is in the yellow area .")
    
    def f8():
        return y-((y1+y2)/2)
    def f9():
        i1 = math.pow((x-x1), 2)
        i2 = math.pow((y-y2), 2)
        i3 = math.sqrt(i1+i2)
        return i3
    if f8() == (x-x1) and x>=x1 and x<=(x1+x2)/2:
        print("Mouse is on the hyportnes of the red area .")
    elif x>=x1 and x<=(x1+x2)/2 and y == y2:
        print("Mouse on the x axis of the red area .")
    elif y>=y1 and y<=y2 and x == x1:
        print("Mouse on the y axis of the red area .")
    elif f9()<f2() and f8()>(x-x1) and x<=(x1+x2)/2 and x>=x1 and y>=(y1+y2)/2 and y<=y2:
        print("Mouse is in the red area .")

    def whiteArea():
        if x>=x1 and x<=x2 and y>=y1 and y<=y2:
            print("If the above line of statement is True then don't consider the below line !")
            print("Mouse is in the white area i.e inbetween the blue, green, yellow and red area .")
    whiteArea()
    i = input("Enter q to quit .")
    if i == 'q':
        break
    
print("Thanks for using this method .")
Related