Delphi Matrix和Bounds Rect

时间:2016-10-05 09:48:46

标签: delphi matrix bounds

有谁可以帮助我?

我有一个具有以下属性的对象:

FRect := TRectF( 50, 45, 150, 145 );
FRotationScale := 15;
FRotationCenter := TPointF( 0.5, 0.5 );
FScale := 1;
FScaleCenter := TPointF( 0.5, 0.5 );

使用此功能我可以在画布上绘制Matrix:

function GetMatrix: TMatrix;
var
  LRotationOffSet: TPointF;
  LScalingOffSet: TPointF;
begin
  LRotationOffSet := TPointF.Create( FRect.Left + FRect.Width * FRotationCenter.X, FRect.Top + FRect.Height * FRotationCenter.Y );
  LScalingOffSet := TPointF.Create( FRect.Left + FRect.Width * FScaleCenter.X, FRect.Top + FRect.Height * FScaleCenter.Y );
  Result :=
    TMatrix.Identity
    * TMatrix.CreateTranslation( FRect.Left, FRect.Top )
    * TMatrix.CreateTranslation( - LRotationOffSet.X, - LRotationOffSet.Y )
    * TMatrix.CreateRotation( DegToRad( FRotationAngle ) )
    * TMatrix.CreateTranslation( LRotationOffSet.X, LRotationOffSet.Y )
    * TMatrix.CreateTranslation( - LScalingOffSet.X, - LScalingOffSet.Y )
    * TMatrix.CreateScaling( FScale, FScale )
    * TMatrix.CreateTranslation( LScalingOffSet.X, LScalingOffSet.Y )
  ;
end;

工作正常:

enter image description here

正如你可以看到跟随函数我可以得到Bounds Rect:

function BoundsPoints: TArray<TPointF>;
begin
  SetLength( Result, 8 );
    Result[ 0 ] := TPointF.Create( FRect.Left, FRect.Top ) * FMatrix;
    Result[ 1 ] := TPointF.Create( FRect.Left + FRect.Width / 2, FRect.Top ) * FMatrix;
    Result[ 2 ] := TPointF.Create( FRect.Left + FRect.Width, FRect.Top ) * FMatrix;
    Result[ 3 ] := TPointF.Create( FRect.Left + FRect.Width, FRect.Top + FRect.Height / 2 ) * FMatrix;
    Result[ 4 ] := TPointF.Create( FRect.Left + FRect.Width, FRect.Top + FRect.Height ) * FMatrix;
    Result[ 5 ] := TPointF.Create( FRect.Left + FRect.Width / 2, FRect.Top + FRect.Height ) * FMatrix;
    Result[ 6 ] := TPointF.Create( FRect.Left, FRect.Top + FRect.Height ) * FMatrix;
    Result[ 7 ] := TPointF.Create( FRect.Left, FRect.Top + FRect.Height /2 ) * FMatrix;
end;

现在问题是当我拖动一些Bounds:

enter image description here

正如您在图片中看到的,我可以平滑地调整边界矩形。 但是当我释放鼠标时,我应该从Bounds Rect开始计算FRect,怎么做?

我尝试这种方式却失败了:

NormalizeRectF( [ 
  BoundsPoint[ 0 ] * FMatrix.Inverse, 
  BoundsPoint[ 2 ] * FMatrix.Inverse, 
  BoundsPoint[ 4 ] * FMatrix.Inverse, 
  BoundsPoint[ 6 ] * FMatrix.Inverse 
)

请帮助!!!!

更新

好的,我试试看:

从矩阵反向拖动的边界矩形我设置新的FRect

  FRect.TopLeft :=

    FRect.TopLeft

    + ReversedBoundsRect.TopLeft

  ;

  FRect.BottomRight :=

    FRect.TopLeft

    + ReversedBoundsRect.BottomRight

    - ReversedBoundsRect.TopLeft

  ;

几乎可以用于:

1)当FScaleCenter为0,0时我可以拖动所有角落的上下左侧否则FRACT的所有点都移动错误

2)当我拖动所有角落时FScaleCenter为0.5,0.5时,FRect的所有点都移动错误

3)当FScaleCenter为1,1时我可以拖动所有角落的底部和右边否则FRACT的所有点都移动错误

FRotationCenter也一样!

更新

普通人!

我使用以下公式抵消了FRect,并且效果很好但仅适用于FRotationCenter = 0,0!

FRect.OffSet(

        ( - ARect.Left * ( 1 - LCos ) - ARect.Top * LSin ),

        ( ARect.Left * LSin - ARect.Top * ( 1 - LCos ) )

      );

可能没有人遇到过这些麻烦,或者我不知道的数学:(!

0 个答案:

没有答案