Skip to content

Instantly share code, notes, and snippets.

@NicolaM94
Created January 3, 2024 21:01
Show Gist options
  • Save NicolaM94/0f9826a99162b2ca2978149b9943bd4f to your computer and use it in GitHub Desktop.
Save NicolaM94/0f9826a99162b2ca2978149b9943bd4f to your computer and use it in GitHub Desktop.
overlapping rectangles solution
package main
import (
"fmt"
)
type Rectangle struct {
startingX int
startingY int
width int
height int
}
func (r Rectangle) Points() (out [][]int) {
for w := r.startingX; w <= r.startingX+r.width; w++ {
couple := []int{}
for h := r.startingY; h >= r.startingY-r.height; h-- {
couple = append(couple, w)
couple = append(couple, h)
out = append(out, couple)
couple = []int{}
}
}
return
}
func CommonPoints(a, b Rectangle) (out [][]int) {
for _, p := range a.Points() {
for _, c := range b.Points() {
if p[0] == c[0] && p[1] == c[1] {
out = append(out, p)
}
}
}
return
}
func main() {
A := Rectangle{
startingX: 1,
startingY: 4,
width: 3,
height: 3,
}
B := Rectangle{
startingX: 0,
startingY: 5,
width: 4,
height: 3,
}
fmt.Println("Points of A: ", A.Points())
fmt.Println("Points of B: ", B.Points())
internal := CommonPoints(A, B)
fmt.Println("Common points: ", internal)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment