capacity.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package eventServer
  2. import (
  3. "math"
  4. "sync"
  5. )
  6. var LhChan = make(chan [2]float64, 20) //传长,高
  7. var WChan = make(chan float64, 20) //传宽
  8. func CalcCap() (l, w, h float64) {
  9. var wg sync.WaitGroup
  10. wg.Add(2)
  11. go func() {
  12. lh := <-LhChan
  13. l = lh[0]
  14. h = lh[1]
  15. wg.Done()
  16. }()
  17. go func() {
  18. w = <-WChan
  19. wg.Done()
  20. }()
  21. wg.Wait()
  22. return
  23. }
  24. type Constant struct {
  25. Length float64
  26. Width float64
  27. Height float64
  28. Volume float64
  29. }
  30. func (c Constant) Calc() float64 {
  31. return c.Length * c.Width * c.Height
  32. }
  33. func FindBestMatch(constants []Constant, variable Constant) Constant {
  34. bestMatch := constants[0]
  35. bestMatchScore := CalculateMatchScore(bestMatch, variable)
  36. for i := 1; i < len(constants); i++ {
  37. score := CalculateMatchScore(constants[i], variable)
  38. if score < bestMatchScore ||
  39. (score == bestMatchScore && constants[i].Length > bestMatch.Length) ||
  40. (score == bestMatchScore && constants[i].Length == bestMatch.Length && constants[i].Height > bestMatch.Height) ||
  41. (score == bestMatchScore && constants[i].Length == bestMatch.Length && constants[i].Height == bestMatch.Height && constants[i].Width > bestMatch.Width) {
  42. bestMatch = constants[i]
  43. bestMatchScore = score
  44. }
  45. }
  46. return bestMatch
  47. }
  48. func CalculateMatchScore(constant, variable Constant) float64 {
  49. //lengthScore := math.Abs(float64(constant.Length - variable.Length))
  50. //widthScore := math.Abs(float64(constant.Width - variable.Width))
  51. //heightScore := math.Abs(float64(constant.Height - variable.Height))
  52. return math.Abs(variable.Calc() - constant.Calc())
  53. }