9fans.net/go@v0.0.5/draw/drawrepl.go (about) 1 package draw 2 3 // ReplXY clips x to be in the half-open interval [min, max) y adding or 4 // subtracking a multiple of max - min. 5 // That is, assuming [min, max) specify the base of an infinite tiling 6 // of the integer line, ReplXY returns the value of the image of x that appears 7 // in the interval. 8 func ReplXY(min, max, x int) int { 9 sx := (x - min) % (max - min) 10 if sx < 0 { 11 sx += max - min 12 } 13 return sx + min 14 } 15 16 // Repl clips the point p to be within the rectangle r by translating p 17 // horizontally by an integer multiple of the rectangle width 18 // and vertically by an integer multiple of the rectangle height. 19 // That is, it returns the point corresponding to the image of p that appears inside 20 // the base rectangle r, which represents a tiling of the plane. 21 func Repl(r Rectangle, p Point) Point { 22 return Point{ 23 ReplXY(r.Min.X, r.Max.X, p.X), 24 ReplXY(r.Min.Y, r.Max.Y, p.Y), 25 } 26 }