0

I would like to detect whether or not the cursor is within the area of a mobile circle/disk. I've attempted to solve the problem with the code below. The if statement seems to be "functioning", as it does not report an error, but the else condition is the only output ever printed, even when the cursor is moved within the area of the circle, across its circumference, or through its center.

I am obviously missing something with regard to defining the appropriate area to detect the mouse within, but I cannot figure what. I assumed (ignorantly) that the best option would be to use the vertex coordinates defined within the glVertex2f(), but that is either obviously inaccurate or I have executed it improperly. Any assistance or guidance would be appreciated.

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);
zzz
  • 123
  • 1
  • 9
  • are you sure you that opening a `FILE *f = fopen("data.txt", "a");` each time mouse is being moved is the right way to address your problem? – user3078414 May 09 '16 at 19:34
  • @user3078414 It looks like debugging code. Inefficiencies are usually acceptable when dealing with buggy code. – Xirema May 09 '16 at 19:41
  • @user3078414 Xirema is correct. I've been attempting to check functionality. To use or not to use the mentioned fopen code or addressing its efficiency/inefficiency seems to circumnavigate the problem of whether or not the cursor is detected within the vertices, unless you were approaching an untyped thought that would also explain why it was not being detected. Thank you for the thought though! – zzz May 09 '16 at 19:57
  • If you know the _center of the circle_, triangulated as fan of `n` _triangle elements_, and its _radius_, for each _element_ you can check if your _point_ is inside that [triangle](http://stackoverflow.com/questions/2049582/how-to-determine-a-point-in-a-2d-triangle). – user3078414 May 09 '16 at 20:38
  • `if (cursor_x == (x1 + radius * cos(i)) && cursor_y == (y1 + radius * sin(i))` is pretty much impossible to achieve. It should be something like `if (sqrt(pow(cursor_x, 2) + pow(cursor_y, 2) < radius)` – RecursiveExceptionException Jun 14 '16 at 22:00

1 Answers1

2

A point lies within a circle if the distance from the point to the center point of the circle is less than or equal to the circle's radius.

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
}

Your code has other issues:

  • Defining functions withing other functions in C is a non-portable GCC extension, and likely does not work the way you think it works (I'm pretty sure it runs into undefined behavior as soon as i leaves scope).
  • Only one function may be registered with glfwSetCursorPosCallback (or any GLFW callback function); calling glfwSetCursorPosCallback again sets the callback to the new function and discards the old one.

You need to keep a global list of circles to test against and check it in your singular cursor callback function.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
  • Like a charm. Thank you for the corrections. I knew of the non-portable issue, but, I must admit, your second point did escape me until reading and checking it. Thank you for the teaching moment! Appreciated. – zzz May 10 '16 at 20:53