update example, add 'Large' option to marker

This commit is contained in:
Dawid Wysokiński 2020-07-31 21:30:13 +02:00
parent 5c03ba1d81
commit 79fc85bed9
6 changed files with 72 additions and 11 deletions

BIN
example/image.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 621 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

View File

@ -2,22 +2,58 @@ package main
import (
"log"
"math/rand"
"os"
"time"
"github.com/tribalwarshelp/map-generator/generator"
"github.com/tribalwarshelp/shared/models"
)
func main() {
villages := generateVillages(10)
// villages2 := generateVillages(1)
t1 := time.Now()
f, _ := os.Create("image.jpg")
f, _ := os.Create("image.jpeg")
defer f.Close()
generator.Generate(generator.Config{
err := generator.Generate(generator.Config{
Destination: f,
Scale: 10,
Scale: 1,
ContinentGrid: true,
ContinentNumbers: true,
Markers: []*generator.Marker{
&generator.Marker{
Color: "#fff",
Villages: villages,
Larger: false,
},
&generator.Marker{
Color: "#ff0",
Villages: []*models.Village{
&models.Village{
X: 500,
Y: 500,
},
},
Larger: true,
},
},
})
log.Print(time.Now().Sub(t1).String())
log.Println(time.Now().Sub(t1).String(), err)
}
func generateVillages(ch int) []*models.Village {
villages := []*models.Village{}
for y := 0; y <= 1000; y++ {
for x := 0; x <= 1000; x++ {
if rand.Intn(100) <= ch {
villages = append(villages, &models.Village{
X: x,
Y: y,
})
}
}
}
return villages
}

View File

@ -8,6 +8,7 @@ import (
"io"
"github.com/disintegration/imaging"
"golang.org/x/sync/errgroup"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/models"
@ -21,6 +22,7 @@ const (
)
type Marker struct {
Larger bool
Villages []*models.Village `json:"villages" gqlgen:"villages" xml:"villages"`
Color string `json:"color" gqlgen:"color" xml:"color"`
}
@ -74,6 +76,7 @@ func Generate(cfg Config) error {
mapSizeDividedBy10 := cfg.MapSize / 10
imgHalfWidth := cfg.MapSize / 2
imgHalfHeight := imgHalfWidth
g := new(errgroup.Group)
backgroundColor, err := parseHexColorFast(cfg.BackgroundColor)
if err != nil {
@ -89,13 +92,32 @@ func Generate(cfg Config) error {
// Markers
for _, marker := range cfg.Markers {
parsedColor, err := parseHexColorFast(marker.Color)
if err != nil {
return err
}
for _, village := range marker.Villages {
img.Set(village.X, village.Y, parsedColor)
}
m := marker
g.Go(func() error {
parsedColor, err := parseHexColorFast(m.Color)
if err != nil {
return err
}
for _, village := range m.Villages {
if m.Larger {
for y := 1; y <= 4; y++ {
for x := 1; x <= 4; x++ {
img.Set(village.X+x, village.Y-y, parsedColor)
img.Set(village.X-x, village.Y-y, parsedColor)
img.Set(village.X+x, village.Y+y, parsedColor)
img.Set(village.X-x, village.Y+y, parsedColor)
}
}
} else {
img.Set(village.X, village.Y, parsedColor)
}
}
return nil
})
}
if err := g.Wait(); err != nil {
return err
}
//Continents

1
go.mod
View File

@ -7,4 +7,5 @@ require (
github.com/pkg/errors v0.9.1
github.com/tribalwarshelp/shared v0.0.0-20200622084436-3a768c8bf574
golang.org/x/image v0.0.0-20200618115811-c13761719519
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
)

2
go.sum
View File

@ -7,4 +7,6 @@ github.com/tribalwarshelp/shared v0.0.0-20200622084436-3a768c8bf574/go.mod h1:tf
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20200618115811-c13761719519 h1:1e2ufUJNM3lCHEY5jIgac/7UTjd6cgJNdatjPdFWf34=
golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=