当前位置: 首页>>代码示例>>Golang>>正文


Golang math.Min函数代码示例

本文整理汇总了Golang中math.Min函数的典型用法代码示例。如果您正苦于以下问题:Golang Min函数的具体用法?Golang Min怎么用?Golang Min使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Min函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: SetAspect

// Change the aspect ratio of a rectangle.
// The mode can be "area", "width", "height", "fit", "fill" or "stretch".
// Panics if mode is empty or unrecognized.
func SetAspect(w, h, aspect float64, mode string) (float64, float64) {
	switch mode {
	case "area":
		// aspect = width / height
		// width = height * aspect
		// width^2 = width * height * aspect
		// height = width / aspect
		// height^2 = width * height / aspect
		w, h = math.Sqrt(w*h*aspect), math.Sqrt(w*h/aspect)
	case "width":
		// Set height from width.
		h = w / aspect
	case "height":
		// Set width from height.
		w = h * aspect
	case "fit":
		// Shrink one dimension.
		w, h = math.Min(w, h*aspect), math.Min(h, w/aspect)
	case "fill":
		// Grow one dimension.
		w, h = math.Max(w, h*aspect), math.Max(h, w/aspect)
	case "stretch":
		// Do nothing.
	case "":
		panic("no mode specified")
	default:
		panic("unknown mode: " + mode)
	}
	return w, h
}
开发者ID:jvlmdr,项目名称:go-cv,代码行数:33,代码来源:rect.go

示例2: 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)
}
开发者ID:ChrisOHu,项目名称:platform,代码行数:45,代码来源:adjust.go

示例3: ExtendPointZ

func (b *Bounds) ExtendPointZ(pointZ PointZ) *Bounds {
	b.Min.X = math.Min(b.Min.X, pointZ.X)
	b.Min.Y = math.Min(b.Min.Y, pointZ.Y)
	b.Max.X = math.Max(b.Max.X, pointZ.X)
	b.Max.Y = math.Max(b.Max.Y, pointZ.Y)
	return b
}
开发者ID:romaxa,项目名称:gogeom,代码行数:7,代码来源:bounds.go

示例4: distanceRectRect

func distanceRectRect(rect1 Rect, rect2 Rect) float64 {
	var d float64 = 10000

	for _, point := range rect1.MakeCorners() {
		d = math.Min(d, distancePointRect(point, rect2))

		if d < 1 {
			return d
		}
	}
	for _, point := range rect2.MakeCorners() {
		d = math.Min(d, distancePointRect(point, rect1))

		if d < 1 {
			return d
		}
	}

	for _, line := range rect1.MakeLines() {
		d = math.Min(d, distanceLineRect(line, rect2))

		if d < 1 {
			return d
		}
	}

	return d
}
开发者ID:OSPokemon,项目名称:ospokemon,代码行数:28,代码来源:distance.go

示例5: Min

func (v3 *Vector3) Min(v *Vector3) *Vector3 {
	v3.X = math.Min(v3.X, v.X)
	v3.Y = math.Min(v3.Y, v.Y)
	v3.Z = math.Min(v3.Z, v.Z)

	return v3
}
开发者ID:uzudil,项目名称:three.go,代码行数:7,代码来源:Vector3.go

示例6: subtractPSF

func subtractPSF(psf []float32,
	psfWidth uint64,
	residual []float32,
	residualWidth uint64,
	peakPos uint64, psfPeakPos uint64,
	absPeakVal float32,
	gain float32) {
	var rx = float64(xpos(peakPos, residualWidth))
	var ry = float64(ypos(peakPos, residualWidth))

	var px = float64(xpos(psfPeakPos, psfWidth))
	var py = float64(ypos(psfPeakPos, psfWidth))

	var diffx uint64 = uint64(rx - px)
	var diffy uint64 = uint64(ry - py)

	var startx = math.Max(0, float64(rx-px))
	var starty = math.Max(0, float64(ry-py))

	var stopx = math.Min(float64(residualWidth-1), rx+(float64(psfWidth)-px-1))
	var stopy = math.Min(float64(residualWidth-1), ry+(float64(psfWidth)-py-1))

	factor := gain * absPeakVal
	for y := uint64(starty); y <= uint64(stopy); y++ {
		for x := uint64(startx); x <= uint64(stopx); x++ {
			residual[posToIdx(residualWidth, x, y)] -= factor *
				psf[posToIdx(psfWidth, x-diffx, y-diffy)]
		}
	}
}
开发者ID:ATNF,项目名称:askap-benchmarks,代码行数:30,代码来源:Hogbom.go

示例7: doReliableRegistration

func (driver *MesosSchedulerDriver) doReliableRegistration(maxBackoff float64) {
	for {
		if !driver.registerOnce() {
			return
		}
		maxBackoff = math.Min(maxBackoff, registrationRetryIntervalMax)

		// If failover timeout is present, bound the maximum backoff
		// by 1/10th of the failover timeout.
		if driver.failoverTimeout > 0 {
			maxBackoff = math.Min(maxBackoff, driver.failoverTimeout/10.0)
		}

		// Determine the delay for next attempt by picking a random
		// duration between 0 and 'maxBackoff' (jitter).
		delay := time.Duration(maxBackoff * rand.Float64())

		log.V(1).Infof("will retry registration in %v if necessary", delay)

		t := time.NewTimer(delay)
		defer t.Stop()

		select {
		case <-driver.stopCh:
			return
		case <-t.C:
			maxBackoff *= 2
		}
	}
}
开发者ID:basho-labs,项目名称:mesos-go,代码行数:30,代码来源:scheduler.go

示例8: 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
}
开发者ID:slav123,项目名称:bimg,代码行数:33,代码来源:resize.go

示例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)
}
开发者ID:vladimir-ch,项目名称:ode,代码行数:36,代码来源:integrate.go

示例10: 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
}
开发者ID:RouGang,项目名称:goa,代码行数:34,代码来源:example.go

示例11: evaluate

func (self *Population) evaluate() {
	for k := 0; k < self.conf.CrossoversCount; k++ {
		if rand.Float64() < self.conf.CrossoverProb {
			g1 := self.P[rand.Int31n(int32(math.Min(float64(len(self.P)), float64(self.conf.SelectionCount))))]
			g2 := self.P[rand.Int31n(int32(math.Min(float64(len(self.P)), float64(self.conf.SelectionCount))))]

			child := g1.Crossover(g2)

			if rand.Float64() < self.conf.MutationProb {
				child.Mutate()
			}
			child.EvalFitness()

			self.P = append(self.P, child)
		}
	}

	self.sort()
	if self.conf.RemoveDuplicates {
		self.removeDuplicates()
	}
	for i := range self.P {
		self.P[i].EvalFitness()
	}
	if len(self.P) > self.conf.PopulationSize {
		self.P = self.P[:self.conf.PopulationSize]
	}
	//	pretty.Println(self.P)
}
开发者ID:igorcoding,项目名称:go-genpath,代码行数:29,代码来源:population.go

示例12: stepMovingObj

// stepMovingObj steps the specified MovingObj and draws its image to its new position onto the LabImg.
func stepMovingObj(m *model.MovingObj) {
	x, y := int(m.Pos.X), int(m.Pos.Y)

	// Only horizontal or vertical movement is allowed!
	if x != m.TargetPos.X {
		dx := math.Min(dt*model.V, math.Abs(float64(m.TargetPos.X)-m.Pos.X))
		if x > m.TargetPos.X {
			dx = -dx
			m.Direction = model.DirLeft
		} else {
			m.Direction = model.DirRight
		}
		m.Pos.X += dx
	} else if y != m.TargetPos.Y {
		dy := math.Min(dt*model.V, math.Abs(float64(m.TargetPos.Y)-m.Pos.Y))
		if y > m.TargetPos.Y {
			dy = -dy
			m.Direction = model.DirUp
		} else {
			m.Direction = model.DirDown
		}
		m.Pos.Y += dy
	}

	// Draw image at new position
	m.DrawImg()
}
开发者ID:fun-alex-alex2006hw,项目名称:golab,代码行数:28,代码来源:engine.go

示例13: 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
}
开发者ID:apanasenko,项目名称:MonsterQuest,代码行数:26,代码来源:projectileManager.go

示例14: GeoCell

func GeoCell(lat, lon float64, resolution int) string {
	north := 90.0
	south := -90.0
	east := 180.0
	west := -180.0
	cell := make([]byte, resolution, resolution)

	for i := 0; i < resolution; i++ {
		subcellLonSpan := (east - west) / GEOCELL_GRID_SIZE
		subcellLatSpan := (north - south) / GEOCELL_GRID_SIZE

		x := int(math.Min(GEOCELL_GRID_SIZE*(lon-west)/(east-west), GEOCELL_GRID_SIZE-1))
		y := int(math.Min(GEOCELL_GRID_SIZE*(lat-south)/(north-south), GEOCELL_GRID_SIZE-1))

		pos := (y&2)<<2 | (x&2)<<1 | (y&1)<<1 | (x&1)<<0
		cell[i] = GEOCELL_ALPHABET[pos]

		south += subcellLatSpan * float64(y)
		north = south + subcellLatSpan

		west += subcellLonSpan * float64(x)
		east = west + subcellLonSpan
	}

	return string(cell)
}
开发者ID:ayngling,项目名称:geomodel,代码行数:26,代码来源:geomodel.go

示例15: RGBToHSV

// RGBToHSV converts an RGB triple to a HSV triple.
//
// Ported from http://goo.gl/Vg1h9
func RGBToHSV(r, g, b uint8) (h, s, v float64) {
	fR := float64(r) / 255
	fG := float64(g) / 255
	fB := float64(b) / 255
	max := math.Max(math.Max(fR, fG), fB)
	min := math.Min(math.Min(fR, fG), fB)
	d := max - min
	s, v = 0, max
	if max > 0 {
		s = d / max
	}
	if max == min {
		// Achromatic.
		h = 0
	} else {
		// Chromatic.
		switch max {
		case fR:
			h = (fG - fB) / d
			if fG < fB {
				h += 6
			}
		case fG:
			h = (fB-fR)/d + 2
		case fB:
			h = (fR-fG)/d + 4
		}
		h /= 6
	}
	return
}
开发者ID:Bobberino,项目名称:musings,代码行数:34,代码来源:hsv.go


注:本文中的math.Min函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。