RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1600283
Accepted
Ivan Polevikov
Ivan Polevikov
Asked:2024-11-19 23:50:12 +0000 UTC2024-11-19 23:50:12 +0000 UTC 2024-11-19 23:50:12 +0000 UTC

如何在只知道 x 和 y 坐标的情况下将屏幕分成 4 个三角形区域?

  • 772

我的想法是只知道鼠标的x和y坐标来检查光标位于屏幕的哪个区域,你需要以这种形式划分它:屏幕

代码形式的完整实现并不是必需的,但至少需要理论形式的解决方案。

python
  • 3 3 个回答
  • 58 Views

3 个回答

  • Voted
  1. Best Answer
    Amgarak
    2024-11-20T02:15:57Z2024-11-20T02:15:57Z

    这是一个有趣的问题,所以我决定为您做一个小例子。

    代码:

    from collections import namedtuple
    import ctypes
    import win32con
    import win32api
    import win32gui
    import atexit
    # pip install pywin32
    
    MouseEvent = namedtuple('MouseEvent', ['event_type', 'x', 'y'])
    handlers = []
    
    def listen():
        def low_level_handler(nCode, wParam, lParam):
            point = ctypes.cast(lParam, ctypes.POINTER(ctypes.c_long * 2)).contents
            x = point[0]
            y = point[1]
            event = MouseEvent(wParam, x, y)
    
            for handler in handlers:
                handler(event)
            
            return ctypes.windll.user32.CallNextHookEx(hook_id, nCode, wParam, lParam)
    
        CMPFUNC = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_void_p))
        ctypes.windll.user32.SetWindowsHookExW.argtypes = (
            ctypes.c_int,
            CMPFUNC,
            ctypes.c_void_p,
            ctypes.c_uint
        )
        pointer = CMPFUNC(low_level_handler)
    
        hook_id = ctypes.windll.user32.SetWindowsHookExW(win32con.WH_MOUSE_LL, pointer,
                                                         win32api.GetModuleHandle(None), 0)
    
        atexit.register(ctypes.windll.user32.UnhookWindowsHookEx, hook_id)
        win32gui.GetMessage(None, 0, 0)
        
    
    def get_screen_resolution():
        user32 = ctypes.windll.user32
        user32.SetProcessDPIAware()  
        screen_width = user32.GetSystemMetrics(0)
        screen_height = user32.GetSystemMetrics(1)
        return screen_width, screen_height
    
    
    def get_triangles(screen_width, screen_height):
        top_left = (0, 0)
        top_right = (screen_width, 0)
        bottom_right = (screen_width, screen_height)
        bottom_left = (0, screen_height)
        
        center = (screen_width // 2, screen_height // 2)
        
        triangles = [
            ("Triangle 1", bottom_left, center, top_left),
            ("Triangle 2", top_left, center, top_right),
            ("Triangle 3", top_right, center, bottom_right),
            ("Triangle 4", bottom_right, center, bottom_left),
        ]
        
        return triangles
    
    
    screen_width, screen_height = get_screen_resolution()
    triangles = get_triangles(screen_width, screen_height)
    
    
    def is_in_triangle(x, y, v1, v2, v3):
        x1, y1 = v1
        x2, y2 = v2
        x3, y3 = v3
    
        def is_same_side(x2, y2, x3, y3):
            return (x - x3) * (y2 - y3) - (x2 - x3) * (y - y3) < 0
    
        bool1 = is_same_side(x1, y1, x2, y2)
        bool2 = is_same_side(x2, y2, x3, y3)
        bool3 = is_same_side(x3, y3, x1, y1)
     
        return bool1 == bool2 == bool3
    
    
    def find_triangle(x, y):
        for name, v1, v2, v3 in triangles:
            if is_in_triangle(x, y, v1, v2, v3):
                return name
        return "Outside"
     
    
    if __name__ == '__main__':
        def print_event(event):
            print(f"Position: ({event.x}, {event.y})")
            print(find_triangle(event.x, event.y)) 
    
        handlers.append(print_event)
        listen()
    
    • listen()- 设置一个钩子来获取(x,y)
    • print_event(event)- 我们返回(x,y)的回调
    • get_screen_resolution()- 让我们返回屏幕尺寸。
    • get_triangles(screen_width, screen_height)- 我们得到所有4 个三角形的顶点。
    • find_triangle(x, y)/is_in_triangle(x, y, v1, v2, v3) - 这里我们已经计算(x,y)是否落入三角形,如果是,则计算是哪一个。

    在此输入图像描述

    • 1
  2. Віктор
    2024-11-20T02:47:49Z2024-11-20T02:47:49Z

    我还是不让你写评论,所以就写在这里吧。

    1 我们建立直线方程。 2 我们首先检查给定点相对于一条直线,消除2个三角形。 3 检查相对于第二条直线

    • 0
  3. Ivan Polevikov
    2024-11-20T04:10:49Z2024-11-20T04:10:49Z

    总的来说,我坐下来用方程式思考这个提案并编写了以下代码。

    import win32api
    import pyautogui
    import time
    
    width, height = 2560, 1440
    k1 = width/height
    k2 = height/width
    
    while True:
        if win32api.GetKeyState(0x01) < 0:
            x, y = pyautogui.position()
            if y * k1 < x < (height - y) * k1:
                print(2) # число - четверть согласно рисунку
            if y * k1 > x > (height - y) * k1:
                print(4)
            if x * k2 < y < (width - x) * k2:
                print(1)
            if x * k2 > y > (width - x) * k2:
                print(3)
        time.sleep(0.1)
    

    k1 和 k2 是系数,其中 x = y * k1 且 y = x * k2。 (对角函数)

    • 0

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5