本文整理匯總了Golang中math.Hypot函數的典型用法代碼示例。如果您正苦於以下問題:Golang Hypot函數的具體用法?Golang Hypot怎麽用?Golang Hypot使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Hypot函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GenerateMap
func GenerateMap(width, height int, gridSize int) *Map {
m := new(Map)
m.width = width
m.height = height
m.gridSize = gridSize
m.grid = make([]int, width*height)
diag := math.Hypot(float64(m.width/2), float64(m.height/2))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
fx := float64(x)
fy := float64(y)
// calculate inverse distance from center
d := 1.0 - math.Hypot(float64(m.width/2)-fx, float64(m.height/2)-fy)/diag
d = d
v := noise.OctaveNoise2d(fx, fy, 4, 0.5, 1.0/64)
v = (v + 1.0) / 2
v = v * 256
m.grid[y*width+x] = int(v)
m.gridLines = append(m.gridLines, float32(x*m.gridSize), 0, float32(x*m.gridSize), float32((m.height-1)*m.gridSize))
}
m.gridLines = append(m.gridLines, 0, float32(y*m.gridSize), float32((m.width-1)*m.gridSize), float32(y*m.gridSize))
}
m.RebuildVertices()
return m
}
示例2: main
func main() {
var N int
sc.Split(bufio.ScanWords)
fmt.Scan(&N)
dataA := make([]point, N)
dataB := make([]point, N)
var aveA, aveB point
for i := 0; i < N; i++ {
dataA[i].x = nextInt()
dataA[i].y = nextInt()
Add(&aveA, dataA[i])
}
for j := 0; j < N; j++ {
dataB[j].x = nextInt()
dataB[j].y = nextInt()
Add(&aveB, dataB[j])
}
X := float64(N)
aveA.x /= X
aveA.y /= X
aveB.x /= X
aveB.y /= X
lenA := float64(0)
lenB := float64(0)
for i := 0; i < N; i++ {
if lenA < math.Hypot(dataA[i].x-aveA.x, dataA[i].y-aveA.y) {
lenA = math.Hypot(dataA[i].x-aveA.x, dataA[i].y-aveA.y)
}
if lenB < math.Hypot(dataB[i].x-aveB.x, dataB[i].y-aveB.y) {
lenB = math.Hypot(dataB[i].x-aveB.x, dataB[i].y-aveB.y)
}
}
fmt.Println(lenB / lenA)
}
示例3: wireBrane
func wireBrane(min int, max int) {
// wire the brane in a random fashion
// for each neurode...
for i := 0; i < len(brane.neurodes); i++ {
this := brane.neurodes[i]
// ...find all neurodes within reach of its forcept
//var nearNeurodes []Neurode
for j := 0; j < len(brane.neurodes); j++ {
// skip self...
if j != i {
that := brane.neurodes[j]
// calc 3d distance
distance := int(math.Hypot(math.Hypot(float64(this.x)-float64(that.x), float64(this.y)-float64(that.y)), float64(this.z)-float64(that.z)))
if distance <= this.flength {
fmt.Println(this.id, that.id, distance)
}
}
}
}
}
示例4: HasEdgeBetween
// HasEdgeBetween optimistically returns whether an edge is exists between u and v.
func (l *LimitedVisionGrid) HasEdgeBetween(u, v graph.Node) bool {
if u.ID() == v.ID() {
return false
}
ur, uc := l.RowCol(u.ID())
vr, vc := l.RowCol(v.ID())
if abs(ur-vr) > 1 || abs(uc-vc) > 1 {
return false
}
if !l.Grid.AllowDiagonal && ur != vr && uc != vc {
return false
}
x, y := l.XY(l.Location)
ux, uy := l.XY(u)
vx, vy := l.XY(v)
uKnown := l.Known[u.ID()] || math.Hypot(x-ux, y-uy) <= l.VisionRadius
vKnown := l.Known[v.ID()] || math.Hypot(x-vx, y-vy) <= l.VisionRadius
switch {
case uKnown && vKnown:
return l.Grid.HasEdgeBetween(u, v)
case uKnown:
return l.Grid.HasOpen(u)
case vKnown:
return l.Grid.HasOpen(v)
default:
return true
}
}
示例5: TestComputeAccelerationOnBodyBarnesHut
// test that the barnes hut computation of repulsive
// forces is close to the classic computation
func TestComputeAccelerationOnBodyBarnesHut(t *testing.T) {
bodies := make([]quadtree.Body, 2000000)
SpreadOnCircle(&bodies)
// quadtree.InitBodiesUniform( &bodies, 200)
var r Run
r.Init(&bodies)
r.computeAccelerationOnBody(0)
accReference := (*r.bodiesAccel)[0]
r.computeAccelerationOnBodyBarnesHut(0)
accBH := (*r.bodiesAccel)[0]
r.q.CheckIntegrity(t)
// measure the difference between reference and BH
accReferenceLength := math.Hypot(accReference.X, accReference.Y)
diff := math.Hypot((accReference.X - accBH.X), (accReference.Y - accBH.Y))
relativeError := diff / accReferenceLength
// tolerance is arbitrary set
tolerance := 0.02 // 5%
if relativeError > tolerance {
t.Errorf("different results, accel ref x %f y %f, got x %f y %f", accReference.X, accReference.Y, accBH.X, accBH.Y)
t.Errorf("different results, expected less than %f, got %f", tolerance, relativeError)
}
}
示例6: 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
}
示例7: b
func b(x, y int) uint8 {
i := float64(x)
j := float64(y)
top := math.Hypot(300-i, 20-j) + 1
bottom := math.Sqrt(float64(math.Abs(math.Sin((math.Hypot(503-i, 103-j))/115)))) + 1
return uint8(top / bottom)
}
示例8: r
func r(x, y int) uint8 {
i := float64(x)
j := float64(y)
top := math.Hypot(148-i, 1000-j) + 1
bottom := math.Sqrt(float64(math.Abs(math.Sin((math.Hypot(500-i, 400-j))/115)))) + 1
return uint8(top / bottom)
}
示例9: g
func g(x, y int) uint8 {
i := float64(x)
j := float64(y)
top := math.Hypot(610-i, 60-j) + 1
bottom := math.Sqrt(float64(math.Abs(math.Sin((math.Hypot(864-i, 860-j))/115)))) + 1
return uint8(top / bottom)
}
示例10: DrawLinear
// DrawLinear draws a linear gradient to dst. If the gradient vector (as
// defined by x0, y0, x1, and y1) is found to be purely horizontal or purely
// vertical, the appropriate optimized functions will be called.
func DrawLinear(dst draw.Image, x0, y0, x1, y1 float64, stops []Stop) {
if y0 == y1 && x0 != x1 {
drawHLinear(dst, x0, x1, stops)
return
}
if x0 == x1 && y0 != y1 {
drawVLinear(dst, y0, y1, stops)
return
}
if len(stops) == 0 {
return
}
if y0 > y1 {
panic(fmt.Sprintf("invalid bounds y0(%f)>y1(%f)", y0, y1))
}
if x0 > x1 {
panic(fmt.Sprintf("invalid bounds x0(%f)>x1(%f)", x0, x1))
}
bb := dst.Bounds()
width, height := bb.Dx(), bb.Dy()
x0, y0 = x0*float64(width), y0*float64(height)
x1, y1 = x1*float64(width), y1*float64(height)
dx, dy := x1-x0, y1-y0
px0, py0 := x0-dy, y0+dx
mag := math.Hypot(dx, dy)
var col color.Color
for y := 0; y < width; y++ {
fy := float64(y)
for x := 0; x < width; x++ {
fx := float64(x)
// is the pixel before the start of the gradient?
s0 := (px0-x0)*(fy-y0) - (py0-y0)*(fx-x0)
if s0 > 0 {
col = stops[0].Col
} else {
// calculate the distance of the pixel from the first stop line
u := ((fx-x0)*(px0-x0) + (fy-y0)*(py0-y0)) /
(mag * mag)
x2, y2 := x0+u*(px0-x0), y0+u*(py0-y0)
d := math.Hypot(fx-x2, fy-y2) / mag
col = getColour(d, stops)
}
dst.Set(x+bb.Min.X, y+bb.Min.Y, col)
}
}
}
示例11: Arenstorf
func Arenstorf(yDot []float64, _ float64, y []float64) {
mu1 := 0.012277471
mu2 := 1 - mu1
d1 := math.Hypot(y[0]+mu1, y[1])
d1 *= d1 * d1
d2 := math.Hypot(y[0]-mu2, y[1])
d2 *= d2 * d2
yDot[0] = y[2]
yDot[1] = y[3]
yDot[2] = y[0] + 2*y[3] - mu2*(y[0]+mu1)/d1 - mu1*(y[0]-mu2)/d2
yDot[3] = y[1] - 2*y[2] - mu2*y[1]/d1 - mu1*y[1]/d2
}
示例12: nearest
// nearest finds the nearest mean to a given point.
// return values are the index of the nearest mean, and the distance from
// the point to the mean.
func nearest(p r2c, mean []r2) (int, float64) {
iMin := 0
dMin := math.Hypot(p.x-mean[0].x, p.y-mean[0].y)
for i := 1; i < len(mean); i++ {
d := math.Hypot(p.x-mean[i].x, p.y-mean[i].y)
if d < dMin {
dMin = d
iMin = i
}
}
return iMin, dMin
}
示例13: TestHypot
func TestHypot(t *testing.T) {
f := func(x struct{ X, Y float32 }) bool {
y := Hypot(x.X, x.Y)
if math.Hypot(float64(x.X), float64(x.Y)) > math.MaxFloat32 {
return true
}
return floats.EqualWithinRel(float64(y), math.Hypot(float64(x.X), float64(x.Y)), tol)
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
示例14: Distance
// Distance computes the L-norm of s - t. See Norm for special cases.
// A panic will occur if the lengths of s and t do not match.
func Distance(s, t []float64, L float64) float64 {
if len(s) != len(t) {
panic("floats: slice lengths do not match")
}
if len(s) == 0 {
return 0
}
var norm float64
if L == 2 {
for i, v := range s {
diff := t[i] - v
norm = math.Hypot(norm, diff)
}
return norm
}
if L == 1 {
for i, v := range s {
norm += math.Abs(t[i] - v)
}
return norm
}
if math.IsInf(L, 1) {
for i, v := range s {
absDiff := math.Abs(t[i] - v)
if absDiff > norm {
norm = absDiff
}
}
return norm
}
for i, v := range s {
norm += math.Pow(math.Abs(t[i]-v), L)
}
return math.Pow(norm, 1/L)
}
示例15: Norm
// Norm returns the L norm of the slice S, defined as
// (sum_{i=1}^N s[i]^L)^{1/L}
// Special cases:
// L = math.Inf(1) gives the maximum absolute value.
// Does not correctly compute the zero norm (use Count).
func Norm(s []float64, L float64) float64 {
// Should this complain if L is not positive?
// Should this be done in log space for better numerical stability?
// would be more cost
// maybe only if L is high?
if len(s) == 0 {
return 0
}
if L == 2 {
twoNorm := math.Abs(s[0])
for i := 1; i < len(s); i++ {
twoNorm = math.Hypot(twoNorm, s[i])
}
return twoNorm
}
var norm float64
if L == 1 {
for _, val := range s {
norm += math.Abs(val)
}
return norm
}
if math.IsInf(L, 1) {
for _, val := range s {
norm = math.Max(norm, math.Abs(val))
}
return norm
}
for _, val := range s {
norm += math.Pow(math.Abs(val), L)
}
return math.Pow(norm, 1/L)
}