function PointDistance(p1,p2:tpoint):extended;
var
pow1,pow2:extended;
dx,dy:integer;
begin
dx:=p1.X-p2.X;
dy:=p1.Y-p2.Y; pow1:=dx*dx;//<<===================这里产生异常。
pow2:=dy*dy;
result:=sqrt(pow1+pow2);
end;function PointDistance(p1,p2:tpoint):extended;
var
pow1,pow2:extended;
dx,dy:integer;
begin
dx:=p1.X-p2.X;
dy:=p1.Y-p2.Y;showmessage(inttostr(dx));
showmessage(inttostr(dy));// 添加如上两句就不会有异常,哪怕p1,p2一样的值!!! 为啥????????? pow1:=dx*dx;
pow2:=dy*dy;
result:=sqrt(pow1+pow2);
end;////////////////////////////////////////////win2000 professional sp4delphi6企业版 sp2
var
pow1,pow2:extended;
dx,dy:integer;
begin
dx:=p1.X-p2.X;
dy:=p1.Y-p2.Y; pow1:=dx*dx;//<<===================这里产生异常。
pow2:=dy*dy;
result:=sqrt(pow1+pow2);
end;function PointDistance(p1,p2:tpoint):extended;
var
pow1,pow2:extended;
dx,dy:integer;
begin
dx:=p1.X-p2.X;
dy:=p1.Y-p2.Y;showmessage(inttostr(dx));
showmessage(inttostr(dy));// 添加如上两句就不会有异常,哪怕p1,p2一样的值!!! 为啥????????? pow1:=dx*dx;
pow2:=dy*dy;
result:=sqrt(pow1+pow2);
end;////////////////////////////////////////////win2000 professional sp4delphi6企业版 sp2
解决方案 »
- 读取xml文件内容的问题。
- 关于启动画面的问题
- 我想了好久,不通
- DELPHI中:如何将一个原已存在的窗口替换为另一个窗口?比如:TComboBox,想将原来的下拉窗口替换为TTreeView,3Q
- 查询日期字段里一段时间间隔的数据?
- 一个更新update语句,很急的.帮忙看看吧
- 老板让做一个信息管理系统,我做好后的基本功能都有了,一个文件,大小只有500K,点击即可运行,可老板说这样不正规,必须得有安装文件,
- 用dbgrid做类似计算器的功能,最好还能能做到和excel一样有灵活的格式,大伙能给点提示吗?
- 请问各位数据集的filter字符串太长该怎么处理?
- Delphi中对话框标题栏上有关闭按钮但不可移动?
- 请问如何在DEPHI中用语句调用SQL的存储过程.
- 数据库出错!解决马上给分!在线等!
function DotDis(x1,x2,y1,y2:Integer):Double;
Var X,y:Double;
//需要先将整形数据转换成双精度浮点型才不容易溢出。
begin
x:=(x1-x2)/5000000;
y:=(y1-y2)/5000000;
Result:=(SQRT(SQR(x)+SQR(y)))*5000000;
end;
Var X,y:Double;
//ÐèÒªÏȽ«ÕûÐÎÊý¾Ýת»»³ÉË«¾«¶È¸¡µãÐͲŲ»ÈÝÒ×Òç³ö¡£
x1,x2,y1,y2:integer;
begin
x1:=p1.X;
x2:=p2.X;
y1:=p1.Y;
y2:=p2.Y;
x:=(x1-x2)/5000000;
y:=(y1-y2)/5000000;
Result:=(SQRT(SQR(x)+SQR(y)))*5000000;
end;
异常照旧阿
var
pow1,pow2:extended;
dx,dy:integer;
begin
dx:=p1.X-p2.X;
dy:=p1.Y-p2.Y; pow1:=dx*dx;
pow2:=dy*dy;
result:=sqrt(pow1+pow2);
end;procedure TForm1.Button1Click(Sender: TObject);
var p1,p2:tpoint;
e: extended;
begin
p1.X := 2147483647;
p1.Y := 2147483647;
p2.X := 2147483647;
p2.Y := 2147483647;
e := PointDistance(p1, p2);
edit1.Text := FloatToStr(e);
end;只是回溢出而已, 不会出错啊!!!
可能你打开了溢出检查!!!
在菜单 Project - Options 中的Compile 中的
Overflow checked
工作没镐头,要准备跳槽,但不知道去边到,郁闷
uses
windows,sysutils,math;type
TDestination=(dLeft,dRight,dUp,DDown);
function OnDestination(p,center:tpoint;
Destination:TDestination;
Include45:boolean=true
):boolean;overload;function OnDestination(r,center:TRect;
Destination:TDestination;
Include45:boolean=true
):boolean;overload;
function RectTopRight(rect:TRect):TPoint;function PointDistance(p1,p2:tpoint):extended;
function RectDistance(r1,r2:trect;VisualDistance:boolean):extended;function RectBottomLeft(rect:TRect):TPoint;function PointInRect(Point:TPoint;Rect:TRect):boolean;function NearestRect(TheIndex:integer;Destination:TDestination;
const Rects:array of TRect; VisualDistance:boolean):integer;function OverLappedRects(Rect1,Rect2:TRect):boolean;function RectCenter(rect:TRect):TPoint;function NearestPoint(TheIndex:integer;Destination:TDestination;
const points:array of TPoint):integer;function ExtremeIndex(const a:array of integer;Max_true_Min_false:boolean):integer;
implementation
function ExtremeIndex(const a:array of integer;Max_true_Min_false:boolean):integer;
var
e,i:integer;
begin
result:=low(a);
e:=a[result];
if max_true_min_false then
for i:=low(a)+1 to high(a) do
begin
if a[i]>e then
begin
result:=i;
e:=a[i];
end;
end else
for i:=low(a)+1 to high(a) do
begin
if a[i]<e then
begin
result:=i;
e:=a[i];
end;
end;
end;
function PointDistance(p1,p2:tpoint):extended;
var
pow1,pow2:extended;
dx,dy:extended;
a,b,c,d: extended;
begin
// msg2(p1.x);
// beep;
a := p1.X;
b := p1.Y;
c := p2.X;
d := p2.Y;
dx:=a-c;
dy:=b-d; pow1:=dx*dx;
pow2:=dy*dy;
result:=sqrt(pow1+pow2);
end;function RectDistance(r1,r2:trect;VisualDistance:boolean):extended;
var
r1o,r2o:tpoint;
d1,d2,d:integer;
rate:double;
begin
result:=0;
r1o:=rectcenter(r1);
r2o:=rectcenter(r2);
if VisualDistance and (not overlappedrects(r1,r2)) then
begin d:=abs(r1o.X-r2o.X);
if d<>0 then
begin
d1:=abs(r1o.X-r1.Left);
d2:=abs(r2o.X-r2.Left);
end else begin
d:=abs(r1o.Y-r2o.Y);
if d=0 then exit;
d1:=abs(r1o.Y-r1.Top);
d2:=abs(r2o.Y-r2.Top);
end;
// beep;
rate:=((1.0*d1)+(d2+0.0))/d; result:=pointdistance(r1o,r2o)*(1-rate);
end else result:=pointdistance(r1o,r2o);end;
///////////////////////
function OnDestination(p,center:tpoint;
Destination:TDestination;
Include45:boolean=true):boolean;
var
dx,dy:integer;
begin
result:=false;
dx:=abs(p.X-center.X);
dy:=abs(p.Y-center.Y);
case destination of
dleft: result:=(dy<dx) and (p.X<center.X);
dright: result:=(dy<dx) and (p.X>center.X);
dup: result:=(dy>dx) and (p.Y<center.Y);
ddown: result:=(dy>dx) and (p.Y>center.Y);
end;
if include45 then result:=result or (dx=dy);
end;
function OnDestination(r,center:TRect;
Destination:TDestination;
Include45:boolean=true
):boolean;
var
tl,bl,tr,br,cp:tpoint;
begin
cp:=rectcenter(center);
tl:=r.TopLeft;
br:=r.BottomRight;
tr:=recttopright(r);
bl:=rectbottomleft(r);
result:=ondestination(tl,cp,destination,include45)
or ondestination(tr,cp,destination,include45)
or ondestination(bl,cp,destination,include45)
or ondestination(br,cp,destination,include45);
end;function NearestPoint(TheIndex:integer;Destination:TDestination;const points:array of TPoint):integer;
var
t,j,i:integer;
p,pp:tpoint;
b:boolean;
ia,indexa:array of integer;
begin
result:=theindex;
p:=points[theindex]; setlength(ia,length(points));
setlength(indexa,length(ia));
j:=0;
for i:=low(points) to high(points) do
begin
pp:=points[i];
if (i=theindex)or(not(ondestination(pp,p,destination))) then continue; case destination of
dLeft,dright: t:=pp.X;
dup,ddown: t:=pp.Y;
end;
ia[j]:=t;
indexa[j]:=i;
inc(j);
end;
setlength(ia,j);
setlength(indexa,j);
case destination of
dleft,dup: b:=true;
dright,ddown: b:=false;
end;
// msg2(length(ia));
// msgbool(length(ia)=length(indexa));
if j<=0 then exit;
i:=extremeindex(ia,b);
result:=indexa[i];
// beep;
setlength(ia,0);
setlength(indexa,0);
end;function RectCenter(rect:TRect):TPoint;
begin
result.X:=((rect.Right-rect.Left) div 2)+rect.Left;
result.Y:=((rect.Bottom-rect.Top) div 2)+rect.Top;
end;
function RectTopRight(rect:TRect):TPoint;
begin
result.X:=rect.Right;
result.Y:=rect.Top;
end;
function RectBottomLeft(rect:TRect):TPoint;
begin
result.X:=rect.Left;
result.Y:=rect.Bottom;
end;function PointInRect(Point:TPoint;Rect:TRect):boolean;
begin
result:=(point.X>=rect.Left) and (point.X<=rect.Right)
and (point.Y>=rect.Top) and (point.Y<=rect.Bottom);;
end;
function OverLappedRects(Rect1,Rect2:TRect):boolean;
begin
result:=(pointinrect(rect1.TopLeft,rect2))or
(pointinrect(rect1.BottomRight,rect2))or
(pointinrect(recttopright(rect1),rect2))or
(pointinrect(rectbottomleft(rect1),rect2))or
(pointinrect(rect2.TopLeft,rect1))or
(pointinrect(rect2.BottomRight,rect1))or
(pointinrect(recttopright(rect2),rect1))or
(pointinrect(rectbottomleft(rect2),rect1));
end;function NearestRect(TheIndex:integer;Destination:TDestination;
const Rects:array of TRect;VisualDistance:boolean):integer;
varj:integer;
points:array of tpoint;i,l:integer;
min,d:extended;
r,tr:trect;
begin{ setlength(points,length(rects));
j:=low(rects)-low(points);
for i:=low(points) to high(points) do
begin
points[i]:=rectcenter(rects[j+i]);
end;
result:= nearestpoint(theindex-j,destination,points);
setlength(points,0); exit; }
//////////////////////////////////// Curious Exceptions!!!!!!!! r:=rects[theindex];
result:=theindex;
l:=low(rects);
for i:=l to high(rects) do
begin
tr:=rects[i];
if (i=theindex)or(not ondestination(tr,r,destination,true)) then continue;
d:=rectdistance(tr,r,visualdistance);
if i=l then
min:=d
else
begin
if d<min then
begin
min:=d;
result:=i;
end;
end; end;
end;end.///////////////////////////////////////////在窗体上多方几个控件。procedure Tform1.button1clicl(sender:tobject);
var
i:integer;
a:array of trect;
begin
setlength(a,controlCount);
for i:=low(a) to high(a) do
begin a[i]:=controls[i].boundsrect;//<<===改一下
end;
i:=math_san.nearestrect(0,dleft,a,false);
setlength(a,0);
end; //测试代码的意思就是:
找到根第0个control最近的control,
math_san.nearestrect(0,dleft,a,false);
代表中心最近的,
math_san.nearestrect(0,dleft,a,true);
代表视觉上最近的,就是两个rect中心的连线与各自rect的边相交的两点的距离最小
nearestrect(theindex:integer//<<==这个参数代表基准rect再a数组中的下标,返回值是属组中根此rect相距最近的rect的下标 ,destination参数代表在哪个方向上找
/////////////////////////////////////////请帮我测试一下,是不是有EInvalidOP异常?谢谢!
用Single或者Double,就不会有这个问题了.
我Delphi坏了。5555555555~~~~打开后就出现下面几个警告:
Error reading CDKSmartClipboard.BufferScrollBars: Not enough memory for operationNot enough storage is available to process this command.Error reading MemExpansion.LeftMargin: Division by zeroThe instruction at "0x40008e48" referenced memory at "0x01db0910". The memory could not be "red".