如何确定MousePos是否接触线?

时间:2011-06-14 03:43:48

标签: delphi canvas drawing lines angle

我不确定这是否足以说明问题...

我有2个不同的点,开始 - >结束然后它形成一条线。

如果MousePos接触到行,我想在MouseMove上创建一个事件..

我所做的是使用PtInRect,但结果是矩形区域而不是线条。是否有任何功能可以使用或手动制作。任何想法?

1 个答案:

答案 0 :(得分:4)

check if the Cursor is on a line?

检查此代码(原始来源torry's
type
  TForm73 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
  private
    x1,y1,x2,y2 : Integer;
  public
  end;

var
  Form73: TForm73;

implementation

{$R *.dfm}

function PontInLine(X, Y, x1, y1, x2, y2, d: Integer): Boolean;
var
  sine, cosinus: Double;
  dx, dy, len: Integer;
begin
  if d = 0 then d := 1;
  asm
    fild(y2)
    fisub(y1) // Y-Difference
    fild(x2)
    fisub(x1) // X-Difference
    fpatan    // Angle of the line in st(0)
    fsincos   // Cosinus in st(0), Sinus in st(1)
    fstp cosinus
    fstp sine
  end;
  dx  := Round(cosinus * (x - x1) + sine * (y - y1));
  dy  := Round(cosinus * (y - y1) - sine * (x - x1));
  len := Round(cosinus * (x2 - x1) + sine * (y2 - y1)); // length of line
  Result:= (dy > -d) and (dy < d) and (dx > -d) and (dx < len + d);
end;


procedure TForm73.Button1Click(Sender: TObject);
begin
  with Canvas do
  begin
    Pen.Color := clRed;
    MoveTo(x1,y1);
    LineTo(x2,y2);
  end;
end;

procedure TForm73.FormCreate(Sender: TObject);
begin
   x1:=10;
   y1:=100;
   x2:=200;
   y2:=150;
end;

procedure TForm73.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  p: TPoint;
begin
  GetCursorPos(p);
  p := ScreenToClient(p);
  if PontInLine(p.x, p.y, x1, y1, x2, y2, 1) then
    Caption := 'Mouse on line.'
  else
    Caption := 'Mouse not on line.'
end;

<强>更新

这是等效函数PontInLine,不使用汇编(直接)。

uses
  Math;

function PontInLine(X, Y, x1, y1, x2, y2, d: Integer): Boolean;
var
  Theta,  sine, cosinus: Double;
  dx, dy, len: Integer;
begin
  if d = 0 then d := 1;
  //calc the angle of the line
  Theta:=ArcTan2( (y2-y1),(x2-x1));
  SinCos(Theta,sine, cosinus);
  dx  := Round(cosinus * (x - x1) + sine * (y - y1));
  dy  := Round(cosinus * (y - y1) - sine * (x - x1));
  len := Round(cosinus * (x2 - x1) + sine * (y2 - y1)); // length of line
  Result:= (dy > -d) and (dy < d) and (dx > -d) and (dx < len + d);
end;

http://www.swissdelphicenter.ch/screenshots/tip906.png