github.com/searKing/golang/go@v1.2.117/image/point.go (about) 1 // Copyright 2022 The searKing Author. 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 image 6 7 import ( 8 "fmt" 9 "image" 10 ) 11 12 // A Point2f is an X, Y coordinate pair. The axes increase right and down. 13 type Point2f struct { 14 X, Y float32 15 } 16 17 // String returns a string representation of p like "(3,4)". 18 func (p Point2f) String() string { 19 return fmt.Sprintf("(%.2f,%.2f)", p.X, p.Y) 20 } 21 22 // Add returns the vector p+q. 23 func (p Point2f) Add(q Point2f) Point2f { 24 return Point2f{p.X + q.X, p.Y + q.Y} 25 } 26 27 // Sub returns the vector p-q. 28 func (p Point2f) Sub(q Point2f) Point2f { 29 return Point2f{p.X - q.X, p.Y - q.Y} 30 } 31 32 // Mul returns the vector p*k. 33 func (p Point2f) Mul(k float32) Point2f { 34 return Point2f{p.X * k, p.Y * k} 35 } 36 37 // Div returns the vector p/k. 38 func (p Point2f) Div(k float32) Point2f { 39 return Point2f{p.X / k, p.Y / k} 40 } 41 42 // In reports whether p is in r. 43 func (p Point2f) In(r Rectangle2f) bool { 44 return r.Min.X <= p.X && p.X < r.Max.X && 45 r.Min.Y <= p.Y && p.Y < r.Max.Y 46 } 47 48 // Mod returns the point q in r such that p.X-q.X is a multiple of r's width 49 // and p.Y-q.Y is a multiple of r's height. 50 func (p Point2f) Mod(r image.Rectangle) image.Point { 51 p2 := p.RoundPoint() 52 return p2.Mod(r) 53 } 54 55 // Eq reports whether p and q are equal. 56 func (p Point2f) Eq(q Point2f) bool { 57 return p == q 58 } 59 60 func (p Point2f) RoundPoint() image.Point { 61 return image.Pt(round(p.X), round(p.Y)) 62 } 63 64 // ZP2f is the zero Point2f. 65 // 66 // Deprecated: Use a literal image.Point2f{} instead. 67 var ZP2f Point2f 68 69 // Pt2f is shorthand for Point2f{X, Y}. 70 func Pt2f(X, Y float32) Point2f { 71 return Point2f{X, Y} 72 }