capacity.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 FindBestMatch(constants []Constant, variable Constant) Constant {
  31. bestMatch := constants[0]
  32. bestMatchScore := CalculateMatchScore(bestMatch, variable)
  33. for i := 1; i < len(constants); i++ {
  34. score := CalculateMatchScore(constants[i], variable)
  35. if score < bestMatchScore ||
  36. (score == bestMatchScore && constants[i].Length > bestMatch.Length) ||
  37. (score == bestMatchScore && constants[i].Length == bestMatch.Length && constants[i].Height > bestMatch.Height) ||
  38. (score == bestMatchScore && constants[i].Length == bestMatch.Length && constants[i].Height == bestMatch.Height && constants[i].Width > bestMatch.Width) {
  39. bestMatch = constants[i]
  40. bestMatchScore = score
  41. }
  42. }
  43. return bestMatch
  44. }
  45. func (c Constant) Calc() float64 {
  46. return c.Length * c.Width * c.Height
  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(constant.Calc() - variable.Calc())
  53. }