|
| 1 | +package texture |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/jphsd/graphics2d" |
| 5 | + "github.com/jphsd/graphics2d/util" |
| 6 | + "image/color" |
| 7 | +) |
| 8 | + |
| 9 | +// Reflect contains a line along which a reflection is performed. The line defines where the |
| 10 | +// mirror is. Points on the + side of the line remain untransformed, points on the other are |
| 11 | +// reflected through the transformation. |
| 12 | +type Reflect struct { |
| 13 | + Src Field |
| 14 | + Start []float64 |
| 15 | + End []float64 |
| 16 | + Xfm *graphics2d.Aff3 |
| 17 | +} |
| 18 | + |
| 19 | +// NewReflect creates a new Reflection placing the mirror along lp1, lp2. |
| 20 | +func NewReflect(src Field, lp1, lp2 []float64) *Reflect { |
| 21 | + xfm := graphics2d.NewAff3() |
| 22 | + xfm.Reflect(lp1[0], lp1[1], lp2[0], lp2[1]) |
| 23 | + return &Reflect{src, lp1, lp2, xfm} |
| 24 | +} |
| 25 | + |
| 26 | +// Eval2 implements the Field interface. |
| 27 | +func (r *Reflect) Eval2(x, y float64) float64 { |
| 28 | + pt := []float64{x, y} |
| 29 | + if util.SideOfLine(r.Start, r.End, pt) < 0 { |
| 30 | + pt = r.Xfm.Apply(pt)[0] |
| 31 | + } |
| 32 | + return r.Src.Eval2(pt[0], pt[1]) |
| 33 | +} |
| 34 | + |
| 35 | +// ReflectVF contains a line along which a reflection is performed. The line defines where the |
| 36 | +// mirror is. Points on the + side of the line remain untransformed, points on the other are |
| 37 | +// reflected through the transformation. |
| 38 | +type ReflectVF struct { |
| 39 | + Src VectorField |
| 40 | + Start []float64 |
| 41 | + End []float64 |
| 42 | + Xfm *graphics2d.Aff3 |
| 43 | +} |
| 44 | + |
| 45 | +// NewReflect creates a new Reflection placing the mirror along lp1, lp2. |
| 46 | +func NewReflectVF(src VectorField, lp1, lp2 []float64) *ReflectVF { |
| 47 | + xfm := graphics2d.NewAff3() |
| 48 | + xfm.Reflect(lp1[0], lp1[1], lp2[0], lp2[1]) |
| 49 | + return &ReflectVF{src, lp1, lp2, xfm} |
| 50 | +} |
| 51 | + |
| 52 | +// Eval2 implements the Field interface. |
| 53 | +func (r *ReflectVF) Eval2(x, y float64) []float64 { |
| 54 | + pt := []float64{x, y} |
| 55 | + if util.SideOfLine(r.Start, r.End, pt) < 0 { |
| 56 | + pt = r.Xfm.Apply(pt)[0] |
| 57 | + } |
| 58 | + return r.Src.Eval2(pt[0], pt[1]) |
| 59 | +} |
| 60 | + |
| 61 | +// ReflectCF contains a line along which a reflection is performed. The line defines where the |
| 62 | +// mirror is. Points on the + side of the line remain untransformed, points on the other are |
| 63 | +// reflected through the transformation. |
| 64 | +type ReflectCF struct { |
| 65 | + Src ColorField |
| 66 | + Start []float64 |
| 67 | + End []float64 |
| 68 | + Xfm *graphics2d.Aff3 |
| 69 | +} |
| 70 | + |
| 71 | +// NewReflect creates a new Reflection placing the mirror along lp1, lp2. |
| 72 | +func NewReflectCF(src ColorField, lp1, lp2 []float64) *ReflectCF { |
| 73 | + xfm := graphics2d.NewAff3() |
| 74 | + xfm.Reflect(lp1[0], lp1[1], lp2[0], lp2[1]) |
| 75 | + return &ReflectCF{src, lp1, lp2, xfm} |
| 76 | +} |
| 77 | + |
| 78 | +// Eval2 implements the Field interface. |
| 79 | +func (r *ReflectCF) Eval2(x, y float64) color.Color { |
| 80 | + pt := []float64{x, y} |
| 81 | + if util.SideOfLine(r.Start, r.End, pt) < 0 { |
| 82 | + pt = r.Xfm.Apply(pt)[0] |
| 83 | + } |
| 84 | + return r.Src.Eval2(pt[0], pt[1]) |
| 85 | +} |
0 commit comments