| |
| |
| |
|
|
| package image |
|
|
| import ( |
| "image/color" |
| "math/bits" |
| "strconv" |
| ) |
|
|
| |
| type Point struct { |
| X, Y int |
| } |
|
|
| |
| func (p Point) String() string { |
| return "(" + strconv.Itoa(p.X) + "," + strconv.Itoa(p.Y) + ")" |
| } |
|
|
| |
| func (p Point) Add(q Point) Point { |
| return Point{p.X + q.X, p.Y + q.Y} |
| } |
|
|
| |
| func (p Point) Sub(q Point) Point { |
| return Point{p.X - q.X, p.Y - q.Y} |
| } |
|
|
| |
| func (p Point) Mul(k int) Point { |
| return Point{p.X * k, p.Y * k} |
| } |
|
|
| |
| func (p Point) Div(k int) Point { |
| return Point{p.X / k, p.Y / k} |
| } |
|
|
| |
| func (p Point) In(r Rectangle) bool { |
| return r.Min.X <= p.X && p.X < r.Max.X && |
| r.Min.Y <= p.Y && p.Y < r.Max.Y |
| } |
|
|
| |
| |
| func (p Point) Mod(r Rectangle) Point { |
| w, h := r.Dx(), r.Dy() |
| p = p.Sub(r.Min) |
| p.X = p.X % w |
| if p.X < 0 { |
| p.X += w |
| } |
| p.Y = p.Y % h |
| if p.Y < 0 { |
| p.Y += h |
| } |
| return p.Add(r.Min) |
| } |
|
|
| |
| func (p Point) Eq(q Point) bool { |
| return p == q |
| } |
|
|
| |
| |
| |
| var ZP Point |
|
|
| |
| func Pt(X, Y int) Point { |
| return Point{X, Y} |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| type Rectangle struct { |
| Min, Max Point |
| } |
|
|
| |
| func (r Rectangle) String() string { |
| return r.Min.String() + "-" + r.Max.String() |
| } |
|
|
| |
| func (r Rectangle) Dx() int { |
| return r.Max.X - r.Min.X |
| } |
|
|
| |
| func (r Rectangle) Dy() int { |
| return r.Max.Y - r.Min.Y |
| } |
|
|
| |
| func (r Rectangle) Size() Point { |
| return Point{ |
| r.Max.X - r.Min.X, |
| r.Max.Y - r.Min.Y, |
| } |
| } |
|
|
| |
| func (r Rectangle) Add(p Point) Rectangle { |
| return Rectangle{ |
| Point{r.Min.X + p.X, r.Min.Y + p.Y}, |
| Point{r.Max.X + p.X, r.Max.Y + p.Y}, |
| } |
| } |
|
|
| |
| func (r Rectangle) Sub(p Point) Rectangle { |
| return Rectangle{ |
| Point{r.Min.X - p.X, r.Min.Y - p.Y}, |
| Point{r.Max.X - p.X, r.Max.Y - p.Y}, |
| } |
| } |
|
|
| |
| |
| |
| func (r Rectangle) Inset(n int) Rectangle { |
| if r.Dx() < 2*n { |
| r.Min.X = (r.Min.X + r.Max.X) / 2 |
| r.Max.X = r.Min.X |
| } else { |
| r.Min.X += n |
| r.Max.X -= n |
| } |
| if r.Dy() < 2*n { |
| r.Min.Y = (r.Min.Y + r.Max.Y) / 2 |
| r.Max.Y = r.Min.Y |
| } else { |
| r.Min.Y += n |
| r.Max.Y -= n |
| } |
| return r |
| } |
|
|
| |
| |
| func (r Rectangle) Intersect(s Rectangle) Rectangle { |
| if r.Min.X < s.Min.X { |
| r.Min.X = s.Min.X |
| } |
| if r.Min.Y < s.Min.Y { |
| r.Min.Y = s.Min.Y |
| } |
| if r.Max.X > s.Max.X { |
| r.Max.X = s.Max.X |
| } |
| if r.Max.Y > s.Max.Y { |
| r.Max.Y = s.Max.Y |
| } |
| |
| |
| |
| |
| if r.Empty() { |
| return Rectangle{} |
| } |
| return r |
| } |
|
|
| |
| func (r Rectangle) Union(s Rectangle) Rectangle { |
| if r.Empty() { |
| return s |
| } |
| if s.Empty() { |
| return r |
| } |
| if r.Min.X > s.Min.X { |
| r.Min.X = s.Min.X |
| } |
| if r.Min.Y > s.Min.Y { |
| r.Min.Y = s.Min.Y |
| } |
| if r.Max.X < s.Max.X { |
| r.Max.X = s.Max.X |
| } |
| if r.Max.Y < s.Max.Y { |
| r.Max.Y = s.Max.Y |
| } |
| return r |
| } |
|
|
| |
| func (r Rectangle) Empty() bool { |
| return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y |
| } |
|
|
| |
| |
| func (r Rectangle) Eq(s Rectangle) bool { |
| return r == s || r.Empty() && s.Empty() |
| } |
|
|
| |
| func (r Rectangle) Overlaps(s Rectangle) bool { |
| return !r.Empty() && !s.Empty() && |
| r.Min.X < s.Max.X && s.Min.X < r.Max.X && |
| r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y |
| } |
|
|
| |
| func (r Rectangle) In(s Rectangle) bool { |
| if r.Empty() { |
| return true |
| } |
| |
| |
| return s.Min.X <= r.Min.X && r.Max.X <= s.Max.X && |
| s.Min.Y <= r.Min.Y && r.Max.Y <= s.Max.Y |
| } |
|
|
| |
| |
| func (r Rectangle) Canon() Rectangle { |
| if r.Max.X < r.Min.X { |
| r.Min.X, r.Max.X = r.Max.X, r.Min.X |
| } |
| if r.Max.Y < r.Min.Y { |
| r.Min.Y, r.Max.Y = r.Max.Y, r.Min.Y |
| } |
| return r |
| } |
|
|
| |
| func (r Rectangle) At(x, y int) color.Color { |
| if (Point{x, y}).In(r) { |
| return color.Opaque |
| } |
| return color.Transparent |
| } |
|
|
| |
| func (r Rectangle) RGBA64At(x, y int) color.RGBA64 { |
| if (Point{x, y}).In(r) { |
| return color.RGBA64{0xffff, 0xffff, 0xffff, 0xffff} |
| } |
| return color.RGBA64{} |
| } |
|
|
| |
| func (r Rectangle) Bounds() Rectangle { |
| return r |
| } |
|
|
| |
| func (r Rectangle) ColorModel() color.Model { |
| return color.Alpha16Model |
| } |
|
|
| |
| |
| |
| var ZR Rectangle |
|
|
| |
| |
| |
| func Rect(x0, y0, x1, y1 int) Rectangle { |
| if x0 > x1 { |
| x0, x1 = x1, x0 |
| } |
| if y0 > y1 { |
| y0, y1 = y1, y0 |
| } |
| return Rectangle{Point{x0, y0}, Point{x1, y1}} |
| } |
|
|
| |
| |
| func mul3NonNeg(x int, y int, z int) int { |
| if (x < 0) || (y < 0) || (z < 0) { |
| return -1 |
| } |
| hi, lo := bits.Mul64(uint64(x), uint64(y)) |
| if hi != 0 { |
| return -1 |
| } |
| hi, lo = bits.Mul64(lo, uint64(z)) |
| if hi != 0 { |
| return -1 |
| } |
| a := int(lo) |
| if (a < 0) || (uint64(a) != lo) { |
| return -1 |
| } |
| return a |
| } |
|
|
| |
| |
| func add2NonNeg(x int, y int) int { |
| if (x < 0) || (y < 0) { |
| return -1 |
| } |
| a := x + y |
| if a < 0 { |
| return -1 |
| } |
| return a |
| } |
|
|