...

Source file src/gonum.org/v1/plot/vg/geom.go

Documentation: gonum.org/v1/plot/vg

     1  // Copyright ©2016 The Gonum 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 vg
     6  
     7  // A Point is a location in 2d space.
     8  //
     9  // Points are used for drawing, not for data.  For
    10  // data, see the XYer interface.
    11  type Point struct {
    12  	X, Y Length
    13  }
    14  
    15  // Dot returns the dot product of two points.
    16  func (p Point) Dot(q Point) Length {
    17  	return p.X*q.X + p.Y*q.Y
    18  }
    19  
    20  // Add returns the component-wise sum of two points.
    21  func (p Point) Add(q Point) Point {
    22  	return Point{p.X + q.X, p.Y + q.Y}
    23  }
    24  
    25  // Sub returns the component-wise difference of two points.
    26  func (p Point) Sub(q Point) Point {
    27  	return Point{p.X - q.X, p.Y - q.Y}
    28  }
    29  
    30  // Scale returns the component-wise product of a point and a scalar.
    31  func (p Point) Scale(s Length) Point {
    32  	return Point{p.X * s, p.Y * s}
    33  }
    34  
    35  // A Rectangle represents a rectangular region of 2d space.
    36  type Rectangle struct {
    37  	Min Point
    38  	Max Point
    39  }
    40  
    41  // Size returns the width and height of a Rectangle.
    42  func (r Rectangle) Size() Point {
    43  	return Point{
    44  		X: r.Max.X - r.Min.X,
    45  		Y: r.Max.Y - r.Min.Y,
    46  	}
    47  }
    48  
    49  // Add returns the rectangle r translated by p.
    50  func (r Rectangle) Add(p Point) Rectangle {
    51  	return Rectangle{
    52  		Min: r.Min.Add(p),
    53  		Max: r.Max.Add(p),
    54  	}
    55  }
    56  
    57  // Path returns the path of a Rect specified by its
    58  // upper left corner, width and height.
    59  func (r Rectangle) Path() (p Path) {
    60  	p.Move(r.Min)
    61  	p.Line(Point{X: r.Max.X, Y: r.Min.Y})
    62  	p.Line(r.Max)
    63  	p.Line(Point{X: r.Min.X, Y: r.Max.Y})
    64  	p.Close()
    65  	return
    66  }
    67  

View as plain text