MCPcopy
hub / github.com/mum4k/termdash / Diagonal

Function Diagonal

private/segdisp/segment/segment.go:392–439  ·  view source on GitHub ↗

Diagonal draws a diagonal segment of the specified width filling the area.

(bc *braille.Canvas, ar image.Rectangle, width int, dt DiagonalType, opts ...DiagonalOption)

Source from the content-addressed store, hash-verified

390
391// Diagonal draws a diagonal segment of the specified width filling the area.
392func Diagonal(bc *braille.Canvas, ar image.Rectangle, width int, dt DiagonalType, opts ...DiagonalOption) error {
393 if err := validArea(ar); err != nil {
394 return err
395 }
396 if min := 1; width < min {
397 return fmt.Errorf("invalid width %d, must be width >= %d", width, min)
398 }
399 opt := &diagonalOptions{}
400 for _, o := range opts {
401 o.set(opt)
402 }
403
404 var start, end image.Point
405 var nextFn nextDiagLineFn
406 switch dt {
407 case LeftToRight:
408 start = ar.Min
409 end = image.Point{ar.Max.X - 1, ar.Max.Y - 1}
410 nextFn = nextLRLine
411
412 case RightToLeft:
413 start = image.Point{ar.Max.X - 1, ar.Min.Y}
414 end = image.Point{ar.Min.X, ar.Max.Y - 1}
415 nextFn = nextRLLine
416
417 default:
418 return fmt.Errorf("unsupported diagonal type %v(%d)", dt, dt)
419 }
420
421 if err := draw.BrailleLine(bc, start, end, draw.BrailleLineCellOpts(opt.cellOpts...)); err != nil {
422 return err
423 }
424
425 ns := start
426 ne := end
427 for i := 1; i < width; i++ {
428 ns, ne = nextFn(i, start, end, ns, ne)
429
430 if !ns.In(ar) || !ne.In(ar) {
431 return fmt.Errorf("cannot draw diagonal segment of width %d in area %v, the area isn't large enough for line %v-%v", width, ar, ns, ne)
432 }
433
434 if err := draw.BrailleLine(bc, ns, ne, draw.BrailleLineCellOpts(opt.cellOpts...)); err != nil {
435 return err
436 }
437 }
438 return nil
439}
440
441// nextLRLine is a function that determines the start and end points of the
442// next line of a left-to-right diagonal segment.

Callers 3

MustDiagonalFunction · 0.92
DrawMethod · 0.92
TestDiagonalFunction · 0.85

Calls 4

BrailleLineFunction · 0.92
BrailleLineCellOptsFunction · 0.92
validAreaFunction · 0.85
setMethod · 0.65

Tested by 1

TestDiagonalFunction · 0.68