회사에서 delphi소스 보면서 프로젝트 진행하는게 있어서..........

선 위에 마우스 좌표가 존재하는지 판별 하는 알고리즘이다~

식이 궁금한데.........아~~~~~중학 수학부터 책사서 공부해야겠다ㅜㅠ

 

 

 

                               #region 델파이소스 (선 판별)
                                /*
                                function PtOnLine(const P1: TPoint; const P2: TPoint;
                                  const P: TPoint; const Ptol: Integer): Boolean;
                                {-------------------------------------------------------}
                                {  Checks if point P is on line between points P1 & P2  }
                                {-------------------------------------------------------}
                                var
                                  dx,  dy  : Integer;
                                  dx1, dy1 : Integer;
                                  a, b, c  : Integer;
                                begin
                                  Result := False;
                                  dx := P2.X - P1.X;
                                  dy := P2.Y - P1.Y;
                                  dx1 := P.X - P1.X;
                                  dy1 := P.Y - P1.Y;
                                  if (Abs(dx) > 0) and (Abs(dy) < Abs(dx)) then
                                  begin
                                    if (dx*dx1 >= 0) and (Abs(dx1) <= Abs(dx)) then
                                    begin
                                      a := (dy*dx1);
                                      b := (dx*dy1);
                                      c := Abs(dx*Ptol);
                                      if Abs(a-b) <= c then Result := True;
                                    end;
                                  end
                                  else if Abs(dy) > 0 then
                                  begin
                                    if (dy*dy1 >= 0) and (Abs(dy1) <= Abs(dy)) then
                                    begin
                                      a := (dx*dy1);
                                      b := (dy*dx1);
                                      c := Abs(dy*Ptol);
                                      if Abs(a-b) <= c then Result := True;
                                    end;
                                  end;
                                end;*/

                                #endregion
                                Point startP = link.Locations[j];
                                Point endP = link.Locations[j+1];
                                int dx = endP.X - startP.X;
                                int dy = endP.Y - startP.Y;
                                int dx1 = point.X - startP.X;
                                int dy1 = point.Y - startP.Y;
                                int a = 0, b = 0, c = 0;
                                if (Math.Abs(dx) > 0 && Math.Abs(dy) < Math.Abs(dx))
                                {
                                    if (dx * dx1 >= 0 && Math.Abs(dx1) <= Math.Abs(dx))
                                    {
                                        a = (dy * dx1);
                                        b = (dx * dy1);
                                        c = Math.Abs(dx * ptol);
                                        if (Math.Abs(a - b) <= c)
                                        {
                                            iDraw = link;
                                            return iDraw;
                                        }
                                    }
                                }
                                else if (Math.Abs(dy) > 0)
                                {
                                    if (dy * dy1 >= 0 && (Math.Abs(dy1) <= Math.Abs(dy)))
                                    {
                                        a = (dx * dy1);
                                        b = (dy * dx1);
                                        c = Math.Abs(dy * ptol);
                                        if (Math.Abs(a - b) <= c)
                                        {
                                            if (Math.Abs(a - b) <= c)
                                            {
                                                iDraw = link;
                                                return iDraw;
                                            }
                                        }
                                    }
                                }


 

 

+ Recent posts