Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw a line from BottomLeft To TopRight with TLine

Is it possible to draw a diagonal line that runs from Bottomleft to Topright or the other way around.

I can only draw lines from TopLeft to BottomRight.

The Linetype is set to ltDiagonal. The line is always drawn from TopLeft To BottomRight

If set a negative width (bottomright is left of TopLeft), the Tline is not drawn, because the width is set to 0.

Tline(Control).LineType:=TLineType.ltDiagonal;
like image 941
r_j Avatar asked Dec 13 '25 23:12

r_j


2 Answers

You can use RotationAngle property. Set it to 90 and the line will be drawn from BottomLeft to TopRight, as well as size dimensions will swap.

like image 156
Sergey Krasilnikov Avatar answered Dec 15 '25 13:12

Sergey Krasilnikov


Here is the utility function I created to handle this situation for any two points. The TLine must be set to diagonal.

  //------------------------------------------------------------------
  // DrawLineBetweenPoints
  // TLine component draws a line bound by a rectangular box. The default
  // line goes low to high when set as a diagonal, so we need to do the 
  // math to make it handle the other quadrant possibilities.
  //------------------------------------------------------------------
  procedure DrawLineBetweenPoints(L: TLine; p1, p2: TPointF);
  begin
    L.LineType := TLineType.Diagonal;
    L.RotationCenter.X := 0.0;
    L.RotationCenter.Y := 0.0;
    if (p2.X >= p1.X) then begin
      // Line goes left to right, what about vertical?
      if (p2.Y > p1.Y) then begin
        // Case #1 - Line goes high to low, so NORMAL DIAGONAL
        L.RotationAngle := 0;
        L.Position.X := p1.X;
        L.Width := p2.X - p1.X;
        L.Position.Y := p1.Y;
        L.Height := p2.Y - p1.Y;
      end else begin
        // Case #2 - Line goes low to high, so REVERSE DIAGONAL
        // X and Y are now upper left corner and width and height reversed
        L.RotationAngle := -90;
        L.Position.X := p1.X;
        L.Width := p1.Y - p2.Y;
        L.Position.Y := p1.Y;
        L.Height := p2.X - p1.X;
      end;
    end else begin
      // Line goes right to left
      if (p1.Y > p2.Y) then begin
        // Case #3 - Line goes high to low (but reversed) so NORMAL DIAGONAL
        L.RotationAngle := 0;
        L.Position.X := p2.X;
        L.Width := p1.X - p2.X;
        L.Position.Y := p2.Y;
        L.Height := p1.Y - p2.Y;
      end else begin
        // Case #4 - Line goes low to high, REVERSE DIAGONAL
        // X and Y are now upper left corner and width and height reversed
        L.RotationAngle := -90;
        L.Position.X := p2.X;
        L.Width := p2.Y - p1.Y;
        L.Position.Y := p2.Y;
        L.Height := p1.X - p2.X;
      end;
    end;
    if (L.Height < 0.01) then L.Height := 0.1;
    if (L.Width < 0.01) then L.Width := 0.1;
  end;
like image 37
Scott Lynn Avatar answered Dec 15 '25 12:12

Scott Lynn