github.com/richardwilkes/toolbox@v1.121.0/xmath/geom/visibility/segment.go (about)

     1  // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, version 2.0. If a copy of the MPL was not distributed with
     5  // this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  //
     7  // This Source Code Form is "Incompatible With Secondary Licenses", as
     8  // defined by the Mozilla Public License, version 2.0.
     9  
    10  package visibility
    11  
    12  import (
    13  	"github.com/richardwilkes/toolbox/xmath/geom"
    14  	"golang.org/x/exp/constraints"
    15  )
    16  
    17  // Segment holds the start and end points of a line.
    18  type Segment[T constraints.Float] struct {
    19  	Start geom.Point[T]
    20  	End   geom.Point[T]
    21  }
    22  
    23  // Bounds returns the bounding rectangle of this Segment. This includes a slight bit of expansion to compensate for
    24  // floating-point imprecision.
    25  func (s Segment[T]) Bounds() geom.Rect[T] {
    26  	minX := min(s.Start.X, s.End.X)
    27  	minY := min(s.Start.Y, s.End.Y)
    28  	return geom.NewRect[T](minX-epsilon, minY-epsilon, max(s.Start.X, s.End.X)-minX+epsilon*2,
    29  		max(s.Start.Y, s.End.Y)-minY+epsilon*2)
    30  }