本文整理汇总了Golang中math.Max函数的典型用法代码示例。如果您正苦于以下问题:Golang Max函数的具体用法?Golang Max怎么用?Golang Max使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Max函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: valueToArrayIndex
func valueToArrayIndex(indexValue Value, length uint, negativeIsZero bool) uint {
index := toIntegerFloat(indexValue)
if !negativeIsZero {
if 0 > length {
return uint(index)
}
if 0 > index {
index = math.Max(index+float64(length), 0)
} else {
index = math.Min(index, float64(length))
}
return uint(index)
}
{
index := uint(math.Max(index, 0))
if 0 > length {
return index
}
// minimum(index, length)
if index > length {
return length
}
return index
}
}
示例2: ExtendPoint
func (b *Bounds) ExtendPoint(point Point) *Bounds {
b.Min.X = math.Min(b.Min.X, point.X)
b.Min.Y = math.Min(b.Min.Y, point.Y)
b.Max.X = math.Max(b.Max.X, point.X)
b.Max.Y = math.Max(b.Max.Y, point.Y)
return b
}
示例3: ExtendPointZM
func (b *Bounds) ExtendPointZM(pointZM PointZM) *Bounds {
b.Min.X = math.Min(b.Min.X, pointZM.X)
b.Min.Y = math.Min(b.Min.Y, pointZM.Y)
b.Max.X = math.Max(b.Max.X, pointZM.X)
b.Max.Y = math.Max(b.Max.Y, pointZM.Y)
return b
}
示例4: Max
func (v3 *Vector3) Max(v *Vector3) *Vector3 {
v3.X = math.Max(v3.X, v.X)
v3.Y = math.Max(v3.Y, v.Y)
v3.Z = math.Max(v3.Z, v.Z)
return v3
}
示例5: AdjustSigmoid
// AdjustSigmoid changes the contrast of the image using a sigmoidal function and returns the adjusted image.
// It's a non-linear contrast change useful for photo adjustments as it preserves highlight and shadow detail.
// The midpoint parameter is the midpoint of contrast that must be between 0 and 1, typically 0.5.
// The factor parameter indicates how much to increase or decrease the contrast, typically in range (-10, 10).
// If the factor parameter is positive the image contrast is increased otherwise the contrast is decreased.
//
// Examples:
//
// dstImage = imaging.AdjustSigmoid(srcImage, 0.5, 3.0) // increase the contrast
// dstImage = imaging.AdjustSigmoid(srcImage, 0.5, -3.0) // decrease the contrast
//
func AdjustSigmoid(img image.Image, midpoint, factor float64) *image.NRGBA {
if factor == 0 {
return Clone(img)
}
lut := make([]uint8, 256)
a := math.Min(math.Max(midpoint, 0.0), 1.0)
b := math.Abs(factor)
sig0 := sigmoid(a, b, 0)
sig1 := sigmoid(a, b, 1)
e := 1.0e-6
if factor > 0 {
for i := 0; i < 256; i++ {
x := float64(i) / 255.0
sigX := sigmoid(a, b, x)
f := (sigX - sig0) / (sig1 - sig0)
lut[i] = clamp(f * 255.0)
}
} else {
for i := 0; i < 256; i++ {
x := float64(i) / 255.0
arg := math.Min(math.Max((sig1-sig0)*x+sig0, e), 1.0-e)
f := a - math.Log(1.0/arg-1.0)/b
lut[i] = clamp(f * 255.0)
}
}
fn := func(c color.NRGBA) color.NRGBA {
return color.NRGBA{lut[c.R], lut[c.G], lut[c.B], c.A}
}
return AdjustFunc(img, fn)
}
示例6: area
// area returns the value of the largest area from 3 possible areas created
// from the 4 given points.
//
// Based on bullet btPersistentManifold::calcArea4Points
func (con *contactPair) area(p0, p1, p2, p3 *lin.V3) float64 {
v0, v1, vx := con.v0, con.v1, con.v2
l0 := vx.Cross(v0.Sub(p0, p1), v1.Sub(p2, p3)).LenSqr()
l1 := vx.Cross(v0.Sub(p0, p2), v1.Sub(p1, p3)).LenSqr()
l2 := vx.Cross(v0.Sub(p0, p3), v1.Sub(p1, p2)).LenSqr()
return math.Max(math.Max(l0, l1), l2)
}
示例7: normalizeSlice
func normalizeSlice(valuesA []float64, valuesB []float64) ([]float64, []float64) {
offsetA := int(math.Max(0, float64(len(valuesA)-len(valuesB))))
offsetB := int(math.Max(0, float64(len(valuesB)-len(valuesB))))
sliceA := valuesA[offsetA:]
sliceB := valuesB[offsetB:]
return sliceA, sliceB
}
示例8: getFoldedSize
func (g *group) getFoldedSize() (w, h float64) {
var inWidth, outWidth, inHeight, outHeight float64
for _, t := range g.inputs {
t.refineSize()
if t.horizontal {
inWidth += t.Width() + GroupIOSpacing
} else {
inHeight += t.Height() + GroupIOSpacing
}
}
for _, t := range g.outputs {
t.refineSize()
if t.horizontal {
outWidth += t.Width() + GroupIOSpacing
} else {
outHeight += t.Height() + GroupIOSpacing
}
}
inWidth -= GroupIOSpacing
inHeight -= GroupIOSpacing
outWidth -= GroupIOSpacing
outHeight -= GroupIOSpacing
w, h = math.Max(math.Max(inWidth, outWidth), GroupMinSize)+GroupMargin*2,
math.Max(math.Max(inHeight, outHeight), GroupMinSize)+GroupMargin*2
return
}
示例9: startingStepSize
// startingStepSize implements the algorithm for estimating the starting step
// size as described in:
// - Hairer, E., Wanner, G., Nørsett, S.: Solving Ordinary Differential
// Equations I: Nonstiff Problems. Springer Berlin Heidelberg (1993)
func startingStepSize(rhs Function, init, tmp *State, weight Weighting, w []float64, order float64, s *Settings) float64 {
// Store 1 / (rtol * |Y_i| + atol) into w.
weight(init.Y, w)
d0 := s.Norm(init.Y, w)
d1 := s.Norm(init.YDot, w)
var h0 float64
if math.Min(d0, d1) < 1e-5 {
h0 = 1e-6
} else {
// Make the increment of an explicit Euler step small compared to the
// size of the initial value.
h0 = 0.01 * d0 / d1
}
// Perform one explicit Euler step.
floats.AddScaledTo(tmp.Y, init.Y, h0, init.YDot)
// Evaluate the right-hand side f(init.Time+h, tmp.Y).
rhs(tmp.YDot, init.Time+h0, tmp.Y)
// Estimate the second derivative of the solution.
floats.Sub(tmp.YDot, init.YDot)
d2 := s.Norm(tmp.YDot, w) / h0
var h1 float64
if math.Max(d1, d2) < 1e-15 {
h1 = math.Max(1e-6, 1e-3*h0)
} else {
h1 = math.Pow(0.01/math.Max(d1, d2), 1/(order+1))
}
return math.Min(100*h0, h1)
}
示例10: reform
func (v *typeView) reform() {
const spacing = 2
maxWidth := float64(0)
h1 := float64(0)
for i, c := range v.elems.left {
h1 += Height(c)
if i > 0 {
h1 += spacing
}
if w := Width(c); w > maxWidth {
maxWidth = w
}
}
h2 := float64(0)
for i, c := range v.elems.right {
h2 += Height(c)
if i > 0 {
h2 += spacing
}
}
if v.unexported != nil {
if h2 > 0 {
h2 += spacing
}
h2 += Height(v.unexported)
}
x := 0.0
if v.name != nil {
v.name.Move(Pt(0, (math.Max(h1, h2)-Height(v.name))/2))
x += Width(v.name) + spacing
}
y := math.Max(0, h2-h1) / 2
for i := len(v.elems.left) - 1; i >= 0; i-- {
c := v.elems.left[i]
c.Move(Pt(x+maxWidth-Width(c), y))
y += Height(c) + spacing
}
x += maxWidth + spacing
if v.pkg != nil {
v.pkg.Move(Pt(x, (math.Max(h1, h2)-Height(v.pkg))/2))
x += Width(v.pkg)
}
v.text.Move(Pt(x, (math.Max(h1, h2)-Height(v.text))/2))
x += Width(v.text) + spacing
y = math.Max(0, h1-h2) / 2
if v.unexported != nil {
v.unexported.Move(Pt(x, y))
y += Height(v.unexported) + spacing
}
for i := len(v.elems.right) - 1; i >= 0; i-- {
c := v.elems.right[i]
c.Move(Pt(x, y))
y += Height(c) + spacing
}
ResizeToFit(v, 2)
if p, ok := Parent(v).(*typeView); ok {
p.reform()
}
}
示例11: extractImage
func extractImage(image *C.struct__VipsImage, o Options) (*C.struct__VipsImage, error) {
var err error = nil
inWidth := int(image.Xsize)
inHeight := int(image.Ysize)
switch {
case o.Crop:
width := int(math.Min(float64(inWidth), float64(o.Width)))
height := int(math.Min(float64(inHeight), float64(o.Height)))
left, top := calculateCrop(inWidth, inHeight, o.Width, o.Height, o.Gravity)
left, top = int(math.Max(float64(left), 0)), int(math.Max(float64(top), 0))
image, err = vipsExtract(image, left, top, width, height)
break
case o.Embed:
left, top := (o.Width-inWidth)/2, (o.Height-inHeight)/2
image, err = vipsEmbed(image, left, top, o.Width, o.Height, o.Extend)
break
case o.Top > 0 || o.Left > 0:
if o.AreaWidth == 0 {
o.AreaHeight = o.Width
}
if o.AreaHeight == 0 {
o.AreaHeight = o.Height
}
if o.AreaWidth == 0 || o.AreaHeight == 0 {
return nil, errors.New("Extract area width/height params are required")
}
image, err = vipsExtract(image, o.Left, o.Top, o.AreaWidth, o.AreaHeight)
break
}
return image, err
}
示例12: generateValidatedLengthExample
// generateValidatedLengthExample generates a random size array of examples based on what's given.
func (eg *exampleGenerator) generateValidatedLengthExample() interface{} {
minlength, maxlength := math.Inf(1), math.Inf(-1)
for _, v := range eg.a.Validations {
switch actual := v.(type) {
case *dslengine.MinLengthValidationDefinition:
minlength = math.Min(minlength, float64(actual.MinLength))
maxlength = math.Max(maxlength, float64(actual.MinLength))
case *dslengine.MaxLengthValidationDefinition:
minlength = math.Min(minlength, float64(actual.MaxLength))
maxlength = math.Max(maxlength, float64(actual.MaxLength))
}
}
count := 0
if math.IsInf(minlength, 1) {
count = int(maxlength) - (eg.r.Int() % 3)
} else if math.IsInf(maxlength, -1) {
count = int(minlength) + (eg.r.Int() % 3)
} else if minlength < maxlength {
count = int(minlength) + (eg.r.Int() % int(maxlength-minlength))
} else if minlength == maxlength {
count = int(minlength)
} else {
panic("Validation: MinLength > MaxLength")
}
if !eg.a.Type.IsArray() {
return eg.r.faker.Characters(count)
}
res := make([]interface{}, count)
for i := 0; i < count; i++ {
res[i] = eg.a.Type.ToArray().ElemType.GenerateExample(eg.r)
}
return res
}
示例13: GenerateMap
func GenerateMap(width, depth int, gridSize int) *Map {
m := new(Map)
m.width = width
m.depth = depth
m.gridSize = gridSize
m.minHeight = 1000000
m.maxHeight = 0
diag := math.Hypot(float64(m.width/2), float64(m.depth/2))
for z := 0; z < depth; z++ {
for x := 0; x < width; x++ {
fx := float64(x) + float64(z%2)*0.5
fz := float64(z)
d := math.Hypot(float64(m.width/2)-fx, float64(m.depth/2)-fz)
d = 1.0 - d/diag
h := noise.OctaveNoise2d(fx, fz, 4, 0.25, 1.0/28)
h = (h + 1.0) * 0.5
h = math.Sqrt(h) * 1024 * (math.Pow(d, 2))
h = math.Max(h, 120)
m.heightMap = append(m.heightMap, float32(h))
m.minHeight = math.Min(m.minHeight, h)
m.maxHeight = math.Max(m.maxHeight, h)
}
}
return m
}
示例14: CheckCollision
func (pm *ProjectileManager) CheckCollision(p pM.Projectiler, dx, dy float64) (bool, gameObjectsBase.Activer) {
center := p.GetCenter()
newCenter := geometry.MakePoint(center.X+dx, center.Y+dy)
rects := make([]*geometry.Rectangle, 0, 100)
rect2obj := make(map[*geometry.Rectangle]gameObjectsBase.Activer)
for i := int(math.Min(center.Y, newCenter.Y)); i <= int(math.Max(center.Y, newCenter.Y)); i++ {
for j := int(math.Min(center.X, newCenter.X)); j <= int(math.Max(center.X, newCenter.X)); j++ {
if pm.field.IsBlocked(j, i) {
rects = append(rects, pm.field.GetCellRectangle(j, i))
} else {
for _, actor := range pm.field.GetActors(j, i) {
r := actor.GetRectangle()
rects = append(rects, &r)
rect2obj[&r] = actor
}
}
}
}
s := geometry.MakeSegment(center.X, center.Y, center.X+dx, center.Y+dy)
for _, rect := range rects {
if rect.CrossedBySegment(s) {
return true, rect2obj[rect]
}
}
return false, nil
}
示例15: GetStringBounds
// GetStringBounds returns the approximate pixel bounds of the string s at x, y.
// The left edge of the em square of the first character of s
// and the baseline intersect at 0, 0 in the returned coordinates.
// Therefore the top and left coordinates may well be negative.
func (gc *ImageGraphicContext) GetStringBounds(s string) (left, top, right, bottom float64) {
font, err := gc.loadCurrentFont()
if err != nil {
log.Println(err)
return 0, 0, 0, 0
}
top, left, bottom, right = 10e6, 10e6, -10e6, -10e6
cursor := 0.0
prev, hasPrev := truetype.Index(0), false
for _, rune := range s {
index := font.Index(rune)
if hasPrev {
cursor += fUnitsToFloat64(font.Kerning(int32(gc.Current.Scale), prev, index))
}
if err := gc.glyphBuf.Load(gc.Current.Font, int32(gc.Current.Scale), index, truetype.NoHinting); err != nil {
log.Println(err)
return 0, 0, 0, 0
}
e0 := 0
for _, e1 := range gc.glyphBuf.End {
ps := gc.glyphBuf.Point[e0:e1]
for _, p := range ps {
x, y := pointToF64Point(p)
top = math.Min(top, y)
bottom = math.Max(bottom, y)
left = math.Min(left, x+cursor)
right = math.Max(right, x+cursor)
}
}
cursor += fUnitsToFloat64(font.HMetric(int32(gc.Current.Scale), index).AdvanceWidth)
prev, hasPrev = index, true
}
return left, top, right, bottom
}