2017년 9월 6일 수요일

A line drawing with two positions (x1, y1) and (x2, y2) with TLine.

A line drawing with two positions(x1, y1) and (x2, y2) with TLine.

TLine's attributes are height and width, and TLine's coordinates are x, y position at the top left, so it is not easy to draw lines by specifying (x1, y1) and (x2, y2).

I have created a method that draws two positions using TLine's Rotation Angle attribute.

The principle is to find only the line width and rotation angle value with two postion.
First, you can specify the properties of TLine as shown in the figure.
The important thing is RotationCenter.
If RotationCenter has default values ​​of 0.5 and 0.5, it rotates around the center of the line.
RotationCenter should be changed to (0,0) to rotate around the end point of the line (x1.y1).



The line width and rotation angle designation can be calculated as shown in the figure.
Red color line is a TLine.



I wrote the sample code using the above method.
The position.X values is x2>x1 when the rotation angle range is 0 ~ 180.
If the SIN value exceeds 180, the +/- value changes.
So, as in the sample code, if x1>x2 then, change two position.
The drawing order is not considered.

Even if you do not understand the principle of line drawing, you can draw a line by inputting the Thickness of two Position and line as parameters.
Of course, color change is also possible.

procedure TForm1.Draw_Line2P( x1,y1, x2,y2, thick : single );
var
  d, xtemp, ytemp, rAngle : single;
  drawLine : TLine;
begin
  if x1 > x2 then
  begin
    xtemp := x1;     ytemp := y1;
    x1 := x2;          y1 := y2;
    x2 := xtemp;     y2 := ytemp;
  end;

  d := SQRT( Power( x2-x1, 2 ) + Power( y2-y1, 2 ) );   // Uses System.Math
  rAngle := RadToDeg( ArcSin( (y2-y1)/d ));

  drawLine := TLine.Create(self);
  drawLine.Parent := self;
  drawLine.LineLocation := TLineLocation.Inner;
  drawLine.LineType := TLineType.Bottom;
  drawLine.RotationCenter.X := 0;
  drawLine.RotationCenter.Y := 0;
  drawLine.Stroke.Thickness := thick;
  drawLine.Height := 1;
  drawLine.Width  := d;
  drawLine.Position.X := x1;
  drawLine.Position.Y := y1;
  drawLine.RotationAngle := rAngle;
end;





댓글 없음:

댓글 쓰기