Beispiel #1
0
func (l Line) WeightedIterator() (pix []WeightedPoint) {

	max_delta := 2.0 * l.radius // will miss prob mass outside of 2 std dev
	normalizer := 1.0 / math.Sqrt(2.0*math.Pi*l.radius*l.radius)

	var p image.Point
	cur := l.left
	iter := int(math.Fmax(math.Fabs(l.Dx()), math.Fabs(l.Dy()))) + 1
	dx := l.Dx() / float64(iter)
	dy := l.Dy() / float64(iter)

	for i := 0; i < iter; i++ {
		for d := -max_delta; d < max_delta; d += 1.0 {

			if dx > dy { // vertical sweeps
				p = image.Point{int(cur.X), int(cur.Y + d)}
			} else { // horizontal sweeps
				p = image.Point{int(cur.X + d), int(cur.Y)}
			}

			fp := NewFloat64Point(p)
			dist := l.Distance(fp.X, fp.Y)
			dist = math.Fmin(dist, Distance(fp, l.left))
			dist = math.Fmin(dist, Distance(fp, l.right))
			weight := normalizer * math.Exp(-dist*dist/(2.0*l.radius*l.radius))

			pix = append(pix, WeightedPoint{p, weight})
		}
		cur.X += dx
		cur.Y += dy
	}
	return pix
}
Beispiel #2
0
func DrawTable(r io.Writer, t *DataTable, w uint, h uint) os.Error {

	var minVal float64
	var maxVal float64

	for _, v := range t.Values {
		if v != nil {
			f64 := float64(v.Float64Value())
			minVal = math.Fmin(minVal, f64)
			maxVal = math.Fmax(maxVal, f64)
		}
	}

	i := image.NewRGBA(int(w), int(h))
	gc := draw2d.NewGraphicContext(i)
	dx := float64(w-1) / (float64(t.End) - float64(t.Start))
	dy := float64(h-1) / (maxVal - minVal)

	gc.SetLineWidth(1)
	for i, v := range t.Values {
		if v == nil {
			continue
		}
		x := dx * float64(int64(i)*t.Resolution)
		y := dy * (float64(v.Float64Value()) - minVal)
		gc.MoveTo(x+0.45, y+0.5)
		gc.ArcTo(x+0.5, y+0.5, 0.1, 0.1, 0, -math.Pi*2)
		gc.Stroke()
	}

	return png.Encode(r, i)
}
Beispiel #3
0
func (s *Statistics) ComputeScore(c *Combat) {
	// we are more aggresive towards players we outnumber esp if
	// we have the most ant however if we have not seen much of the
	// map we are risk averse
	if s.LTS == nil {
		return
	}
	// fraction of map seen
	vis := .1
	if c != nil {
		vis = float64(c.Size()-s.LTS.Horizon) / float64(c.Size())
	}

	frac := float64(s.LTS.Seen[0]) / float64(s.LTS.SeenAll)

	for i := range s.LTS.Seen {
		if TurnGet() < 60 { // MAGIC
			s.LTS.Score[i] = .4
		} else {
			frace := float64(s.LTS.Seen[i]) / float64(s.LTS.SeenAll)
			s.LTS.Score[i] = math.Fmax(math.Fmin(frac*vis*vis/frace, 2.0), .3)
		}
	}
	s.LTS.Score[0] = -1.5

	if Debug[DBG_Scoring] {
		log.Print("Score per player is ", s.LTS.Score[0:s.NP])
	}
}
Beispiel #4
0
func main() {
	//runtime.GOMAXPROCS(1)

	const tableRez = 1000

	mat := Eye(2)
	mat.Scale(.58)
	mat.Set(1, 0, .2)

	const transCount = 3
	trans := make([]*affine.Affine, transCount)
	trans[0] = affine.FromOrigin2(mat, 0, 0)
	trans[1] = affine.FromOrigin2(mat, .5, 1)
	trans[2] = affine.FromOrigin2(mat, 1, 0)

	x1, y1, x2, y2 := fit(trans)
	//print (x1,x2,y1,y2,"\n")

	shift := Zeros(2, 1)
	shift.Set(0, 0, -x1)
	shift.Set(1, 0, -y1)
	scale := (tableRez - 4) / math.Fmax(x2-x1, y2-y1)
	shift.Scale(scale)
	shift.AddDense(Scaled(Ones(2, 1), 2))

	//print (scale," "+shift.String(),"\n")
	for i, t := range trans {
		origin := Scaled(t.GetOrigin(), scale)
		origin.AddDense(shift)
		trans[i] = affine.FromOrigin(t.GetMat(), origin)
	}

	x1, y1, x2, y2 = fit(trans)
	ix1 := int(x1)
	ix2 := int(x2)
	iy1 := int(y1)
	iy2 := int(y2)
	//print (int(x1)," ",int(x2)," ",int(y1)," ",int(y2),"\n")

	rezx := ix2 + 2
	rezy := iy2 + 2

	ft := floatTable.NewFloatTable(rezx, rezy, channelCount)
	ft2 := floatTable.NewFloatTable(rezx, rezy, channelCount)

	ft.Fill(fill)

	t := time.Nanoseconds()
	Render(ix1, ix2, iy1, iy2, ft, ft2, trans)

	t = time.Nanoseconds() - t
	print("Time", "\n")
	print(t/1000000, "\n")

	println("Saving image")
	f, err := os.Open("testFile.png", os.O_WRONLY|os.O_CREAT, 0666)
	println(err)
	MakeImage(f, ft, MakeColorizer(ft))

}
func (this *eastHandle) InvokeStep(x, y, anchorX, anchorY int, view DrawingView) {
	r := this.owner.GetDisplayBox()
	this.owner.SetDisplayBox(
		this.owner,
		&Point{r.X, r.Y},
		&Point{int(math.Fmax(float64(r.X), float64(x))), r.Y + r.Height})
}
Beispiel #6
0
func (c *Encoder) Open() {
	c.open(CODEC_TYPE_ENCODER)
	c.last_dts = 0
	c.audio_fifo = av_fifo_alloc(1024)
	log.Printf("Encoder Oppened")
	c.stream_index = 0
	size := c.Ctx.ctx.width * c.Ctx.ctx.height
	c.buffer_size = int(math.Fmax(float64(4*192*1024), float64(6*size+200)))
	c.buffer /*[]byte*/ = make([]byte, c.buffer_size+8)
}
Beispiel #7
0
func fit(trans []*affine.Affine) (x1, y1, x2, y2 float64) {
	org := trans[0].GetOrigin()
	x2 = org.Get(0, 0)
	x1 = x2
	y2 = org.Get(1, 0)
	y1 = y2
	nx1, ny1, nx2, ny2 := x1, y1, x2, y2
	done := false
	for !done {
		//print (x1,x2,y1,y2,"\n")
		for i := 0; i < 4; i++ {
			xx := x1
			yy := y1
			if i%2 == 0 {
				xx = x2
			}
			if i > 1 {
				yy = y2
			}
			for _, t := range trans {
				x, y := t.Trans(xx, yy)
				nx1 = math.Fmin(nx1, x)
				nx2 = math.Fmax(nx2, x)
				ny1 = math.Fmin(ny1, y)
				ny2 = math.Fmax(ny2, y)
			}
		}
		if nx1 > x1 && nx2 < x2 && ny1 > y1 && ny2 < y2 {
			done = true
		}
		x1, y1, x2, y2 = nx1, ny1, nx2, ny2
		dx := nx2 - nx1
		const f = .001
		x1 -= dx * f
		x2 += dx * f
		dy := ny2 - ny1
		y1 -= dy * f
		y2 += dy * f

	}
	return
}
func (self *Encoder) encodeVideo(frame *Frame) *[]Packet {
	var result *[]Packet
	size := self.Ctx.width * self.Ctx.height
	buffer_size := C.int(math.Fmax(float64(1024*256), float64(6*size+200)))
	var buffer []byte = make([]byte, int(buffer_size))
	var pbuffer *byte = &buffer[0]
	println(pbuffer)
	//  ret := C.avcodec_encode_video(self.Ctx, (*C.uint8_t) (unsafe.Pointer(pbuffer)), buffer_size, (*C.AVFrame)(unsafe.Pointer(frame)));
	//  println(ret)
	return result
}
Beispiel #9
0
func MakeColorizer(ft *floatTable.FloatTable) func(cell []float32) image.NRGBAColor {
	maxv := float64(0.0)
	for _, t := range ft.Data {
		maxv = math.Fmax(maxv, float64(t))
	}

	println(maxv)
	maxv = math.Log2(maxv / fill)
	return func(cell []float32) image.NRGBAColor {
		c := new(image.NRGBAColor)
		c.R = col(float64(cell[0]), maxv)
		c.G = col(float64(cell[1]), maxv)
		c.B = col(float64(cell[2]), maxv)
		c.A = 255
		return *c
	}
}
Beispiel #10
0
func decodegeohashbitset(bitset string, floor, ceiling float64) float64 {
	mid := 0.0
	// calculate the mean error for improved rounding
	err := (ceiling - floor) / 2.0

	for _, val := range bitset {
		mid = (floor + ceiling) / 2.0
		err /= 2.0
		switch val {
		case '1':
			floor = mid
		case '0':
			ceiling = mid
		}
	}
	// rounding according to
	return round(mid, int(math.Fmax(1.0, -round(math.Log10(err), 0))-1))
}
Beispiel #11
0
// it would be nice to use goroutines for the interator, but
// it appears to be way too slow:
// http://groups.google.com/group/golang-nuts/browse_thread/thread/a717e1286a8736fd#
// for now i'll use a fully blocking producer-consumer model
func (l Line) UnweightedIterator() (pix []WeightedPoint) {

	cur := l.left
	iter := int(math.Fmax(math.Fabs(l.Dx()), math.Fabs(l.Dy())))
	if iter == 0 {
		p := image.Point{int(l.left.X), int(l.right.Y)}
		return append(pix, WeightedPoint{p, 1.0})
	}
	dx := l.Dx() / float64(iter)
	dy := l.Dy() / float64(iter)
	for i := 0; i < iter; i++ {
		p := image.Point{int(cur.X), int(cur.Y)}
		pix = append(pix, WeightedPoint{p, 1.0})
		cur.X += dx
		cur.Y += dy
	}
	return pix
}
Beispiel #12
0
func ValueIteration(qt *discrete.QTable, mdp discrete.MDP, epsilon float64) (numIterations int) {
	//fmt.Fprintf(os.Stderr, "+ValueIteration\n")
	//fmt.Println(mdp.GetGamma())
	//defer fmt.Fprintf(os.Stderr, "-ValueIteration\n")
	var error float64
	for {
		numIterations += 1
		//fmt.Printf("iteration %d\n", numIterations)
		error = 0
		for s := range mdp.S64() {
			for a := range mdp.A64() {
				saError := BackupStateAction(qt, mdp, s, a)
				error = math.Fmax(error, saError)
			}
		}
		//fmt.Printf("QT\n%v\n", qt)
		//fmt.Fprintf(os.Stderr, "error %f\n%v\n", error, qt)
		if error < epsilon {
			return
		}
	}
	return
}
Beispiel #13
0
func (this *Rectangle) ContainsRect(rect *Rectangle) bool {
	return (rect.X >= this.X && rect.Y >= this.Y &&
		(rect.X+int(math.Fmax(0, float64(rect.Width)))) <= this.X+int(math.Fmax(0, float64(this.Width))) &&
		(rect.Y+int(math.Fmax(0, float64(rect.Height)))) <= this.Y+int(math.Fmax(0, float64(this.Height))))
}
Beispiel #14
0
func col(v, maxv float64) uint8 {
	v = math.Log2(v / fill)
	v = v * 255 / maxv
	return uint8(math.Fmax(0, math.Fmin(255, v)))
}
Beispiel #15
0
func (this *southWestHandle) InvokeStep(x, y, anchorX, anchorY int, view DrawingView) {
	r := this.owner.GetDisplayBox()
	topLeft := &Point{int(math.Fmin(float64(r.X+r.Width), float64(x))), r.Y}
	bottomRight := &Point{r.X + r.Width, int(math.Fmax(float64(r.Y), float64(y)))}
	this.owner.SetDisplayBox(this.owner, topLeft, bottomRight)
}
Beispiel #16
0
func (this *southHandle) InvokeStep(x, y, anchorX, anchorY int, view DrawingView) {
	r := this.owner.GetDisplayBox()
	this.owner.SetDisplayBox(this.owner, &Point{r.X, r.Y},
		&Point{r.X + r.Width, int(math.Fmax(float64(r.Y), float64(y)))})
}