From e890ca4c4d130ca38992fe598da0f09a2483f756 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Mon, 8 Jan 2024 20:03:22 -0330 Subject: refactor ui.updateCompImg() --- ui.go | 58 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/ui.go b/ui.go index c116b8e..e7b43be 100644 --- a/ui.go +++ b/ui.go @@ -298,40 +298,18 @@ func selectCompressor() g.Widget { var updatedCompImg = make(chan image.Image) func updateCompImg() { - // Copy compressorImage - b := compressorImage.Bounds() - m := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy())) - draw.Draw(m, m.Bounds(), compressorImage, b.Min, draw.Src) - + img := copyImage(compressorImage) for i := 0; i < numPoints; i++ { - min := selectedCompressor.MinX - max := selectedCompressor.MaxX - - unit := mass.KilogramsPerSecond - mfr := engineMassFlowRate[i] / unit - maxMfr := selectedCompressor.MaxFlow / unit - - x := min + int(float32(max-min)*float32(mfr/maxMfr)) - - min = selectedCompressor.MinY - max = selectedCompressor.MaxY - - pr := pressureRatio[i] - maxPr := selectedCompressor.MaxPR - - y := min - int(float32((min-max))*((pr-1.0)/(maxPr-1.0))) - - ps := m.Bounds().Dx() / 100 // Point size - - draw.Draw(m, - image.Rect(x-ps/2, y-ps/2, x+ps/2, y+ps/2), + pos := pointPos(i) + ps := img.Bounds().Dx() / 100 // Point size + draw.Draw(img, + image.Rect(pos.X-ps/2, pos.Y-ps/2, pos.X+ps/2, pos.Y+ps/2), &image.Uniform{red()}, image.ZP, draw.Src, ) } - - updatedCompImg <- m + updatedCompImg <- img } func compressorWidget() { @@ -370,3 +348,27 @@ func compressorWidget() { ) } } + +func copyImage(old *image.RGBA) *image.RGBA { + b := old.Bounds() + img := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy())) + draw.Draw(img, img.Bounds(), old, b.Min, draw.Src) + return img +} + +// The position on the compressor map of an operating point. +func pointPos(i int) (pos image.Point) { + const unit = mass.KilogramsPerSecond + mfr := engineMassFlowRate[i] / unit + maxMfr := selectedCompressor.MaxFlow / unit + min := selectedCompressor.MinX + max := selectedCompressor.MaxX + pos.X = min + int(float32(max-min)*float32(mfr/maxMfr)) + + min = selectedCompressor.MinY + max = selectedCompressor.MaxY + pr := pressureRatio[i] + maxPr := selectedCompressor.MaxPR + pos.Y = min - int(float32((min-max))*((pr-1.0)/(maxPr-1.0))) + return pos +} -- cgit v1.2.3