Debian Codegraph @

linear_sequence.go

raw symbols SCIP
1 package chart
2
3 // LinearRange returns an array of values representing the range from start to end, incremented by 1.0.
4 func LinearRange(start, end float64) []float64 {
5 return Seq{NewLinearSequence().WithStart(start).WithEnd(end).WithStep(1.0)}.Values()
6 }
7
8 // LinearRangeWithStep returns the array values of a linear seq with a given start, end and optional step.
9 func LinearRangeWithStep(start, end, step float64) []float64 {
10 return Seq{NewLinearSequence().WithStart(start).WithEnd(end).WithStep(step)}.Values()
11 }
12
13 // NewLinearSequence returns a new linear generator.
14 func NewLinearSequence() *LinearSeq {
15 return &LinearSeq{step: 1.0}
16 }
17
18 // LinearSeq is a stepwise generator.
19 type LinearSeq struct {
20 start float64
21 end float64
22 step float64
23 }
24
25 // Start returns the start value.
26 func (lg LinearSeq) Start() float64 {
27 return lg.start
28 }
29
30 // End returns the end value.
31 func (lg LinearSeq) End() float64 {
32 return lg.end
33 }
34
35 // Step returns the step value.
36 func (lg LinearSeq) Step() float64 {
37 return lg.step
38 }
39
40 // Len returns the number of elements in the seq.
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 // GetValue returns the value at a given index.
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 // WithStart sets the start and returns the linear generator.
58 func (lg *LinearSeq) WithStart(start float64) *LinearSeq {
59 lg.start = start
60 return lg
61 }
62
63 // WithEnd sets the end and returns the linear generator.
64 func (lg *LinearSeq) WithEnd(end float64) *LinearSeq {
65 lg.end = end
66 return lg
67 }
68
69 // WithStep sets the step and returns the linear generator.
70 func (lg *LinearSeq) WithStep(step float64) *LinearSeq {
71 lg.step = step
72 return lg
73 }