请解释以下代码,谢!功能画一条直线,在直线一端一三角头,像CAD中的尺寸的标注.(50分)

  • 主题发起人 YongSoft
  • 开始时间
Y

YongSoft

Unregistered / Unconfirmed
GUEST, unregistred user!
var i : Integer;
op : TCADPlan;
p,np,p1,p2 : TPoint;
tgbeta,sinbeta : Extended;
a,b,h,dx,dy : Integer;
Const
al=15;
aw=5;
begin
with ACanvas do begin
Pen.Assign(Self.FPen);
Brush.Assign(Self.FBrush);
if (FSelectColor <> -1) then ACanvas.Pen.Color := FSelectColor;
Brush.Style := bsSolid;
Brush.Color := Pen.Color;
p1 := ScrPoint1;//直线坐标1
p2 := Scrpoint2;;//直线坐标2
PolyLine([p1,p2]);
//以下代码解释一下
p := ScrPoint1;
p.Y := FOwner.Height - p.Y;
np := ScrPoint2;
np.Y := FOwner.Height - np.Y;
h := Trunc(Sqrt(Sqr(np.X - p.X) + Sqr(np.Y - p.Y)));
al := Trunc(iArrowLength );
aw := Trunc(iArrowWidth );
if (h > al) then begin
if ((np.X - p.X) <> 0) then tgbeta := (np.Y - p.Y)/(np.X - p.X) else tgbeta := 4e4900;
sinbeta := (np.Y - p.Y)/h;
b := Trunc(np.Y - iArrowLength*sinbeta);
if (tgbeta <> 0) then a := np.X - Trunc((al*sinbeta)/tgbeta) else a := np.X - 10;
b := FOwner.Height - b;
dy := Trunc(((np.X-p.X)*aw)/h);
dx := Trunc(((np.Y-p.Y)*aw)/h);
np.Y := FOwner.Height - np.Y;
Polygon([np,Point(a + dx, b + dy),Point(a - dx, b - dy){,np}]);

end;
end;

效果 : ------------->
 
你的代码有问题。对于XY平面的第一像限不能够正确画出三角形,还有其他的问题。


p := ScrPoint1;
//坐标变换
p.Y := FOwner.Height - p.Y;
np := ScrPoint2;
//坐标变换
np.Y := FOwner.Height - np.Y;
//求直角三角形的斜边长
h := Trunc(Sqrt(Sqr(np.X - p.X) + Sqr(np.Y - p.Y)));
al := Trunc(iArrowLength );
aw := Trunc(iArrowWidth );
if (h > al) then begin
if ((np.X - p.X) <> 0) then tgbeta := (np.Y - p.Y)/(np.X - p.X) else tgbeta := 4e4900;
sinbeta := (np.Y - p.Y)/h;
b := Trunc(np.Y - iArrowLength*sinbeta);
if (tgbeta <> 0) then a := np.X - Trunc((al*sinbeta)/tgbeta) else a := np.X - 10;
//坐标变换
b := FOwner.Height - b;
dy := Trunc(((np.X-p.X)*aw)/h);
dx := Trunc(((np.Y-p.Y)*aw)/h);
//坐标变换
np.Y := FOwner.Height - np.Y;
Polygon([np,Point(a + dx, b + dy),Point(a - dx, b - dy){,np}]);
 
给你一个我的算法(可是花了我近一个小时的时间哦),注意:PaintBox1的大小可要超过200*200


procedure TForm1.PaintBox1Paint(Sender: TObject);
var i: Integer;
p,np,p1,p2 ,ScrPoint1,ScrPoint2: TPoint;
sinbeta,cosbeta : Extended;
h: Integer;
Const
al=15;
aw=5;
begin
ScrPoint1.x :=10;
ScrPoint1.y :=10;

ScrPoint2.x :=100;
ScrPoint2.y :=200;

with paintBox1.Canvas do begin
Brush.Style := bsSolid;
Brush.Color := Pen.Color;
p1 := ScrPoint1;//直线坐标1
p2 := Scrpoint2;;//直线坐标2

moveto(p1.X,p1.Y);
Lineto(p2.X,p2.Y);
h := Trunc(Sqrt(Sqr(p1.X - p2.X) + Sqr(p1.Y - p2.Y)));
sinbeta := (p2.Y - p1.Y)/h;
cosbeta := (p2.x - p1.x)/h;
p:=point(p2.X-trunc(al*cosbeta-aw*sinbeta),p2.Y-trunc(al*sinbeta+aw*cosbeta));
np:=point(p2.X-trunc(al*cosbeta+aw*sinbeta),p2.Y-trunc(al*sinbeta-aw*cosbeta));
Polygon([p2,p,np,p2]);
{moveto(p2.X,p2.Y);
Lineto(p.X,p.Y);
Lineto(np.X,np.Y);
Lineto(p2.X,p2.Y); }
end;
end;
 
顶部