画布拉撒路绘制组件的旋转函数

问题描述 投票:0回答:1

我在Lazarus中需要一个函数,通过它我可以旋转某些组件而不是重写它们(将x与y交换,反之亦然)例如:

我正在使用由另一个用户开发的类在这个类中,我必须使用属性并在过程NameComponent.Draw之后定义组件的名称。Motedi引擎将读取此Draw程序来绘制组件。我将组件保存在文件sch中(还有接线电阻等)当我加载电路时...我的软件会创建具有已保存属性(包括连接导线的连接点)的组件(也包括导线结)电线也具有节点属性我的软件会创建网表等

procedure SW.Draw;
var angle:extended;
begin


if (self.Orientamento = '') or (self.Orientamento = '1') then
begin
  v2d.setpen(pssolid,1,clBlue);
  v2d.SetBrush(clwhite);
  v2d.Line(x,y+30,x+100,y+30);
  v2d.line(x,y+30,x,y+120);
  v2d.line(x,y+120,x+100,y+120);
  v2d.line(x+100,y+30,x+100,y+120);
  v2d.line(x+20,y+30,x+20,y+10);     // nc-
  v2d.line(x+80,y+30,x+80,y+10);     // nc+

  v2d.line(x+20,y+120,x+20,y+140);     // n-
  v2d.line(x+80,y+120,x+80,y+140);     // n+


  // interruttore dentro
  v2d.line(x+20,y+120,x+20,y+80);    // a
  v2d.line(x+80,y+120,x+80,y+80);    // b
  v2d.line(x+20,y+80,x+40,y+80);     // c
  v2d.line(x+60,y+80,x+80,y+80);     // d

  if (self.State = 'OFF') then
  begin
    v2d.setpen(pssolid,2,clGreen);
    v2d.line(x+40,y+80,x+63,y+72);      // e
  end
  else
  begin
    v2d.setpen(pssolid,2,clGreen);
    v2d.line(x+40,y+80,x+60,y+80);
  end;
  v2d.setpen(pssolid,1,clBlue);
  v2d.SetText(clRed, 6,'', true);
  v2d.Texto(X+82, Y +110, 'N+');
  v2d.Texto(X+74, Y +35, 'NC+');
  v2d.SetText(clBlack, 6,'', true);
  v2d.Texto(X+22, Y +110, 'N-');
  v2d.Texto(X+15, Y +35, 'NC-');
  v2d.SetText(clBlack, 8,'', true);
  v2d.Texto(x+104,y+35, self.Nome);
  v2d.Texto(x+104,y+50, self.VT);
  v2d.Texto(x+104,y+65, self.VH);
  v2d.Texto(x+104,y+80, self.RON);
  v2d.Texto(x+104,y+95, self.ROFF);

end;


if (self.Orientamento = '2') then
begin
  v2d.setpen(pssolid,1,clBlue);
  v2d.SetBrush(clwhite);
  v2d.line(x+30,y,x+30,y+100);
  v2d.line(x+30,y,x+120,y);
  v2d.line(x+120,y,x+120,y+100);
  v2d.line(x+30,y+100,x+120,y+100);
  v2d.line(x+30,y+20,x+10,y+20);  // nc-
  v2d.line(x+30,y+80,x+10,y+80); // nc +
  v2d.line(x+120,y+20, x+140,y+20); // n-
  v2d.line(x+120,y+80,x+140,y+80); // n+
  v2d.line(x+120, y+20, x+80, y+20);
  v2d.line(x+120,y+80,x+80,y+80);
  v2d.line(x+80, y+20, x+80, y+40);
  v2d.line(x+80, y+60, x+80, y+80);


  if (self.State = 'OFF') then
  begin
    v2d.setpen(pssolid,2,clGreen);
//    v2d.line(x+80, y+40, x+72, y+63);
    v2d.line(x+80, y+60, x+72, y+37);
  end
  else
  begin
    v2d.setpen(pssolid,2,clGreen);
    v2d.line(x+80,y+60, x+80, y+40);
//    v2d.line(x+80, y+40, x+80, y+60);
  end;


  v2d.setpen(pssolid,1,clBlue);
  v2d.SetText(clBlack, 6,'', true);
  v2d.texto(x+110, y+82, 'N-');
  v2d.texto(x+35, y+74, 'NC-');
  v2d.SetText(clRed, 6,'', true);
  v2d.Texto(x+110, y+22, 'N+');
  v2d.Texto(x+35, y+15, 'NC+');




end;

inherited;

end;  
freepascal
1个回答
0
投票
如果您要的是代码,它支持度而不是预定义的值,这是一个示例,该示例使用Math PI在二维空间上执行点的扇形旋转:

function findCenter(Width, Height: Integer): TPoint; begin // Specify rotation center Result := TPoint.Create(Width DIV 2, Height DIV 2); end; function Rotate(What, Center: TPoint; Angle: Double): TPoint; overload; begin if Angle > 0 then begin // Perform rotation Result.X := ROUND( Center.X + (What.X - Center.X)* COS(Angle) - (What.Y - Center.Y)* SIN(Angle) ); Result.Y := ROUND( Center.X + (What.X - Center.X)* SIN(Angle) + (What.Y - Center.Y)* COS(Angle) ); end else Result := TPoint.Create(What); end; function Rotate(What, Center: TPoint; Degrees: Integer): TPoint; overload; var RotationAngle: Double; begin // Convert Degress to Radians RotationAngle := Degrees * PI/180; Result := Rotate(What, Center, RotationAngle); end; function Rotate(What: TPoint; Degrees: Integer): TPoint; overload; var RotationAngle: Double; begin RotationAngle := Degrees * PI/180; Result := Rotate(What, TPoint.Create(0,0), RotationAngle); end;

使用函数Rotate()旋转元素的位置,对于每个元素,您还需要指定元素的中心(元素的宽度和高度,除以2),信息以获取元素的中心您可以从WinAPI收集的文本(CalcTextWidth或CalcTextRect)文本的实际高度是Font.Size-1,因此无需外部功能即可获得Height值。

[注意,如果要调整多边形的大小,要调整它们的大小,您需要计算每个点之间的关系或将多边形拆分为三角形并为它们做这些(每个n高的多边形都可以分为三角形,椭圆形除外,它有自己的尺寸调整技术-修改直径)。另外,请注意,您必须计算新的区域大小和位置偏移,才能将最终图片保持在视场中。

© www.soinside.com 2019 - 2024. All rights reserved.