| 1 |
package chart |
| 2 |
|
| 3 |
|
| 4 |
func LinearRange(start, end float64) []float64 { |
| 5 |
return Seq{NewLinearSequence().WithStart(start).WithEnd(end).WithStep(1.0)}.Values() |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
func LinearRangeWithStep(start, end, step float64) []float64 { |
| 10 |
return Seq{NewLinearSequence().WithStart(start).WithEnd(end).WithStep(step)}.Values() |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
func NewLinearSequence() *LinearSeq { |
| 15 |
return &LinearSeq{step: 1.0} |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
type LinearSeq struct { |
| 20 |
start float64 |
| 21 |
end float64 |
| 22 |
step float64 |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
func (lg LinearSeq) Start() float64 { |
| 27 |
return lg.start |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
func (lg LinearSeq) End() float64 { |
| 32 |
return lg.end |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
func (lg LinearSeq) Step() float64 { |
| 37 |
return lg.step |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
func (lg LinearSeq) Len() int { |
| 42 |
if lg.start < lg.end { |
| 43 |
return int((lg.end-lg.start)/lg.step) + 1 |
| 44 |
} |
| 45 |
return int((lg.start-lg.end)/lg.step) + 1 |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
func (lg LinearSeq) GetValue(index int) float64 { |
| 50 |
fi := float64(index) |
| 51 |
if lg.start < lg.end { |
| 52 |
return lg.start + (fi * lg.step) |
| 53 |
} |
| 54 |
return lg.start - (fi * lg.step) |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
func (lg *LinearSeq) WithStart(start float64) *LinearSeq { |
| 59 |
lg.start = start |
| 60 |
return lg |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
func (lg *LinearSeq) WithEnd(end float64) *LinearSeq { |
| 65 |
lg.end = end |
| 66 |
return lg |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func (lg *LinearSeq) WithStep(step float64) *LinearSeq { |
| 71 |
lg.step = step |
| 72 |
return lg |
| 73 |
} |