unit dottedLines; { Copyright © 1990, by M. Gibbs & R. Meyer, all rights reserved. } { This unit provides routines for drawing dotted lines. All the } { procedures herein use normal QuickDraw calls, and draw in the } { active grafport. QuickDraw pen characteristics, color } { settings, etc., will all affect the lines in the usual way. } { All coordinates are local to the active grafport. } { NOTE: procedure SetBlackWhite MUST be called at least once } { prior to using any of the dotted line drawing } { procedures. It may be called subsequently at any time } { to change the line drawing parameters. } { NOTE: using a floating point processor (if possible) speeds } { the line drawing up considerably. } { NOTE: background color pixels are NOT drawn, they are SKIPPED. } interface procedure SetBlackWhite (blackPixels, whitePixels: integer); { Sets the number of fore- and background colored pixels to use } { when drawing a dotted line. blackPixels specifies the number } { of foreground colored pixels to draw before skipping } { whitePixels pixels. This pattern of fore- and background } { colored pixels is continued until the end point of the line is } { reached. THIS PROCEDURE MUST BE CALLED PRIOR TO USING ANY OF } { LINE DRAWING ROUTINES BELOW. } procedure dottedLine (x, y: integer); { Draws a dotted line in the foreground color, starting at the } { pen location and moving x pixels horizontally, y pixels } { vertically, using the pattern specified by in a call to } { SetBlackWhite. } procedure dottedLineTo (x, y: integer); { Draws a dotted line in the foreground color, starting at the } { pen location and moving to the point specified by (x,y), } { using the pattern specified by in a call to SetBlackWhite. } procedure drawDotted (x1, y1, x2, y2: integer); { Draws a dotted line in the foreground color, starting at the } { point specified by (x1,y1) and moving to the point specified } { by (x2,y2), using the pattern specified by in a call to } { SetBlackWhite. } implementation var blkPixels, whtPixels: integer; totalPixels: integer; blkRatio, whtRatio: extended; procedure SetBlackWhite (blackPixels, whitePixels: integer); begin if blackPixels < 2 then blackPixels := 2; if whitePixels < 2 then whitePixels := 2; blkPixels := blackPixels; whtPixels := whitePixels; totalPixels := blackPixels + whitePixels; blkRatio := blackPixels / totalPixels; whtRatio := whitePixels / totalPixels; end; procedure drawDottedLine (x1, y1, x2, y2: integer); var blkDeltaX, blkDeltaY: extended; whtDeltaX, whtDeltaY: extended; totalXDelta, totalYDelta: extended; lineLen: extended; iterations, x: integer; blkX, blkY: integer; temp1, temp2: extended; begin temp1 := x2 - x1; temp2 := y2 - y1; lineLen := sqrt(sqr(temp1) + sqr(temp2)); iterations := round(lineLen) div totalPixels; x := x2 - x1; blkDeltaX := x / iterations * blkRatio; whtDeltaX := x / iterations * whtRatio; x := y2 - y1; blkDeltaY := x / iterations * blkRatio; whtDeltaY := x / iterations * whtRatio; totalXDelta := blkDeltaX + whtDeltaX; totalYDelta := blkDeltaY + whtDeltaY; blkX := round(blkDeltaX); blkY := round(blkDeltaY); for x := 1 to iterations do begin line(blkX, blkY); moveto(round(x * totalXDelta + x1), round(x * totalYDelta + y1)); end; end; procedure dottedLine (x, y: integer); var penLocation: point; begin GetPen(penLocation); with penLocation do drawDottedLine(h, v, h + x, v + y); end; procedure dottedLineTo (x, y: integer); var penLocation: point; begin GetPen(penLocation); with penLocation do drawDottedLine(h, v, x, y); end; procedure drawDotted (x1, y1, x2, y2: integer); begin moveto(x1, y1); drawDottedLine(x1, y1, x2, y2); end; end.