Board index » delphi » Drawing non solid line with line width greater than one

Drawing non solid line with line width greater than one

Hi,
1) Does any body knows how a I can draw a non Solid line e.g dotted line,
dashed line etc on a canvas.
 But the line width should be greater than 1.

2) Or is there nay Delphi or C++ Builder libraries/components that I can
purchase?
Thanks In advance

warmest regards
Wong Yeow Mun

 

Re:Drawing non solid line with line width greater than one


Quote
yeowmun wrote:

> 1) Does any body knows how a I can draw a non Solid line e.g dotted line,
> dashed line etc on a canvas.
>  But the line width should be greater than 1.

The only way I know to do this is by drawing lots of short line segments
yourself.

Here is an example of code I use:

{*****************************************************************************

Draws a dotted line. This function is used to circumvent the limitation in
Windows 95 that prohibits the GDI from outputting lines wider than a pen
width
of 1 that are not solid.

Parameters:
  - Canvas: The Canvas to draw the line on.
  - P1, P2: The starting and ending coordinates of the line.

Written 1999 by Josef Garvi

*****************************************************************************}
procedure DrawDottedLine(Canvas: TCanvas; P1, P2: TPoint);
var
  xStyle: TPenStyle;
  Slant: Extended;
  Length: Extended;
  Fill, Jump, Segment, SegNum: Integer;
  Pos, EndPos: TPoint;
  LengthDone: Extended;
begin
  xStyle := Canvas.Pen.Style;
  Canvas.Pen.Style := psSolid;
  Slant := (Angle(P1, P2));
  Length := Distance(P1, P2);
  Fill := 10 + (Canvas.Pen.Width - 1) * 5;
  Jump := Fill; // * 2 div 3;
  Segment := Jump + Fill;
  SegNum := 0;
  LengthDone := 0;
  Pos := P1;
  while LengthDone < Length do begin
    Pos.X := P1.X + Round(CirculDistX(Slant, Segment * SegNum));
    Pos.Y := P1.Y + Round(CirculDistY(Slant, Segment * SegNum));
    Canvas.PenPos := Pos;
    if Length - LengthDone < Fill then
      EndPos := P2
    else begin
      EndPos.X := Pos.X + Round(CirculDistX(Slant, Fill));
      EndPos.Y := Pos.Y + Round(CirculDistY(Slant, Fill));
    end;
    Canvas.LineTo(EndPos.X, EndPos.Y);
    Inc(SegNum);
    LengthDone := SegNum * Segment;
  end;
  Canvas.Pen.Style := xStyle;
end;

--
Josef Garvi
Eden Foundation, Skreav. 45B, S-311 72 Falkenberg, Sweden
Tel: +46 346-53157, Fax: +46 346-53171
E-Mail: jo...@eden-foundation.org
WWW: http://www.eden-foundation.org/

Re:Drawing non solid line with line width greater than one


Here is something i just wipped together:

procedure ThickLine(x1, y1, x2, y2, Thickness: Integer; Canvas: TCanvas);
var
  vx, vy: Integer;
  d: Extended;
begin
  vx := x2 - x1;
  vy := y2 - y1;
  d  := Sqrt(vx * vx + vy * vy);
  vx := Round(vx * Thickness / d);
  vy := Round(vy * Thickness / d);

  Canvas.MoveTo(x1 + vy, y1 - vx);
  Canvas.LineTo(x1 - vy, y1 + vx);
  Canvas.LineTo(x2 - vy, y2 + vx);
  Canvas.LineTo(x2 + vy, y2 - vx);
  Canvas.LineTo(x1 + vy, y1 - vx);
end;

use it like this:

  Canvas.Pen.Style := psDash;
  ThickLine(ox, oy, x, y, 10, Canvas);

regards David

Re:Drawing non solid line with line width greater than one


I'm sorry I didn't post this in the same thread, but im currently offline,
and I don't have my last answer cached.

Here is a slight modification, I was not sure exactly what you wanted:

procedure ThickSolidLine(x1, y1, x2, y2, Thickness: Integer; Canvas:
TCanvas);
var
  vx, vy: Integer;
  d: Extended;
  Points: array[0..3] of TPoint;
begin
  vx := x2 - x1;
  vy := y2 - y1;
  d  := Sqrt(vx * vx + vy * vy);
  vx := Round(vx * Thickness / d);
  vy := Round(vy * Thickness / d);

  Points[0] := Point(x1 + vy, y1 - vx);
  Points[1] := Point(x1 - vy, y1 + vx);
  Points[2] := Point(x2 - vy, y2 + vx);
  Points[3] := Point(x2 + vy, y2 - vx);
  Canvas.Polygon(Points);
end;

use it like this:

  Canvas.BRUSH{not pen}.Style := bsDiagCross;
  ThickSolidLine(20, 20, 100, 100, 10, Canvas);

Other Threads