|
@@ -1,6 +1,9 @@
|
|
|
package eventServer
|
|
|
|
|
|
-import "sync"
|
|
|
+import (
|
|
|
+ "math"
|
|
|
+ "sync"
|
|
|
+)
|
|
|
|
|
|
var LhChan = make(chan [2]float64, 20) //传长,高
|
|
|
var WChan = make(chan float64, 20) //传宽
|
|
@@ -21,3 +24,39 @@ func CalcCap() (l, w, h float64) {
|
|
|
wg.Wait()
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+type Constant struct {
|
|
|
+ Length float64
|
|
|
+ Width float64
|
|
|
+ Height float64
|
|
|
+ Volume float64
|
|
|
+}
|
|
|
+
|
|
|
+func (c Constant) Calc() float64 {
|
|
|
+ return c.Length * c.Width * c.Height
|
|
|
+}
|
|
|
+
|
|
|
+func FindBestMatch(constants []Constant, variable Constant) Constant {
|
|
|
+ bestMatch := constants[0]
|
|
|
+ bestMatchScore := CalculateMatchScore(bestMatch, variable)
|
|
|
+ for i := 1; i < len(constants); i++ {
|
|
|
+ score := CalculateMatchScore(constants[i], variable)
|
|
|
+ if score < bestMatchScore ||
|
|
|
+ (score == bestMatchScore && constants[i].Length > bestMatch.Length) ||
|
|
|
+ (score == bestMatchScore && constants[i].Length == bestMatch.Length && constants[i].Height > bestMatch.Height) ||
|
|
|
+ (score == bestMatchScore && constants[i].Length == bestMatch.Length && constants[i].Height == bestMatch.Height && constants[i].Width > bestMatch.Width) {
|
|
|
+ bestMatch = constants[i]
|
|
|
+ bestMatchScore = score
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return bestMatch
|
|
|
+}
|
|
|
+
|
|
|
+func CalculateMatchScore(constant, variable Constant) float64 {
|
|
|
+ //lengthScore := math.Abs(float64(constant.Length - variable.Length))
|
|
|
+ //widthScore := math.Abs(float64(constant.Width - variable.Width))
|
|
|
+ //heightScore := math.Abs(float64(constant.Height - variable.Height))
|
|
|
+
|
|
|
+ return math.Abs(variable.Calc() - constant.Calc())
|
|
|
+}
|