Skip to content

Commit

Permalink
Added RadialWarp
Browse files Browse the repository at this point in the history
  • Loading branch information
jphsd committed Apr 27, 2023
1 parent 95574d1 commit 93780dc
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions warp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package texture

import "image/color"
import (
"image/color"
"math"
)

// Warp applies a deformation to the values passed into the Eval2 function.
type Warp struct {
Expand Down Expand Up @@ -30,7 +33,7 @@ func NewWarpVF(src VectorField, wf func(float64, float64) (float64, float64)) *W
return &WarpVF{"WarpVF", src, wf}
}

// Eval2 implements the Field interface.
// Eval2 implements the VectorField interface.
func (w *WarpVF) Eval2(x, y float64) []float64 {
x, y = w.Func(x, y)
return w.Src.Eval2(x, y)
Expand All @@ -47,8 +50,24 @@ func NewWarpCF(src ColorField, wf func(float64, float64) (float64, float64)) *Wa
return &WarpCF{"WarpCF", src, wf}
}

// Eval2 implements the Field interface.
// Eval2 implements the ColorField interface.
func (w *WarpCF) Eval2(x, y float64) color.Color {
x, y = w.Func(x, y)
return w.Src.Eval2(x, y)
}

// RadialWarp performs a scaled warp around Center for use in the above warp types.
type RadialWarp struct {
Center []float64 // Center of warp
RScale float64 // Radial scale
CScale float64 // Circumference scale
}

// Eval converts from Euclidean to radial coords.
func (rw RadialWarp) Eval(x, y float64) (float64, float64) {
lx, ly := x-rw.Center[0], y-rw.Center[1]
rr := math.Hypot(lx, ly) * rw.RScale
rx := rr*math.Atan2(ly, lx)*rw.CScale + rw.Center[0]
ry := rr + rw.Center[1]
return rx, ry
}

0 comments on commit 93780dc

Please sign in to comment.