Skip to content

Commit 17af81e

Browse files
committed
Added reflection
1 parent c692e1a commit 17af81e

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
go.sum
22
*.ngo
3+
*.nmod
34
*.jpeg
45
*.jpg
56

random.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func GetNodes() []Node {
4646
{"Combiner3", MakeCombiner3},
4747
{"Displace", MakeDisplace},
4848
{"VectorCombine", MakeVectorCombine},
49+
{"Reflect", MakeReflect},
4950
}
5051
}
5152
return NodeOptions
@@ -361,6 +362,14 @@ func MakeVectorCombine(md, d int) Field {
361362
return &VectorCombine{MakeVectorField(md, d+1), MakeCombiner3Func(), MakeFilter()}
362363
}
363364

365+
// MakeReflect creates a mirror plane in the field.
366+
func MakeReflect(md, d int) Field {
367+
h, w := 600.0, 600.0
368+
lp1 := []float64{rand.Float64() * w, rand.Float64() * h}
369+
lp2 := []float64{rand.Float64() * w, rand.Float64() * h}
370+
return NewReflect(MakeField(md, d+1), lp1, lp2)
371+
}
372+
364373
// MakeNormal creates a new vector field from a field.
365374
func MakeNormal(md, d int) VectorField {
366375
return NewNormal(MakeField(md, d), 10, 10, 2, 2)

reflect.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)