Skip to content

Commit

Permalink
First test at implementing chords
Browse files Browse the repository at this point in the history
  • Loading branch information
hexylena committed May 14, 2017
1 parent 80f39ce commit a49433d
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func init() {
flag.IntVar(&Alpha, "a", 128, "alpha value")
flag.IntVar(&InputSize, "r", 256, "resize large input images to this size")
flag.IntVar(&OutputSize, "s", 1024, "output image size")
flag.IntVar(&Mode, "m", 1, "0=combo 1=triangle 2=rect 3=ellipse 4=circle 5=rotatedrect 6=beziers 7=rotatedellipse 8=polygon")
flag.IntVar(&Mode, "m", 1, "0=combo 1=triangle 2=rect 3=ellipse 4=circle 5=rotatedrect 6=beziers 7=rotatedellipse 8=polygon 9=chord")
flag.IntVar(&Workers, "j", 0, "number of parallel workers (default uses all cores)")
flag.IntVar(&Nth, "nth", 1, "save every Nth frame (put \"%d\" in path)")
flag.IntVar(&Repeat, "rep", 0, "add N extra shapes per iteration with reduced search")
Expand Down
102 changes: 102 additions & 0 deletions primitive/chord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package primitive

import (
"fmt"
"strings"
"math"

"github.com/fogleman/gg"
"github.com/golang/freetype/raster"
)

type Chord struct {
Worker *Worker
A1, A2 float64
//X1, Y1 float64
//X2, Y2 float64
}

func NewRandomChord(worker *Worker) *Chord {
rnd := worker.Rnd

// These will be on (-1, 1)
a1 := rnd.Float64() * float64(360)
a2 := rnd.Float64() * float64(360)

q := &Chord{worker, a1, a2}
q.Mutate()
return q
}

func (q *Chord) GetPositions() (float64, float64, float64, float64) {
// So we need to make them on a unit circle centered around worker.W / 2, worker.H / 2
// Of width worker.W / 2 and height worker.H / 2
half_width := float64(q.Worker.W) / 2
half_height:= float64(q.Worker.H) / 2

x1 := math.Cos(q.A1)
y1 := math.Sin(q.A1)
x2 := math.Cos(q.A2)
y2 := math.Sin(q.A2)

x1 = (x1 * half_width) + half_width
y1 = (y1 * half_height) + half_height
x2 = (x2 * half_width) + half_width
y2 = (y2 * half_height) + half_height
return x1, y1, x2, y2
}

func (q *Chord) Draw(dc *gg.Context, scale float64) {
x1, y1, x2, y2 := q.GetPositions()

dc.MoveTo(x1, y1)
dc.MoveTo(x2, y2)
dc.SetLineWidth(1)
dc.Stroke()
}

func (q *Chord) SVG(attrs string) string {
// TODO: this is a little silly
x1, y1, x2, y2 := q.GetPositions()
attrs = strings.Replace(attrs, "fill", "stroke", -1)
return fmt.Sprintf(
"<line %s x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" stroke-width=\"1\" />",
attrs, x1, y1, x2, y2)
}

func (q *Chord) Copy() Shape {
a := *q
return &a
}

func (q *Chord) Mutate() {
const m = 16
rnd := q.Worker.Rnd
for {
switch rnd.Intn(2) {
case 0:
q.A1 = clamp(q.A1 + rnd.NormFloat64() * m, 0, 360)
case 1:
q.A2 = clamp(q.A2 + rnd.NormFloat64() * m, 0, 360)
}
if q.Valid() {
break
}
}
}

func (q *Chord) Valid() bool {
return true;
}

func (q *Chord) Rasterize() []Scanline {
x1, y1, x2, y2 := q.GetPositions()

var path raster.Path
p1 := fixp(x1, y1)
p2 := fixp(x2, y2)
path.Start(p1)
path.Add1(p2)
width := fix(1)
return strokePath(q.Worker, path, width, raster.RoundCapper, raster.RoundJoiner)
}
1 change: 1 addition & 0 deletions primitive/shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ const (
ShapeTypeQuadratic
ShapeTypeRotatedEllipse
ShapeTypePolygon
ShapeTypeChord
)
2 changes: 2 additions & 0 deletions primitive/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,7 @@ func (worker *Worker) RandomState(t ShapeType, a int) *State {
return NewState(worker, NewRandomRotatedEllipse(worker), a)
case ShapeTypePolygon:
return NewState(worker, NewRandomPolygon(worker, 4, false), a)
case ShapeTypeChord:
return NewState(worker, NewRandomChord(worker), a)
}
}

0 comments on commit a49433d

Please sign in to comment.