Files
openchamber-android-native/third_party/mygo/ui/chart/downsample.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

77 lines
2.2 KiB
Go

package chart
import (
"math"
"sort"
)
// MinMax reduces a line of many points to the lowest and highest of each
// pixel column of the plot, in their order, and the ends of each run of
// defined points, so that a line through them looks the same and narrow
// spikes still show. xs and ys are fractions, as for Line; xs must not
// decrease, or the columns come out wrong, and a NaN or infinite value
// breaks the line. Points left or right of the plot go, but for the
// nearest one on each side. pixelWidth is the plot's width in pixels,
// ceil(r.W * p.Scale()).
//
// MinMax appends to dstX[:0] and dstY[:0] and returns them, so that a
// view keeping them reduces every frame without allocating; they must not
// share memory with xs or ys. Keep the points themselves for selections
// and readings.
func MinMax(dstX, dstY, xs, ys []float32, pixelWidth int) ([]float32, []float32) {
dstX, dstY = dstX[:0], dstY[:0]
if pixelWidth <= 0 {
return dstX, dstY
}
n := min(len(xs), len(ys))
finite := func(v float32) bool { return !math.IsNaN(float64(v)) && !math.IsInf(float64(v), 0) }
appendPoint := func(i int) { dstX = append(dstX, xs[i]); dstY = append(dstY, ys[i]) }
runs(n, func(i int) bool { return finite(xs[i]) && finite(ys[i]) }, func(from, to int) {
// Binary search leaves at most one point beyond either visible edge.
lo := from + sort.Search(to-from, func(i int) bool { return xs[from+i] >= 0 })
if lo == to {
return
}
hi := from + sort.Search(to-from, func(i int) bool { return xs[from+i] > 1 })
if lo == from && lo == hi {
return
}
if lo > from {
lo--
}
if hi < to {
hi++
}
if len(dstX) > 0 {
dstX = append(dstX, float32(math.NaN()))
dstY = append(dstY, float32(math.NaN()))
}
last := -1
emit := func(i int) {
if i != last {
appendPoint(i)
last = i
}
}
emit(lo)
for i := lo; i < hi; {
bucket := int(math.Floor(float64(xs[i]) * float64(pixelWidth)))
low, high := i, i
i++
for i < hi && xs[i] >= 0 && xs[i] <= 1 && bucket == int(math.Floor(float64(xs[i])*float64(pixelWidth))) {
if ys[i] < ys[low] {
low = i
}
if ys[i] > ys[high] {
high = i
}
i++
}
emit(min(low, high))
emit(max(low, high))
}
emit(hi - 1)
})
return dstX, dstY
}