如何在python中获取对角线上的鼠标坐标?
How to get mouse coordinates in a diagonal line in python?
我一直在努力让这个数学正确。
我有这张图片来演示
整个矩形就是一个瓦片,我有鼠标在这个瓦片内的位置。但是我想知道鼠标什么时候经过红色、黄色、绿色或蓝色区域。
我知道如果我这样做if mouse_x < tile_width/2 and mouse_y < tile_height/2
我可以获得粉红色线条内部的坐标,但我只想知道鼠标是否在红色区域。
令h
和w
分别为粉色矩形的高度和宽度。
红色区域由不等式定义:
mouse_y < (h / w) * (w - mouse_x)
蓝色区域定义为:
mouse_y > (h / w) * mouse_x + h
黄色区域:
mouse_y < (h / w) * mouse_x - h
你能认出绿色的那个吗?
与其说是编程问题,不如说是数学问题...GeoGebra 可以提供帮助。
四行是f、g、h和i。红色区域在f以上,所以公式为
-3x + 4y + 12 > 0
现在,x和y是鼠标坐标,3是tile_height/2
,4是tile_width/2
,12是tile_width*tile_height/4
。因此公式应该是
-tile_height/2*mouse_x + tile_width/2*mouse_y + tile_width*tile_height/4 > 0
这样你可以继续所有4行并检查鼠标是在红色、黄色、蓝色还是绿色上。
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 .")
我一直在努力让这个数学正确。 我有这张图片来演示
整个矩形就是一个瓦片,我有鼠标在这个瓦片内的位置。但是我想知道鼠标什么时候经过红色、黄色、绿色或蓝色区域。
我知道如果我这样做if mouse_x < tile_width/2 and mouse_y < tile_height/2
我可以获得粉红色线条内部的坐标,但我只想知道鼠标是否在红色区域。
令h
和w
分别为粉色矩形的高度和宽度。
红色区域由不等式定义:
mouse_y < (h / w) * (w - mouse_x)
蓝色区域定义为:
mouse_y > (h / w) * mouse_x + h
黄色区域:
mouse_y < (h / w) * mouse_x - h
你能认出绿色的那个吗?
与其说是编程问题,不如说是数学问题...GeoGebra 可以提供帮助。
四行是f、g、h和i。红色区域在f以上,所以公式为
-3x + 4y + 12 > 0
现在,x和y是鼠标坐标,3是tile_height/2
,4是tile_width/2
,12是tile_width*tile_height/4
。因此公式应该是
-tile_height/2*mouse_x + tile_width/2*mouse_y + tile_width*tile_height/4 > 0
这样你可以继续所有4行并检查鼠标是在红色、黄色、蓝色还是绿色上。
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 .")