Results 1 to 3 of 3

Thread: How simply check if mouse cursor is over specific point[i] and over of any ofpoints ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Point is a point, not rectangle Every point usually has same size, it is only necessary to save X and Y.

    I'm usually using my one and only solution to point picking, that is distance comparison. Think of a situation where 3 points are within eachothers "hitbox" or radius. With normal box focusing, 1 point will overlap the other 2, and they are harder to select. I'll try adapt it to your code, optimizing it without use of sqrt too. And since you can only focus 1 point at the time, i rid of some variables:
    Code:
    Type
      TPoint2D = record
        x, y : integer;
      end;    
    
     Var 
      Point : array of TPoint2D;
      Count: integer;
      // focusedPoint is -1 if nothing is focused, else it's focused point index
      focusedPoint: integer;
    ...
    var dist, nearest, xx, yy: single;
    begin
      nearest:=8*8; // Point radius 8 in power of 2
      focusedPoint:=-1;
      for i := 0 to Count-1 do begin
        // Get X and Y distance from cursor to point
        xx:=mousecur.x-point[i].x;
        yy:=mousecur.y-point[i].y;
        dist:=xx*xx+yy*yy;
        // Think of pythagoras distance formula
        // a^2 + b^2 = c^2
        // If both sides of comparison are in power of 2, we don't need sqrt()
        if dist<nearest then begin
          // Current point is closer to cursor than previous best match.
          // Set it as the new best match.
          nearest:=dist;
          focusedPoint:=i;
        end;
      end;
    end;

  2. #2
    Ups.. of course TPoint2D have X,Y.. Rectangle is only "collision checker"

    i update code above to actual (is possible select only one point and move with him) but i think your code looks better, mathematics is clearer than booleans.. thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •