Recent

Author Topic: BGRAControls:TBGRAShape  (Read 2159 times)

morknot

  • New Member
  • *
  • Posts: 44
  • still learning
BGRAControls:TBGRAShape
« on: September 20, 2018, 11:33:46 am »
I would like to use BGRAShape to rotate a rectangle, keeping its original dimensions, using the Angle property.
 I have tried the following:

Set the original BGRAShape.Height to 50, Width to 100 and Angle to 45
Use a trackbar to increment the angle, Min 45 Max 90
In the trackbar onchange event try to adjust the BGRAShape dimensions with

BGRAShape1.Width:=Trunc(50*cos((Trackbar1.Position)*pi/180) + 100*sin(Trackbar1.Position*pi/180));

BGRAShape1.Height:= Trunc((50*sin(Trackbar1.Position)*pi/180) +(100*cos(Trackbar1.Position*pi/180)));

This does not maintain the original dimensions.

Is it possible to achieve what I want and if so is Angle related to the centre point of the shape or the X or Y axis? Is the formula I am using just plain wrong?

Grateful for any advice.
 

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: BGRAControls:TBGRAShape
« Reply #1 on: September 20, 2018, 03:31:58 pm »
I've just tested BGRAShape. On design time, I cannot configure the correct values for a proportional rectangle (ratio 2:1) with 30 degree. BGRAShape has UseRatioXY but it also can't give the correct result. You can test it yourself. So that makes me think BGRAShape cannot do what you want to achieve. Perhaps it is a bug.

Well, if want you can use my code RectRotate.zip:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, Graphics, ComCtrls, StdCtrls, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     PaintBox1: TPaintBox;
  16.     TrackBar1: TTrackBar;
  17.     procedure TrackBar1Change(Sender: TObject);
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.lfm}
  26.  
  27. { TForm1 }
  28.  
  29. procedure TForm1.TrackBar1Change(Sender: TObject);
  30. const
  31.   ShapeWidth  = 100;
  32.   ShapeHeight = 50;
  33.   degtorad    = pi/180;
  34. var
  35.   Angle            : Integer;
  36.   X1, X2, Y1, Y2   : Integer;
  37.   CanvasX, CanvasY : Integer;
  38. begin
  39.   Angle := TrackBar1.Position;
  40.   X1      := Round(ShapeWidth*cos(Angle*degtorad) - ShapeHeight*sin(Angle*degtorad));
  41.   X2      := Round(ShapeWidth*cos(Angle*degtorad) + ShapeHeight*sin(Angle*degtorad));
  42.   Y1      := Round(ShapeWidth*sin(Angle*degtorad) + ShapeHeight*cos(Angle*degtorad));
  43.   Y2      := Round(ShapeWidth*sin(Angle*degtorad) - ShapeHeight*cos(Angle*degtorad));
  44.   CanvasX := PaintBox1.Width  div 2;
  45.   CanvasY := PaintBox1.Height div 2;
  46.   PaintBox1.Refresh;
  47.   PaintBox1.Canvas.Line(CanvasX+X1, CanvasY-Y1, CanvasX+X2, CanvasY-Y2);
  48.   PaintBox1.Canvas.Line(CanvasX+X2, CanvasY-Y2, CanvasX-X1, CanvasY+Y1);
  49.   PaintBox1.Canvas.Line(CanvasX-X1, CanvasY+Y1, CanvasX-X2, CanvasY+Y2);
  50.   PaintBox1.Canvas.Line(CanvasX-X2, CanvasY+Y2, CanvasX+X1, CanvasY-Y1);
  51. end;
  52.  
  53. end.

morknot

  • New Member
  • *
  • Posts: 44
  • still learning
Re: BGRAControls:TBGRAShape
« Reply #2 on: September 21, 2018, 12:01:44 pm »
Thank you for your reply and code, Handoko. I think you are right that BGRAShape is not able to do what I want.

morknot

  • New Member
  • *
  • Posts: 44
  • still learning
Re: BGRAControls:TBGRAShape (rotation)
« Reply #3 on: September 28, 2018, 05:06:24 pm »
I don't know if anyone is interested but a partial solution to rotating a constant sized shape is to use TuEKnob or TuEMultiturn.

Create a bitmap of the shape and size you want. Assign the bitmap to the Picture property of either of the above. Set the width and height of the knob or multiturn to the maximum dimension likely to be achieved by rotating the shape to take account of the changing width and height (e.g. the corners on a rectangle).

Rotate!

 

TinyPortal © 2005-2018