如何检测光标是否在一组顶点内?
How to detect whether or not the cursor is within a set of vertices?
我想检测光标是否在手机区域内circle/disk。我试图用下面的代码解决问题。 if
语句似乎是 "functioning",因为它没有报告错误,但 else
条件是唯一打印过的输出,即使光标在圆,穿过它的圆周,或穿过它的中心。
我显然遗漏了一些关于定义适当区域以检测其中鼠标的内容,但我不知道是什么。我假设(无知地)最好的选择是使用 glVertex2f()
中定义的顶点坐标,但这显然不准确或者我执行不当。如有任何帮助或指导,我们将不胜感激。
glBegin(GL_TRIANGLE_FAN);
glColor3f(1, 0, 0);
int j[5] = {100,80,60,55,50};
int z = 1;
float radius = j[z];
int xoffset = radius - (2 * radius);
int yoffset = 384;
double x1 = xoffset + radius;
double y1 = yoffset + radius * (sin(iteration));
void cursor_position_callback(GLFWwindow* w, double x, double y)
{
FILE *f = fopen("data.txt", "a");
if (cursor_x == (x1 + radius * cos(i)) && cursor_y == (y1 + radius * sin(i))){
fprintf(f,"+++%0.3f: Cursor position: %f %f (%+f %+f)\n",
glfwGetTime(),
x, y, x - cursor_x, y - cursor_y);
}
else{
fprintf(f,"---%0.3f: Cursor position: %f %f (%+f %+f)\n",
glfwGetTime(),
x, y, x - cursor_x, y - cursor_y);
}
cursor_x = x;
cursor_y = y;
fclose(f);
}
for (double i = 0; i < 2 * M_PI; i = i + ((2 * M_PI) / a))
{
glVertex2f(x1 + radius * cos(i), y1 + radius * sin(i));
glfwSetCursorPosCallback(w, cursor_position_callback);
}
iteration += 0.01;
glEnd();
glTranslatef(1.0f,0.0f,0.0f);
如果点到圆心的距离小于或等于圆的半径,则该点位于圆内。
double dx = cursor_x - circle_x;
double dy = cursor_y - circle_y;
double dist_squared = dx*dx+dy*dy;
if(dist_squared <= radius*radius) {
// inside circle
}
您的代码还有其他问题:
- Defining functions with other functions in C is a non-portable GCC extension, and likely doesn't work the way you think it works (I'm pretty sure it runs to undefined behavior as soon
i
离开范围)。
- 只能向
glfwSetCursorPosCallback
注册一个函数(或任何 GLFW 回调函数);再次调用 glfwSetCursorPosCallback
将回调设置为新函数并丢弃旧函数。
您需要保留一个全局圆列表以进行测试并在您的单一游标回调函数中对其进行检查。
我想检测光标是否在手机区域内circle/disk。我试图用下面的代码解决问题。 if
语句似乎是 "functioning",因为它没有报告错误,但 else
条件是唯一打印过的输出,即使光标在圆,穿过它的圆周,或穿过它的中心。
我显然遗漏了一些关于定义适当区域以检测其中鼠标的内容,但我不知道是什么。我假设(无知地)最好的选择是使用 glVertex2f()
中定义的顶点坐标,但这显然不准确或者我执行不当。如有任何帮助或指导,我们将不胜感激。
glBegin(GL_TRIANGLE_FAN);
glColor3f(1, 0, 0);
int j[5] = {100,80,60,55,50};
int z = 1;
float radius = j[z];
int xoffset = radius - (2 * radius);
int yoffset = 384;
double x1 = xoffset + radius;
double y1 = yoffset + radius * (sin(iteration));
void cursor_position_callback(GLFWwindow* w, double x, double y)
{
FILE *f = fopen("data.txt", "a");
if (cursor_x == (x1 + radius * cos(i)) && cursor_y == (y1 + radius * sin(i))){
fprintf(f,"+++%0.3f: Cursor position: %f %f (%+f %+f)\n",
glfwGetTime(),
x, y, x - cursor_x, y - cursor_y);
}
else{
fprintf(f,"---%0.3f: Cursor position: %f %f (%+f %+f)\n",
glfwGetTime(),
x, y, x - cursor_x, y - cursor_y);
}
cursor_x = x;
cursor_y = y;
fclose(f);
}
for (double i = 0; i < 2 * M_PI; i = i + ((2 * M_PI) / a))
{
glVertex2f(x1 + radius * cos(i), y1 + radius * sin(i));
glfwSetCursorPosCallback(w, cursor_position_callback);
}
iteration += 0.01;
glEnd();
glTranslatef(1.0f,0.0f,0.0f);
如果点到圆心的距离小于或等于圆的半径,则该点位于圆内。
double dx = cursor_x - circle_x;
double dy = cursor_y - circle_y;
double dist_squared = dx*dx+dy*dy;
if(dist_squared <= radius*radius) {
// inside circle
}
您的代码还有其他问题:
- Defining functions with other functions in C is a non-portable GCC extension, and likely doesn't work the way you think it works (I'm pretty sure it runs to undefined behavior as soon
i
离开范围)。 - 只能向
glfwSetCursorPosCallback
注册一个函数(或任何 GLFW 回调函数);再次调用glfwSetCursorPosCallback
将回调设置为新函数并丢弃旧函数。
您需要保留一个全局圆列表以进行测试并在您的单一游标回调函数中对其进行检查。