-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathline.view.ts
81 lines (58 loc) · 1.83 KB
/
line.view.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace $.$$ {
export class $mol_plot_line extends $.$mol_plot_line {
sub() {
return this.hint() ? super.sub() : []
}
@ $mol_mem
indexes() {
const threshold = this.threshold()
const {
x: {min: viewport_left, max: viewport_right},
y: {min: viewport_bottom, max: viewport_top},
} = this.viewport()
const [shift_x, shift_y] = this.shift()
const [scale_x, scale_y] = this.scale()
const indexes = [] as number[]
let last = new $mol_vector_2d( Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY )
let last_zone = new $mol_vector_2d( 0, 0 )
const series_x = this.series_x()
const series_y = this.series_y()
const zone_of = ( point: $mol_vector_2d<number> )=> new $mol_vector_2d(
point.x < viewport_left ? -1
: point.x > viewport_right ? 1
: 0,
point.y < viewport_bottom ? -1
: point.y > viewport_top ? 1
: 0,
)
for (let i = 0; i < series_x.length-1; i++) {
const scaled = new $mol_vector_2d(
Math.round( shift_x + this.repos_x( series_x[i] ) * scale_x ),
Math.round( shift_y + this.repos_y( series_y[i] ) * scale_y ),
)
if (
Math.abs( scaled.x - last.x ) < threshold
&& Math.abs( scaled.y - last.y ) < threshold
) continue
const zone = zone_of( scaled )
last = scaled
if( zone.x !== 0 && zone.x === last_zone.x || zone.y !== 0 && zone.y === last_zone.y ) {
continue
}
if( last_zone.x !== 0 || last_zone.y !== 0 ) {
indexes.push( i - 1 )
}
last_zone = zone
indexes.push(i)
}
indexes.push( series_x.length - 1 )
return indexes
}
curve() {
const points = this.points()
if( points.length === 0 ) return ''
const main = points.map( point => `L ${point.join(' ')}`).join(' ')
return `M ${points[0].join(' ')} ${main}`
}
}
}