go-hep.org/x/hep@v0.38.1/hbook/points.go (about)

     1  // Copyright ©2016 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package hbook
     6  
     7  // Point2D is a position in a 2-dim space
     8  type Point2D struct {
     9  	X    float64 // x-position
    10  	Y    float64 // y-position
    11  	ErrX Range   // error on x-position
    12  	ErrY Range   // error on y-position
    13  }
    14  
    15  // XMin returns the X value minus negative X-error
    16  func (p Point2D) XMin() float64 {
    17  	return p.X - p.ErrX.Min
    18  }
    19  
    20  // XMax returns the X value plus positive X-error
    21  func (p Point2D) XMax() float64 {
    22  	return p.X + p.ErrX.Max
    23  }
    24  
    25  // YMin returns the Y value minus negative Y-error
    26  func (p Point2D) YMin() float64 {
    27  	return p.Y - p.ErrY.Min
    28  }
    29  
    30  // YMax returns the Y value plus positive Y-error
    31  func (p Point2D) YMax() float64 {
    32  	return p.Y + p.ErrY.Max
    33  }
    34  
    35  // ScaleX rescales the X value by a factor f.
    36  func (p *Point2D) ScaleX(f float64) {
    37  	p.X *= f
    38  	p.ErrX.Min *= f
    39  	p.ErrX.Max *= f
    40  }
    41  
    42  // ScaleY rescales the Y value by a factor f.
    43  func (p *Point2D) ScaleY(f float64) {
    44  	p.Y *= f
    45  	p.ErrY.Min *= f
    46  	p.ErrY.Max *= f
    47  }
    48  
    49  // ScaleXY rescales the X and Y values by a factor f.
    50  func (p *Point2D) ScaleXY(f float64) {
    51  	p.ScaleX(f)
    52  	p.ScaleY(f)
    53  }
    54  
    55  // points2D implements sort.Interface
    56  type points2D []Point2D
    57  
    58  func (p points2D) Len() int { return len(p) }
    59  func (p points2D) Less(i, j int) bool {
    60  	pi := p[i]
    61  	pj := p[j]
    62  	if pi.X != pj.X {
    63  		return pi.X < pj.X
    64  	}
    65  	if pi.ErrX.Min != pj.ErrX.Min {
    66  		return pi.ErrX.Min < pj.ErrX.Min
    67  	}
    68  	if pi.ErrX.Max != pj.ErrX.Max {
    69  		return pi.ErrX.Max < pj.ErrX.Max
    70  	}
    71  	if pi.Y != pj.Y {
    72  		return pi.Y < pj.Y
    73  	}
    74  	if pi.ErrY.Min != pj.ErrY.Min {
    75  		return pi.ErrY.Min < pj.ErrY.Min
    76  	}
    77  	if pi.ErrY.Max != pj.ErrY.Max {
    78  		return pi.ErrY.Max < pj.ErrY.Max
    79  	}
    80  	return false
    81  }
    82  func (p points2D) Swap(i, j int) { p[i], p[j] = p[j], p[i] }