|
| 1 | +package w4 |
| 2 | + |
| 3 | +type u8 = uint8 |
| 4 | + |
| 5 | +// A point on the plane. |
| 6 | +type Point struct { |
| 7 | + X u8 |
| 8 | + Y u8 |
| 9 | +} |
| 10 | + |
| 11 | +// A point with equal x value and y set to 0. |
| 12 | +func (p Point) XAxis() Point { |
| 13 | + p.Y = 0 |
| 14 | + return p |
| 15 | +} |
| 16 | + |
| 17 | +// A point with equal y value and x set to 0. |
| 18 | +func (p Point) YAxis() Point { |
| 19 | + p.X = 0 |
| 20 | + return p |
| 21 | +} |
| 22 | + |
| 23 | +// The componentwise minimum of two Points. |
| 24 | +func (p Point) ComponentMin(other Point) Point { |
| 25 | + if other.X < p.X { |
| 26 | + p.X = other.X |
| 27 | + } |
| 28 | + if other.Y < p.Y { |
| 29 | + p.Y = other.Y |
| 30 | + } |
| 31 | + return p |
| 32 | +} |
| 33 | + |
| 34 | +// The componentwise maximum of two Points. |
| 35 | +func (p Point) ComponentMax(other Point) Point { |
| 36 | + if other.X > p.X { |
| 37 | + p.X = other.X |
| 38 | + } |
| 39 | + if other.Y > p.Y { |
| 40 | + p.Y = other.Y |
| 41 | + } |
| 42 | + return p |
| 43 | +} |
| 44 | + |
| 45 | +// Convert the Point to a Size with width equal to x and height equal to y. |
| 46 | +func (p Point) AsSize() Size { |
| 47 | + return Size{Width: p.X, Height: p.Y} |
| 48 | +} |
| 49 | + |
| 50 | +// Add size width to the point x and size height to the point y. |
| 51 | +func (p Point) AddSize(s Size) Point { |
| 52 | + p.X += s.Width |
| 53 | + p.Y += s.Height |
| 54 | + return p |
| 55 | +} |
| 56 | + |
| 57 | +// If the point is outside of the screen, wrap it around to fit on the screen. |
| 58 | +func (p Point) Wrap() Point { |
| 59 | + p.X %= 160 |
| 60 | + p.Y %= 160 |
| 61 | + return p |
| 62 | +} |
| 63 | + |
| 64 | +// Size of a 2D shape. |
| 65 | +type Size struct { |
| 66 | + Width u8 |
| 67 | + Height u8 |
| 68 | +} |
| 69 | + |
| 70 | +// Add two sizes together componentwise. |
| 71 | +func (s Size) AddSize(other Size) Size { |
| 72 | + s.Width += other.Width |
| 73 | + s.Height += other.Height |
| 74 | + return s |
| 75 | +} |
| 76 | + |
| 77 | +// Convert Size to a Point with x set to width and y set to height. |
| 78 | +func (s Size) AsPoint() Point { |
| 79 | + return Point{X: s.Width, Y: s.Height} |
| 80 | +} |
| 81 | + |
| 82 | +// The componentwise minimum of two Sizes. |
| 83 | +func (s Size) ComponentMin(other Size) Size { |
| 84 | + if other.Width < s.Width { |
| 85 | + s.Width = other.Width |
| 86 | + } |
| 87 | + if other.Height < s.Height { |
| 88 | + s.Height = other.Height |
| 89 | + } |
| 90 | + return s |
| 91 | +} |
| 92 | + |
| 93 | +// The componentwise maximum of two Sizes. |
| 94 | +func (s Size) ComponentMax(other Size) Size { |
| 95 | + if other.Width > s.Width { |
| 96 | + s.Width = other.Width |
| 97 | + } |
| 98 | + if other.Height > s.Height { |
| 99 | + s.Height = other.Height |
| 100 | + } |
| 101 | + return s |
| 102 | +} |
0 commit comments