窗口点击问题

本文介绍了一种基于链表的窗口管理器设计,通过自定义数据结构实现窗口的插入和快速查找。讨论了如何在不依赖传统数组下标的情况下,高效地调整窗口显示顺序,以响应用户点击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  具体题目后边补充(网易雷火)

#include<iostream>
#include<vector>
using namespace std;

int defaultId = 1;

typedef struct Win_property
{
	int X_pos;
	int Y_pos;
	int wide;
	int hight;
}Win_property;

typedef struct Window
{
	int w_id;
	Win_property property;
	struct Window* w_next;
}Win, *pWin;

pWin BuyNode()
{
	pWin res = (pWin)malloc(sizeof(Win));
	if (NULL == res)
	{
		exit(1);
	}
	res->w_id = 0;
	res->w_next = NULL;
	res->property.X_pos = 0;
	res->property.Y_pos = 0;
	res->property.wide = 0;
	res->property.hight = 0;

	return res;
}

void Init(pWin *first)
{
	*first = BuyNode();
}

void Insert(pWin pNode, int x, int y, int w, int h)  //头插
{
	pWin s = BuyNode();
	
	s->w_next = pNode->w_next;
	pNode->w_next = s;

	s->w_id = defaultId;
	defaultId++;

	s->property.X_pos = x;
	s->property.Y_pos = y;
	s->property.wide = w;
	s->property.hight = h;
}

int GetWindow(pWin qNode, int posx, int posy)
{
	while (qNode != NULL)
	{
		if (posx >= qNode->property.X_pos && posx <= qNode->property.X_pos + qNode->property.wide
			&& posy >= qNode->property.Y_pos && posy <= qNode->property.Y_pos + qNode->property.hight)
		{
			return qNode->w_id;
		}
		qNode = qNode->w_next;
	}
	return -1;
}

int GetWindowId(pWin pNode, int posx, int posy)
{
	pWin tmp = pNode;
	if (pNode == NULL && tmp->w_next == NULL)
	{
		return -1;
	}
	
	int index = GetWindow(tmp, posx, posy);

	if (pNode->w_next->w_id != index)   //返回的窗口ID不是眼前的第一个,则进行调整
	{
		while (tmp->w_next->w_id != index && tmp->w_next->w_next != NULL)
		{
			tmp = tmp->w_next;
		}

		if (tmp->w_next->w_id == index)
		{
			pWin s = tmp->w_next;
			tmp->w_next = s->w_next;
			s->w_next = pNode->w_next;
			pNode->w_next = s;
		}
	}

	return index;
}

/*
2 4
100 100 100 100
10 10 150 150
105 105
180 180
105 105
1 1
*/

int main()
{
	int x, y;        //左上角的坐标
	int w, h;        //窗口的长和宽
	int n;           //窗口数
	int m;           //鼠标点击事件个数
	int getx, gety;  //鼠标点击事件的坐标位置

	pWin firstview = NULL;   //头指针

	Init(&firstview);

	cin>>n>>m;
	vector<int> re(m, 0);

	while (n > 0)
	{
		cin >> x >> y >> w >> h;
		Insert(firstview, x, y, w, h);
		--n;
	}

	for (int i = 0; i < m; ++i)
	{
		cin >> getx >> gety;
		re[i] = GetWindowId(firstview, getx, gety);
	}

	for (int i = 0; i < m; i++)
	{
		cout << re[i]<< endl;
	}

	return 0;

}

在这里插入图片描述
  需要有一个现成的数据结构来做支撑(我是自己定义的结构和方法),如果能够找到合适的数据结构,则该题极其简单。该数据结构应该符合的要求如下:

  1. 需要一个能够保存key-value的数据结构,该结构最好脱离传统下标的束缚,因而数组之类的结构不可取

  2. 考虑到窗口调整的效率,点击窗口移动到当前视角的时间复杂度要低,同样,避免使用数组,链表是一种不错的选择

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值