9fans.net/go@v0.0.5/draw/line.go (about)

     1  package draw
     2  
     3  // An End specifies how to draw the end of a line.
     4  type End int
     5  
     6  // EndSquare terminates the line perpendicularly to the direction of the
     7  // line; a thick line with EndSquare on both ends will be a rectangle.
     8  // EndDisc terminates the line by drawing a disc of diameter 1+2*thick
     9  // centered on the end point. EndArrow terminates the line with an
    10  // arrowhead whose tip touches the endpoint.
    11  // See the Arrow function for more control over arrow shapes.
    12  const (
    13  	// EndSquare terminates the line perpindicularly
    14  	// to the direction of the line; a thick line with EndSquare
    15  	// on both ends will be a rectangle.
    16  	EndSquare End = 0
    17  
    18  	// EndDisc terminates the line by drawing a disc of diameter 1+2*thick
    19  	// centered on the end point.
    20  	EndDisc End = 1
    21  
    22  	// EndArrow terminates the line with an arrowhead whose tip
    23  	// touches the endpoint.
    24  	// Use the Arrow function for more control over the arrow shape.
    25  	EndArrow End = 2
    26  )
    27  
    28  // Arrow permits explicit control of the shape of a line-ending arrow.
    29  // If all three parameters are zero, it produces the default arrowhead.
    30  // Otherwise, a sets the distance along line from the end of the regular line
    31  // to the tip, b sets the distance along line from the barb to the tip,
    32  // and c sets the distance perpendicular to the line from the edge of the line
    33  // to the tip of the barb, all in pixels.
    34  func Arrow(a, b, c int) End {
    35  	return EndArrow | End(a<<5|b<<14|c<<23)
    36  }
    37  
    38  // Line draws in dst a line of width 1+2*thick pixels joining p0 and p1.
    39  // The line is drawn using pixels from the src image aligned so sp in the
    40  // source corresponds to p0 in the destination. The line touches both p0
    41  // and p1.  End0 and end1 specify how the ends of the line are drawn;
    42  // see the documentation for EndSquare and the Arrow function.
    43  //
    44  // Line and the other geometrical operators are equivalent to calls to
    45  // Draw using a mask produced by the geometric procedure.
    46  func (dst *Image) Line(p0, p1 Point, end0, end1 End, thick int, src *Image, sp Point) {
    47  	dst.Display.mu.Lock()
    48  	defer dst.Display.mu.Unlock()
    49  	dst.lineOp(p0, p1, end0, end1, thick, src, sp, SoverD)
    50  }
    51  
    52  // LineOp draws a line in the source color from p0 to p1, of thickness
    53  // 1+2*radius, with the specified ends. The source is aligned so sp corresponds
    54  // to p0. See the Plan 9 documentation for more information.
    55  func (dst *Image) LineOp(p0, p1 Point, end0, end1 End, radius int, src *Image, sp Point, op Op) {
    56  	dst.Display.mu.Lock()
    57  	defer dst.Display.mu.Unlock()
    58  	dst.lineOp(p0, p1, end0, end1, radius, src, sp, op)
    59  }
    60  
    61  func (dst *Image) lineOp(p0, p1 Point, end0, end1 End, radius int, src *Image, sp Point, op Op) {
    62  	setdrawop(dst.Display, op)
    63  	a := dst.Display.bufimage(1 + 4 + 2*4 + 2*4 + 4 + 4 + 4 + 4 + 2*4)
    64  	a[0] = 'L'
    65  	bplong(a[1:], uint32(dst.id))
    66  	bplong(a[5:], uint32(p0.X))
    67  	bplong(a[9:], uint32(p0.Y))
    68  	bplong(a[13:], uint32(p1.X))
    69  	bplong(a[17:], uint32(p1.Y))
    70  	bplong(a[21:], uint32(end0))
    71  	bplong(a[25:], uint32(end1))
    72  	bplong(a[29:], uint32(radius))
    73  	bplong(a[33:], uint32(src.id))
    74  	bplong(a[37:], uint32(sp.X))
    75  	bplong(a[41:], uint32(sp.Y))
    76  }