2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -7,28 +7,40 @@ import (
"time"
)
type pt struct {
x, y int
}
const (
nPts = 100
rMin = 10
rMax = 15
)
func main() {
rand.Seed(time.Now().UnixNano())
// generate random points, accumulate 100 distinct points meeting condition
m := make(map[int]pt) // key is buffer index
for len(m) < 100 {
p := pt{rand.Intn(31) - 15, rand.Intn(31) - 15}
rs := p.x*p.x + p.y*p.y
if 100 <= rs && rs <= 225 {
m[(p.x+15)*2+(p.y+15)*31*2] = p
rand.Seed(time.Now().Unix())
span := rMax + 1 + rMax
rows := make([][]byte, span)
for r := range rows {
rows[r] = bytes.Repeat([]byte{' '}, span*2)
}
u := 0 // count unique points
min2 := rMin * rMin
max2 := rMax * rMax
for n := 0; n < nPts; {
x := rand.Intn(span) - rMax
y := rand.Intn(span) - rMax
// x, y is the generated coordinate pair
rs := x*x + y*y
if rs < min2 || rs > max2 {
continue
}
n++ // count pair as meeting condition
r := y + rMax
c := (x + rMax) * 2
if rows[r][c] == ' ' {
rows[r][c] = '*'
u++
}
}
// plot to buffer
b := bytes.Repeat([]byte{' '}, 31*31*2)
for i := range m {
b[i] = '*'
}
// print buffer to screen
for i := 0; i < 31; i++ {
fmt.Println(string(b[i*31*2 : (i+1)*31*2]))
for _, row := range rows {
fmt.Println(string(row))
}
fmt.Println(u, "unique points")
}

View file

@ -4,36 +4,45 @@ import (
"bytes"
"fmt"
"math/rand"
"time"
)
type pt struct {
x, y int
}
const (
nPts = 100
rMin = 10
rMax = 15
)
func main() {
// generate possible points
all := make([]pt, 0, 404)
for x := -15; x <= 15; x++ {
for y := -15; y <= 15; y++ {
rs := x*x + y*y
if 100 <= rs && rs <= 225 {
all = append(all, pt{x, y})
rand.Seed(time.Now().Unix())
var poss []struct{ x, y int }
min2 := rMin * rMin
max2 := rMax * rMax
for y := -rMax; y <= rMax; y++ {
for x := -rMax; x <= rMax; x++ {
if r2 := x*x + y*y; r2 >= min2 && r2 <= max2 {
poss = append(poss, struct{ x, y int }{x, y})
}
}
}
if len(all) != 404 {
panic(len(all))
fmt.Println(len(poss), "possible points")
span := rMax + 1 + rMax
rows := make([][]byte, span)
for r := range rows {
rows[r] = bytes.Repeat([]byte{' '}, span*2)
}
// randomly order
rp := rand.Perm(404)
// plot 100 of them to a buffer
b := bytes.Repeat([]byte{' '}, 31*31*2)
for i := 0; i < 100; i++ {
p := all[rp[i]]
b[(p.x+15)*2+(p.y+15)*31*2] = '*'
u := 0
for n := 0; n < nPts; n++ {
i := rand.Intn(len(poss))
r := poss[i].y + rMax
c := (poss[i].x + rMax) * 2
if rows[r][c] == ' ' {
rows[r][c] = '*'
u++
}
}
// print buffer to screen
for i := 0; i < 31; i++ {
fmt.Println(string(b[i*31*2 : (i+1)*31*2]))
for _, row := range rows {
fmt.Println(string(row))
}
fmt.Println(u, "unique points")
}