有关在DELPHI的画布CANVAS上画圆饼的一个函数! ( 积分: 10 )

  • 主题发起人 主题发起人 wangleipin
  • 开始时间 开始时间
W

wangleipin

Unregistered / Unconfirmed
GUEST, unregistred user!
各位,最近编了一个在DELPHI中画圆饼的函数过程,想与大家分享,但为了改进,不用那么繁琐,重复代码,我做了改进,但不知为何不行呢?大家看看吧!
下面是可以正确的代码:
procedure Circle(Canvas:TCanvas;X,Y,R,start_angle,end_angle:longint;now_color:Tcolor);
//X : coordinate
//Y : coordinate
//R : radius
//start_angle: Start angle
//end_angle: End angle
//根据圆心,半径,起点角度,终点角度画圆(扇形)
var left_up_point:Tpoint;
var right_down_point:Tpoint;
var start_point:Tpoint;
var end_point:Tpoint;
//var X1,Y1, X2,Y2, X3,Y3, X4,Y4: longint;
CONST ARC=PI/180;
begin
//坐标处理换算为象素坐标
//X,Y is the centre of a circle
left_up_point.X:=X-R;
left_up_point.Y:=Y-R;
right_down_point.X:=X+R;
right_down_point.Y:=Y+R;
//X3:=0; Y3:=0; X4:=0; Y4:=0;
change_coordinate(start_angle,end_point);
change_coordinate(end_angle,start_point);
{
case start_angle of
0..45:
begin
end_point.X:=X-R;
end_point.Y:=Trunc(Y-R*tan((PI/180)*start_angle));
end;
46..90:
begin
end_point.X:=Trunc(X-R*tan(PI/2-(PI/180)*start_angle));
end_point.Y:=Y-R;
end;
91..135:
begin
end_point.X:=Trunc(X+R*tan((PI/180)*start_angle-PI/2));
end_point.Y:=Y-R;
end;
136..180:
begin
end_point.X:=X+R;
end_point.Y:=Trunc(Y-R*tan(PI-(PI/180)*start_angle));
end;
181..225:
begin
end_point.X:=X+R;
end_point.Y:=Trunc(Y+R*tan((PI/180)*start_angle-PI));
end;
226..270:
begin
end_point.X:=Trunc(X+R*tan((3*PI)/2-(PI/180)*start_angle));
end_point.Y:=Y+R;
end;
271..315:
begin
end_point.X:=Trunc(X-R*tan((PI/180)*start_angle-(3*PI)/2));
end_point.Y:=Y+R;
end;
316..360:
begin
end_point.X:=X-R;
end_point.Y:=Trunc(Y+R*tan((2*PI)-(PI/180)*start_angle));
end;
end;
case end_angle of
0..45:
begin
start_point.X:=X-R;
start_point.Y:=Trunc(Y-R*tan((PI/180)*end_angle));
end;
46..90:
begin
start_point.X:=Trunc(X-R*tan(PI/2-(PI/180)*end_angle));
start_point.Y:=Y-R;
end;
91..135:
begin
start_point.X:=Trunc(X+R*tan((PI/180)*end_angle-PI/2));
start_point.Y:=Y-R;
end;
136..180:
begin
start_point.X:=X+R;
start_point.Y:=Trunc(Y-R*tan(PI-(PI/180)*end_angle));
end;
181..225:
begin
start_point.X:=X+R;
start_point.Y:=Trunc(Y+R*tan((PI/180)*end_angle));
end;
226..270:
begin
start_point.X:=Trunc(X+R*tan(((3*PI)/2)-(PI/180)*end_angle));
start_point.Y:=Y+R;
end;
271..315:
begin
start_point.X:=Trunc(X-R*tan((PI/180)*end_angle-(3*PI)/2));
start_point.Y:=Y+R;
end;
316..360:
begin
start_point.X:=X-R;
start_point.Y:=Trunc(Y+R*tan((2*PI)-(PI/180)*end_angle));
end;
end;
}
with Canvas do
begin
// Color
Brush.Color :=now_color; //绘制圆形的颜色设置
Brush.Style := bsSolid;//绘制圆形的画刷的模式,为填充模式;
Pen.Color :=now_color; //画笔颜色
//draw circle
Canvas.Pie(left_up_point.X,left_up_point.Y,right_down_point.X,right_down_point.Y,start_point.X,start_point.Y,end_point.X,end_point.Y);
end;
end;
本过程按照顺时针方向,从左边开始为零度,可绘制圆形饼图,形参为角度
但我做了改进后,为何不对了呢?下面是改进的:
procedure Circle(Canvas:TCanvas;X,Y,R,start_angle,end_angle:longint;now_color:Tcolor);
procedure change_coordinate(tmp_angle:longint;tmp_point:Tpoint);
begin
case tmp_angle of
0..45:
begin
tmp_point.X:=X-R;
tmp_point.Y:=Trunc(Y-R*tan((PI/180)*start_angle));
end;
46..90:
begin
tmp_point.X:=Trunc(X-R*tan(PI/2-(PI/180)*start_angle));
tmp_point.Y:=Y-R;
end;
91..135:
begin
tmp_point.X:=Trunc(X+R*tan((PI/180)*start_angle-PI/2));
tmp_point.Y:=Y-R;
end;
136..180:
begin
tmp_point.X:=X+R;
tmp_point.Y:=Trunc(Y-R*tan(PI-(PI/180)*start_angle));
end;
181..225:
begin
tmp_point.X:=X+R;
tmp_point.Y:=Trunc(Y+R*tan((PI/180)*start_angle-PI));
end;
226..270:
begin
tmp_point.X:=Trunc(X+R*tan((3*PI)/2-(PI/180)*start_angle));
tmp_point.Y:=Y+R;
end;
271..315:
begin
tmp_point.X:=Trunc(X-R*tan((PI/180)*start_angle-(3*PI)/2));
tmp_point.Y:=Y+R;
end;
316..360:
begin
tmp_point.X:=X-R;
tmp_point.Y:=Trunc(Y+R*tan((2*PI)-(PI/180)*start_angle));
end;
end;
end;
//X : coordinate
//Y : coordinate
//R : radius
//start_angle: Start angle
//end_angle: End angle
//根据圆心,半径,起点角度,终点角度画圆(扇形)
var left_up_point:Tpoint;
var right_down_point:Tpoint;
var start_point:Tpoint;
var end_point:Tpoint;
//var X1,Y1, X2,Y2, X3,Y3, X4,Y4: longint;
CONST ARC=PI/180;
begin
//坐标处理换算为象素坐标
//X,Y is the centre of a circle
left_up_point.X:=X-R;
left_up_point.Y:=Y-R;
right_down_point.X:=X+R;
right_down_point.Y:=Y+R;
//X3:=0; Y3:=0; X4:=0; Y4:=0;
change_coordinate(start_angle,end_point);
change_coordinate(end_angle,start_point);
with Canvas do
begin
// Color
Brush.Color :=now_color; //绘制圆形的颜色设置
Brush.Style := bsSolid;//绘制圆形的画刷的模式,为填充模式;
Pen.Color :=now_color; //画笔颜色
//draw circle
Canvas.Pie(left_up_point.X,left_up_point.Y,right_down_point.X,right_down_point.Y,start_point.X,start_point.Y,end_point.X,end_point.Y);
end;
end;
这样可以避免代码段的重复,为何不行了呢?大家看看有什么建议?谢谢!
 
各位,最近编了一个在DELPHI中画圆饼的函数过程,想与大家分享,但为了改进,不用那么繁琐,重复代码,我做了改进,但不知为何不行呢?大家看看吧!
下面是可以正确的代码:
procedure Circle(Canvas:TCanvas;X,Y,R,start_angle,end_angle:longint;now_color:Tcolor);
//X : coordinate
//Y : coordinate
//R : radius
//start_angle: Start angle
//end_angle: End angle
//根据圆心,半径,起点角度,终点角度画圆(扇形)
var left_up_point:Tpoint;
var right_down_point:Tpoint;
var start_point:Tpoint;
var end_point:Tpoint;
//var X1,Y1, X2,Y2, X3,Y3, X4,Y4: longint;
CONST ARC=PI/180;
begin
//坐标处理换算为象素坐标
//X,Y is the centre of a circle
left_up_point.X:=X-R;
left_up_point.Y:=Y-R;
right_down_point.X:=X+R;
right_down_point.Y:=Y+R;
//X3:=0; Y3:=0; X4:=0; Y4:=0;
change_coordinate(start_angle,end_point);
change_coordinate(end_angle,start_point);
{
case start_angle of
0..45:
begin
end_point.X:=X-R;
end_point.Y:=Trunc(Y-R*tan((PI/180)*start_angle));
end;
46..90:
begin
end_point.X:=Trunc(X-R*tan(PI/2-(PI/180)*start_angle));
end_point.Y:=Y-R;
end;
91..135:
begin
end_point.X:=Trunc(X+R*tan((PI/180)*start_angle-PI/2));
end_point.Y:=Y-R;
end;
136..180:
begin
end_point.X:=X+R;
end_point.Y:=Trunc(Y-R*tan(PI-(PI/180)*start_angle));
end;
181..225:
begin
end_point.X:=X+R;
end_point.Y:=Trunc(Y+R*tan((PI/180)*start_angle-PI));
end;
226..270:
begin
end_point.X:=Trunc(X+R*tan((3*PI)/2-(PI/180)*start_angle));
end_point.Y:=Y+R;
end;
271..315:
begin
end_point.X:=Trunc(X-R*tan((PI/180)*start_angle-(3*PI)/2));
end_point.Y:=Y+R;
end;
316..360:
begin
end_point.X:=X-R;
end_point.Y:=Trunc(Y+R*tan((2*PI)-(PI/180)*start_angle));
end;
end;
case end_angle of
0..45:
begin
start_point.X:=X-R;
start_point.Y:=Trunc(Y-R*tan((PI/180)*end_angle));
end;
46..90:
begin
start_point.X:=Trunc(X-R*tan(PI/2-(PI/180)*end_angle));
start_point.Y:=Y-R;
end;
91..135:
begin
start_point.X:=Trunc(X+R*tan((PI/180)*end_angle-PI/2));
start_point.Y:=Y-R;
end;
136..180:
begin
start_point.X:=X+R;
start_point.Y:=Trunc(Y-R*tan(PI-(PI/180)*end_angle));
end;
181..225:
begin
start_point.X:=X+R;
start_point.Y:=Trunc(Y+R*tan((PI/180)*end_angle));
end;
226..270:
begin
start_point.X:=Trunc(X+R*tan(((3*PI)/2)-(PI/180)*end_angle));
start_point.Y:=Y+R;
end;
271..315:
begin
start_point.X:=Trunc(X-R*tan((PI/180)*end_angle-(3*PI)/2));
start_point.Y:=Y+R;
end;
316..360:
begin
start_point.X:=X-R;
start_point.Y:=Trunc(Y+R*tan((2*PI)-(PI/180)*end_angle));
end;
end;
}
with Canvas do
begin
// Color
Brush.Color :=now_color; //绘制圆形的颜色设置
Brush.Style := bsSolid;//绘制圆形的画刷的模式,为填充模式;
Pen.Color :=now_color; //画笔颜色
//draw circle
Canvas.Pie(left_up_point.X,left_up_point.Y,right_down_point.X,right_down_point.Y,start_point.X,start_point.Y,end_point.X,end_point.Y);
end;
end;
本过程按照顺时针方向,从左边开始为零度,可绘制圆形饼图,形参为角度
但我做了改进后,为何不对了呢?下面是改进的:
procedure Circle(Canvas:TCanvas;X,Y,R,start_angle,end_angle:longint;now_color:Tcolor);
procedure change_coordinate(tmp_angle:longint;tmp_point:Tpoint);
begin
case tmp_angle of
0..45:
begin
tmp_point.X:=X-R;
tmp_point.Y:=Trunc(Y-R*tan((PI/180)*start_angle));
end;
46..90:
begin
tmp_point.X:=Trunc(X-R*tan(PI/2-(PI/180)*start_angle));
tmp_point.Y:=Y-R;
end;
91..135:
begin
tmp_point.X:=Trunc(X+R*tan((PI/180)*start_angle-PI/2));
tmp_point.Y:=Y-R;
end;
136..180:
begin
tmp_point.X:=X+R;
tmp_point.Y:=Trunc(Y-R*tan(PI-(PI/180)*start_angle));
end;
181..225:
begin
tmp_point.X:=X+R;
tmp_point.Y:=Trunc(Y+R*tan((PI/180)*start_angle-PI));
end;
226..270:
begin
tmp_point.X:=Trunc(X+R*tan((3*PI)/2-(PI/180)*start_angle));
tmp_point.Y:=Y+R;
end;
271..315:
begin
tmp_point.X:=Trunc(X-R*tan((PI/180)*start_angle-(3*PI)/2));
tmp_point.Y:=Y+R;
end;
316..360:
begin
tmp_point.X:=X-R;
tmp_point.Y:=Trunc(Y+R*tan((2*PI)-(PI/180)*start_angle));
end;
end;
end;
//X : coordinate
//Y : coordinate
//R : radius
//start_angle: Start angle
//end_angle: End angle
//根据圆心,半径,起点角度,终点角度画圆(扇形)
var left_up_point:Tpoint;
var right_down_point:Tpoint;
var start_point:Tpoint;
var end_point:Tpoint;
//var X1,Y1, X2,Y2, X3,Y3, X4,Y4: longint;
CONST ARC=PI/180;
begin
//坐标处理换算为象素坐标
//X,Y is the centre of a circle
left_up_point.X:=X-R;
left_up_point.Y:=Y-R;
right_down_point.X:=X+R;
right_down_point.Y:=Y+R;
//X3:=0; Y3:=0; X4:=0; Y4:=0;
change_coordinate(start_angle,end_point);
change_coordinate(end_angle,start_point);
with Canvas do
begin
// Color
Brush.Color :=now_color; //绘制圆形的颜色设置
Brush.Style := bsSolid;//绘制圆形的画刷的模式,为填充模式;
Pen.Color :=now_color; //画笔颜色
//draw circle
Canvas.Pie(left_up_point.X,left_up_point.Y,right_down_point.X,right_down_point.Y,start_point.X,start_point.Y,end_point.X,end_point.Y);
end;
end;
这样可以避免代码段的重复,为何不行了呢?大家看看有什么建议?谢谢!
 
10分也要啊!
下面是你的代码和其中存在的问题:
procedure Circle(Canvas:TCanvas;X,Y,R,start_angle,end_angle:longint;now_color:Tcolor);
procedure change_coordinate(tmp_angle:longint;tmp_point:Tpoint);
begin
case tmp_angle of
0..45:
begin
tmp_point.X:=X-R;
tmp_point.Y:=Trunc(Y-R*tan((PI/180)*start_angle));
/////////////////////////////////////////////////////////////
//你太不小心了^_^,这里所有的start_angle也应该改为tmp_angle啊!
/////////////////////////////////////////////////////////////
end;
46..90:
begin
tmp_point.X:=Trunc(X-R*tan(PI/2-(PI/180)*start_angle));
tmp_point.Y:=Y-R;
end;
91..135:
begin
tmp_point.X:=Trunc(X+R*tan((PI/180)*start_angle-PI/2));
tmp_point.Y:=Y-R;
end;
136..180:
begin
tmp_point.X:=X+R;
tmp_point.Y:=Trunc(Y-R*tan(PI-(PI/180)*start_angle));
end;
......
 
谢谢你啦!不小心是我得最大毛病,可我改了,还是不能按照我得意思来绘制阿!
 
不会啊,我将两段代码绘制的图形比较了,一摸一样的啊
 
谢谢啦!是我把变量搞错了的缘故!
 
后退
顶部