Skip to content

Commit

Permalink
Use functional options instead of params
Browse files Browse the repository at this point in the history
  • Loading branch information
polldo committed Aug 7, 2022
1 parent a4ebc90 commit 748654d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 29 deletions.
14 changes: 7 additions & 7 deletions grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ type Grid struct {
Rows, Cols int
}

func NewGrid(cols, rows int, distance float64, params *VerletParams) *Grid {
func NewGrid(cols, rows int, distance float64, opts ...Opt) *Grid {
g := &Grid{
Verlet: New(params),
Verlet: New(opts...),
Rows: rows,
Cols: cols,
}

rad := 2.
x := params.Bound.X / 2
y := params.Bound.Y / 2
x := g.Verlet.Bound.X / 2
y := g.Verlet.Bound.Y / 2
for i := 0; i < cols; i++ {

if i == 0 {
g.Origin = g.NewPoint(x, y, rad, true)
g.Origin = g.NewPoint(x, y, Radius(rad), Fix())
} else {
g.NewPoint(x+float64(i)*distance, y, rad, false)
g.NewPoint(x+float64(i)*distance, y, Radius(rad))
g.NewLine(g.Extract(i, 0), g.Extract(i-1, 0))
}

for j := 1; j < rows; j++ {
g.NewPoint(x+float64(i)*distance, y-float64(j)*distance, rad, false)
g.NewPoint(x+float64(i)*distance, y-float64(j)*distance, Radius(rad))
g.NewLine(g.Extract(i, j), g.Extract(i, j-1))
if i > 0 {
g.NewLine(g.Extract(i, j), g.Extract(i-1, j))
Expand Down
20 changes: 20 additions & 0 deletions point.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ import (
"math"
)

type PointOpt func(*Point)

func (p *Point) SetOptions(opts ...PointOpt) {
for _, opt := range opts {
opt(p)
}
}

func Fix() PointOpt {
return func(p *Point) {
p.Fixed = true
}
}

func Radius(r float64) PointOpt {
return func(p *Point) {
p.Radius = r
}
}

type Point struct {
Fixed bool
Radius float64
Expand Down
13 changes: 6 additions & 7 deletions rope.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ type Rope struct {
Head *Point
}

func NewRope(units int, distance float64, params *VerletParams) *Rope {
func NewRope(units int, distance float64, opts ...Opt) *Rope {
r := &Rope{
Verlet: New(params),
Verlet: New(opts...),
}

rad := 8.
x := params.Bound.X / 2
y := params.Bound.Y / 2
r.Head = r.NewPoint(x, y, rad, true)
x := r.Verlet.Bound.X / 2
y := r.Verlet.Bound.Y / 2
r.Head = r.NewPoint(x, y, Fix(), Radius(8.0))

for i := 1; i < units; i++ {
r.NewPoint(x, y-float64(i)*distance, rad, false)
r.NewPoint(x, y-float64(i)*distance)
r.NewLine(r.Points[i], r.Points[i-1])
}

Expand Down
4 changes: 4 additions & 0 deletions vector.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package verlet

// Vector is a simple struct representing a 2d math vector.
type Vector struct {
X, Y float64
}

// Add sum 'other' to 'v' and return the result in a new vector.
func (v Vector) Add(other Vector) Vector {
return Vector{
X: v.X + other.X,
Y: v.Y + other.Y,
}
}

// Sub subtracts 'other' from 'v' and return the result in a new vector.
func (v Vector) Sub(other Vector) Vector {
return Vector{
X: v.X - other.X,
Y: v.Y - other.Y,
}
}

// Scale multiplies 'v' by a scalar and returns the result in a new vector.
func (v Vector) Scale(scalar float64) Vector {
return Vector{
X: v.X * scalar,
Expand Down
57 changes: 42 additions & 15 deletions verlet.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,70 @@
package verlet

type VerletParams struct {
type Opt func(*Verlet)

// Gravity sets the gravity option for verlet.
func Gravity(x, y float64) Opt {
return func(v *Verlet) {
v.Gravity = Vector{X: x, Y: y}
}
}

// Bound sets the bounds option for verlet.
func Bound(x, y float64) Opt {
return func(v *Verlet) {
v.Bound = Vector{X: x, Y: y}
}
}

// Friction sets the friction option for verlet.
func Friction(f float64) Opt {
return func(v *Verlet) {
v.Friction = f
}
}

type Verlet struct {
Points []*Point
Lines []*Line
Gravity Vector
Bound Vector
Friction float64
}

type Verlet struct {
VerletParams
Points []*Point
Lines []*Line
func New(opts ...Opt) *Verlet {
v := &Verlet{
Gravity: Vector{X: 0.1, Y: -0.1},
Bound: Vector{X: 100, Y: 100},
Friction: 0,
}
v.SetOptions(opts...)
return v
}

func New(params *VerletParams) *Verlet {
return &Verlet{
VerletParams: *params,
func (v *Verlet) SetOptions(opts ...Opt) {
for _, opt := range opts {
opt(v)
}
}

func (v *Verlet) NewPoint(x, y float64, radius float64, fixed bool) *Point {
func (v *Verlet) NewPoint(x, y float64, opts ...PointOpt) *Point {
p := &Point{
Fixed: fixed,
Radius: radius,
Fixed: false,
Radius: 5.0,
Position: Vector{X: x, Y: y},
OldPosition: Vector{X: x, Y: y},
}

p.SetOptions(opts...)
v.Points = append(v.Points, p)
return p
}

func (v *Verlet) NewLine(a, b *Point) *Line {
l := a.Distance(b)
ln := &Line{
A: a,
B: b,
Len: l,
Len: a.Distance(b),
}

v.Lines = append(v.Lines, ln)
return ln
}
Expand Down

0 comments on commit 748654d

Please sign in to comment.