求教一个关于画线的问题,请大家帮帮忙!!!(100分)

H

hoodlum

Unregistered / Unconfirmed
GUEST, unregistred user!
求教一个关于画线的问题,请大家帮帮忙!!!
有几条已知的曲线,要求以动态的效果将线条逐步显示出来。。。。
例如:A,B,C三条线,要求先显示A线,在显示B。。。。C。。。。。。
显示的效果要求最起码就象进度条那样,先谢谢大家了!!!![?][?][?][?]
 
用定时器就好了,先画短的然后改变参数重新画就好了,
如果打算三条线同时开始就要用到线程了
 
我认为那样并不好,会产生抖动,最好是用判断语句
判断第一条画到哪里再画第二条在判断第三条,
虽然是算法多些,但是现在的机器速度根本就不是问题.
 
使用Sleep(20)
 
请问有没有好一点的具体的算法呢?谢谢
 
用chart
你看一下chart的demo
不会产生抖动
我就是用chart作函数曲线模拟


 
下面是我写的关于线段的一个类,给出两端点,可以动态绘制出该线段.
type
TLine=class //y=ax+b
private
FLineStyle:(Horizontal,Vertical,Diagonal);
FPoints:array of TPoint;
FStartPoint:TPoint; //开始端点
FEndPoint:TPoint; //结束端点
FPointCount,FFactPointCount:Integer;
FA,FB:Double; //a,b参数
FInterval:Integer;
function GetSpace(P1,P2:TPoint):Integer;
function GetLength:Integer;
procedure CalcAB;
procedure CalcPoints;
function OnLine(P:TPoint):Boolean;
public
constructor Create;overload;
constructor Create(AStartPoint,AEndPoint:TPoint;APointCount:Integer);overload;
destructor Destroy;override;
procedure Draw(ACanvas:TCanvas);
property StartPoint:TPoint read FStartPoint write FStartPoint;
property EndPoint:TPoint read FEndPoint write FEndPoint;
property PointCount:Integer read FPointCount write FPointCount;
property Length:Integer read GetLength;
Property Interval:Integer read FInterval write FInterval;
end;

implementation

constructor TLine.Create;
begin
FInterval:=10;
end;

constructor TLine.Create(AStartPoint,AEndPoint:TPoint;APointCount:Integer);
begin
FStartPoint:=AStartPoint;
FEndPoint:=AEndPoint;
FPointCount:=APointCount;
FInterval:=10;
end;

destructor TLine.Destroy;
begin
SetLength(FPoints,0);
inherited;
end;

procedure TLine.CalcAB; //求a,b参数
begin
if (FEndPoint.X-FStartPoint.X) =0 then
begin
FA:=0;
FB:=FStartPoint.X;
FLineStyle:=Vertical;
end else
if (FEndPoint.Y-FStartPoint.Y) =0 then
begin
FA:=0;
FB:=FStartPoint.Y;
FLineStyle:=Horizontal;
end else
begin
FA:=(FEndPoint.Y-FStartPoint.Y)/(FEndPoint.X-FStartPoint.X);
FB:=FStartPoint.Y-FA*FStartPoint.X;
FLineStyle:=Diagonal;
end;
end;

function TLine.GetSpace(P1,P2:TPoint): Integer; //两点间距离
begin
Result:=Round(Sqrt(Sqr(P2.X-P1.X)+Sqr(P2.Y-P1.Y)));
end;

function TLine.GetLength: Integer; //线段长度
begin
Result:=GetSpace(FStartPoint,FEndPoint);
end;

function TLine.OnLine(P:TPoint): Boolean; //某点是否在线段上
begin
if (GetSpace(FStartPoint,P)<Length) and (GetSpace(FEndPoint,P)<Length) then
Result:=True
else
Result:=False;
end;

procedure TLine.CalcPoints; //选取线上的一些点
var
I:Integer;
PerLength:Double;
X,Y:Integer;
FlagX,FlagY:Integer;
begin
SetLength(FPoints,FPointCount);
PerLength:=Length/(FPointCount-1); //所选点间的距离
FFactPointCount:=0;
FPoints[0]:=FStartPoint;
Inc(FFactPointCount);
if FStartPoint.X>FEndPoint.X then FlagX:=-1 else FlagX:=1;
if FStartPoint.Y>FEndPoint.Y then FlagY:=-1 else FlagY:=1;
for I:=1 to FPointCount-3 do
begin
if FLineStyle=Vertical then //竖线
begin
X:=Round(FB);
Y:=Round(PerLength*I)*FlagY+FStartPoint.Y;
end else
if FLineStyle=Horizontal then //横线
begin
X:=Round(PerLength*I)*FlagX+FStartPoint.X;
Y:=Round(FB);
end else
begin //斜线
X:=Round(PerLength*I)*FlagX+FStartPoint.X;
Y:=Round(FA*X+FB);
end;
if OnLine(Point(X,Y)) then
begin
FPoints[FFactPointCount]:=Point(X,Y);
Inc(FFactPointCount);
end;
end;
FPoints[FFactPointCount]:=FEndPoint;
Inc(FFactPointCount);
SetLength(FPoints,FFactPointCount);
end;

procedure TLine.Draw(ACanvas: TCanvas); //动态绘制该直线
var
I:Integer;
begin
CalcAB;
CalcPoints;
with ACanvas do
begin
Pen.Color:=clRed;
for I:=0 to FFactPointCount-2 do
begin
Sleep(Interval); //延时
MoveTo(FPoints.X,FPoints.Y);
LineTo(FPoints[I+1].X,FPoints[I+1].Y);
end;
end;
end;
 
至於使用,應該會吧?
procedure TForm1.Button1Click(Sender: TObject);
var
Line:TLine;
begin
Line:=TLine.Create;
try
Line.Interval:=10;
Line.PointCount:=50;
Line.StartPoint:=Point(20,120);
Line.EndPoint:=Point(70,20);
Line.Draw(PaintBox1.Canvas);
Line.StartPoint:=Point(70,20);
Line.EndPoint:=Point(120,120);
Line.Draw(PaintBox1.Canvas);
Line.StartPoint:=Point(120,120);
Line.EndPoint:=Point(170,20);
Line.Draw(PaintBox1.Canvas);
Line.StartPoint:=Point(170,20);
Line.EndPoint:=Point(220,120);
Line.Draw(PaintBox1.Canvas);
finally
Line.Free;
end;
end;
 
谢谢somokingroom,分数已发出,请查收
 
很感谢你somokingroom,你的那个画直线的类很好用,再请问一下,关于曲线的该如何呢??
 
呵呵,曲線我沒有研究過~~
這個類可是特意為了回答你的問題而寫的:)
共勉!
 
hoodlum,我還沒收到分呢:)
 
专门为我写的??太感谢了!!!!你再收一下,我又发了一次!
 
smokingroom,画线用数值微分的方法该怎么作呢?

 

Similar threads

D
回复
0
查看
775
DelphiTeacher的专栏
D
D
回复
0
查看
856
DelphiTeacher的专栏
D
顶部