add Vec.WithX/WithY and Rect.WithMin/WithMax

This commit is contained in:
faiface 2017-03-23 19:27:39 +01:00
parent 4619398b9e
commit 4bebc7e6e4
1 changed files with 30 additions and 0 deletions

View File

@ -107,6 +107,16 @@ func (u Vec) Rotated(angle float64) Vec {
return u * V(cos, sin)
}
// WithX return the vector u with the x coordinate changed to the given value.
func (u Vec) WithX(x float64) Vec {
return V(x, u.Y())
}
// WithY returns the vector u with the y coordinate changed to the given value.
func (u Vec) WithY(y float64) Vec {
return V(u.X(), y)
}
// Dot returns the dot product of vectors u and v.
func (u Vec) Dot(v Vec) float64 {
return u.X()*v.X() + u.Y()*v.Y()
@ -206,6 +216,26 @@ func (r Rect) Moved(delta Vec) Rect {
}
}
// WithMin returns the Rect with it's Min changed to the given position.
//
// Note, that the Rect is not automatically normalized.
func (r Rect) WithMin(min Vec) Rect {
return Rect{
Min: min,
Max: r.Max,
}
}
// WithMax returns the Rect with it's Max changed to the given position.
//
// Note, that the Rect is not automatically normalized.
func (r Rect) WithMax(max Vec) Rect {
return Rect{
Min: r.Min,
Max: max,
}
}
// Resized returns the Rect resized to the given size while keeping the position of the given
// anchor.
//