Golang Sin示例

说明

golang sin示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Golang

命名空间/包名称: math

示例#1
文件: math_test.go 项目: thecc4re/meeus

// Section "Trigonometric functions of large angles":
//
// Meeus makes his point, but an example with integer values is a bit unfair
// when trigonometric functions inherently work on floating point numbers.
func ExamplePMod_integer() {
	const large = 36000030

	// The direct function call loses precition as expected.
	fmt.Println("Direct:    ", math.Sin(large*math.Pi/180))

	// When the value is manually reduced to the integer 30, the Go constant
	// evaluaton does a good job of delivering a radian value to math.Sin that
	// evaluates to .5 exactly.
	fmt.Println("Integer 30:", math.Sin(30*math.Pi/180))

	// Math.Mod takes float64s and returns float64s.  The integer constants
	// here however can be represented exactly as float64s, and the returned
	// result is exact as well.
	fmt.Println("Math.Mod:  ", math.Mod(large, 360))

	// But when math.Mod is substituted into the Sin function, float64s
	// are multiplied instead of the high precision constants, and the result
	// comes back slightly inexact.
	fmt.Println("Sin Mod:   ", math.Sin(math.Mod(large, 360)*math.Pi/180))

	// Use of PMod on integer constants produces results identical to above.
	fmt.Println("PMod int:  ", math.Sin(base.PMod(large, 360)*math.Pi/180))

	// As soon as the large integer is scaled to a non-integer value though,
	// precision is lost and PMod is of no help recovering at this point.
	fmt.Println("PMod float:", math.Sin(base.PMod(large*math.Pi/180, 2*math.Pi)))
	// Output:
	// Direct:     0.49999999995724154
	// Integer 30: 0.5
	// Math.Mod:   30
	// Sin Mod:    0.49999999999999994
	// PMod int:   0.49999999999999994
	// PMod float: 0.49999999997845307
}

示例#2
文件: 7-02WEB计数器.go 项目: oywc410/MYPG

func lissajous(out http.ResponseWriter, r *http.Request) {
	const (
		cycles  = 5
		res     = 0.001
		size    = 100
		nframes = 64
		delay   = 8
	)

	freq := rand.Float64() * 3.0
	anim := gif.GIF{LoopCount: nframes}
	phase := 0.0
	for i := 0; i < nframes; i++ {
		rect := image.Rect(0, 0, 2*size+1, 2*size+1)
		img := image.NewPaletted(rect, palette)
		for t := 0.0; t < cycles*2*math.Pi; t += res {
			x := math.Sin(t)
			y := math.Sin(t*freq + phase)
			img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), blackIndex)
		}
		phase += 0.1
		anim.Delay = append(anim.Delay, delay)
		anim.Image = append(anim.Image, img)
	}

	gif.EncodeAll(out, &anim)
}

示例#3
文件: Ex1_6_2.go 项目: Esper0328/Go_training

func lissajous(out io.Writer) {
	const (
		cycles  = 5     // number of complete x oscillator revolutions
		res     = 0.001 // angular resolution
		size    = 100   // image canvas covers [-size..+size]
		nframes = 64    // number of animation frames
		delay   = 8     // delay between frames in 10ms units
	)
	freq := rand.Float64() * 3.0 // relative frequency of y oscillator
	anim := gif.GIF{LoopCount: nframes}
	phase := 0.0 // phase difference
	for i := 0; i < nframes; i++ {
		rect := image.Rect(0, 0, 2*size+1, 2*size+1)
		img := image.NewPaletted(rect, palette)
		for t := 0.0; t < cycles*2*math.Pi; t += res {
			x := math.Sin(t)
			y := math.Sin(t*freq + phase)
			img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), uint8(i)%3+1)
		}
		phase += 0.1
		anim.Delay = append(anim.Delay, delay)
		anim.Image = append(anim.Image, img)
	}
	gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors
}

示例#4
文件: saturnmoons.go 项目: soniakeys/meeus

func (q *qs) hyperion() (r r4) {
	η := 92.39*d + .5621071*d*q.t6
	ζ := 148.19*d - 19.18*d*q.t8
	θ := 184.8*d - 35.41*d*q.t9
	θʹ := θ - 7.5*d
	as := 176*d + 12.22*d*q.t8
	bs := 8*d + 24.44*d*q.t8
	cs := bs + 5*d
	ϖ := 69.898*d - 18.67088*d*q.t8
	φ := 2 * (ϖ - q.W5)
	χ := 94.9*d - 2.292*d*q.t8
	sη, cη := math.Sincos(η)
	sζ, cζ := math.Sincos(ζ)
	s2ζ, c2ζ := math.Sincos(2 * ζ)
	s3ζ, c3ζ := math.Sincos(3 * ζ)
	sζpη, cζpη := math.Sincos(ζ + η)
	sζmη, cζmη := math.Sincos(ζ - η)
	sφ, cφ := math.Sincos(φ)
	sχ, cχ := math.Sincos(χ)
	scs, ccs := math.Sincos(cs)
	a := 24.50601 - .08686*cη - .00166*cζpη + .00175*cζmη
	e := .103458 - .004099*cη - .000167*cζpη + .000235*cζmη +
		.02303*cζ - .00212*c2ζ + 0.000151*c3ζ + .00013*cφ
	p := ϖ + .15648*d*sχ - .4457*d*sη - .2657*d*sζpη - .3573*d*sζmη -
		12.872*d*sζ + 1.668*d*s2ζ - .2419*d*s3ζ - .07*d*sφ
	λʹ := 177.047*d + 16.91993829*d*q.t6 + .15648*d*sχ + 9.142*d*sη +
		.007*d*math.Sin(2*η) - .014*d*math.Sin(3*η) + .2275*d*sζpη +
		.2112*d*sζmη - .26*d*sζ - .0098*d*s2ζ -
		.013*d*math.Sin(as) + .017*d*math.Sin(bs) - .0303*d*sφ
	i := 27.3347*d + .6434886*d*cχ + .315*d*q.cW3 + .018*d*math.Cos(θ) -
		.018*d*ccs
	Ω := 168.6812*d + 1.40136*d*cχ + .68599*d*q.sW3 - .0392*d*scs +
		.0366*d*math.Sin(θʹ)
	return q.subr(λʹ, p, e, a, Ω, i)
}

示例#5
文件: main.go 项目: sonedazaurus/golang-training

func f(x, y float64) float64 {
	v := math.Pow(2.0, math.Sin(y)) * math.Pow(2.0, math.Sin(x)) / 12
	if math.IsNaN(v) {
		return 0
	}
	return v
}

示例#6
文件: entity.go 项目: iamkether/spacegophers

// Simulate executes the simulation steps in order to move the entity to it's
// new location and apply any pending updates/changes.
func (e *Entity) Simulate() {
	var angleNot = e.Angle
	var velocityNot = e.Velocity

	// mark that we did what they thought we did
	e.Updated = e.Updates

	// apply YAW
	if e.Updates.Left != e.Updates.Right {
		if e.Updates.Left {
			e.Angle -= angleStep
		} else {
			e.Angle += angleStep
		}
	}

	// apply THRUST
	if e.Updates.Forward != e.Updates.Backward {
		if e.Updates.Forward {
			e.Velocity.X += e.thrust * math.Cos(angleNot)
			e.Velocity.Y += e.thrust * math.Sin(angleNot)
		} else {
			e.Velocity.X -= e.thrust * math.Cos(angleNot)
			e.Velocity.Y -= e.thrust * math.Sin(angleNot)
		}
	}

	// reset the updates to be performed
	e.Updates = CommandUpdate{}

	// update POSITION
	e.Position.X += halfTimestep * (velocityNot.X + e.Velocity.X)
	e.Position.Y += halfTimestep * (velocityNot.Y + e.Velocity.Y)
}

示例#7
文件: lissajous.go 项目: xaviergustavo/go-book

func lissajous(out io.Writer) {
	const (
		cycles  = 5     // number of complete x oscillator revolutions
		res     = 0.001 // angular resolution
		size    = 100   // image canvas covers [-size..+size]
		nframes = 64    // number of animation frames
		delay   = 8     // delay between frames in 10ms units
	)
	freq := rand.Float64() * 3.0 // relative frequency of y oscillator
	anim := gif.GIF{LoopCount: nframes}
	phase := 0.0 // phase difference
	for i := 0; i < nframes; i++ {
		rect := image.Rect(0, 0, 2*size+1, 2*size+1)
		img := image.NewPaletted(rect, palette)
		index := uint8(rand.Int() % len(palette)) // random palette index color
		if index == 0 {
			index = 1 // change the index if the color is the same as the background color
		}
		for t := 0.0; t < cycles*2*math.Pi; t += res {
			x := math.Sin(t)
			y := math.Sin(t*freq + phase)
			img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), index)
		}
		phase += 0.1
		anim.Delay = append(anim.Delay, delay)
		anim.Image = append(anim.Image, img)
	}
	if err := gif.EncodeAll(out, &anim); err != nil {
		log.Fatal(err)
	}
}

示例#8
文件: server_lissajous.go 项目: standyck/tgpl

func lissajous(out io.Writer) {
	const (
		cycles  = 5     // number of complete x oscillator revolutions
		res     = 0.001 // angular resolution
		size    = 100   // image canvas covers [-size..+size]
		nframes = 64    // number of animation frames
		delay   = 8     // delay between frames in 10ms units
	)

	freq := rand.Float64() * 3.0 // relative frequency of y oscillator
	// gif.GIF is a struct type.  Somehow this allows the struct to be initialized with nframes,
	// the rest are 0 due to some defaulting.
	anim := gif.GIF{LoopCount: nframes}
	phase := 0.0 // phase difference
	for i := 0; i < nframes; i++ {
		rect := image.Rect(0, 0, 2*size+1, 2*size+1)
		img := image.NewPaletted(rect, palette)
		for t := 0.0; t < cycles*2*math.Pi; t += res {
			x := math.Sin(t)
			y := math.Sin(t*freq + phase)
			// BobK:  I have no idea what int(x*size+0.5) is doing
			img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
				blackIndex)
		}
		phase += 0.1
		anim.Delay = append(anim.Delay, delay)
		anim.Image = append(anim.Image, img)
	}
	gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors
}

示例#9
文件: point.go 项目: calendreco/gos2

func TrueCentroid(a, b, c Point) interface{} {
	angleA := b.Angle(c.Vector).Radians()
	angleB := c.Angle(a.Vector).Radians()
	angleC := a.Angle(b.Vector).Radians()
	ra, rb, rc := 1., 1., 1.
	if angleA != 0 {
		ra = angleA / math.Sin(angleA)
	}
	if angleB != 0 {
		rb = angleB / math.Sin(angleB)
	}
	if angleC != 0 {
		rc = angleC / math.Sin(angleC)
	}

	x := r3.Vector{a.X, b.X - a.X, c.X - a.X}
	y := r3.Vector{a.Y, b.Y - a.Y, c.Y - a.Y}
	z := r3.Vector{a.Z, b.Z - a.Z, c.Z - a.Z}
	r := r3.Vector{ra, rb - ra, rc - ra}
	v := r3.Vector{
		y.Cross(z).Dot(r),
		z.Cross(x).Dot(r),
		x.Cross(y).Dot(r),
	}
	return Point{v.Mul(0.5)}
}

示例#10
文件: zerotemp.go 项目: tflovorn/polecalc

// 1/(t0+tz) = 1/N \sum_k (sin(kx) + alpha*sin(ky))^2 / E(k)
func ZeroTempF0AbsError(env Environment) float64 {
	worker := func(k Vector2) float64 {
		sinPart := math.Sin(k.X) + float64(env.Alpha)*math.Sin(k.Y)
		return sinPart * sinPart / ZeroTempPairEnergy(env, k)
	}
	return 1/(env.T0+env.Tz) - Average(env.GridLength, worker)
}

示例#11
文件: structured_light_scan.go 项目: 2emoore4/lcr

func z_triangulation(x int, y int, scan_number int) float64 {
	y = image_size_y - y
	var theta float64 = translate(float64(scan_number), 0.0, float64(frame_count-1), left_theta, right_theta)
	var alpha float64 = translate(float64(scan_number), 0.0, float64(frame_count-1), left_alpha, right_alpha)
	var z float64 = float64(b) * (math.Sin(theta) / math.Sin(alpha+theta))
	return z * (-float64(f))
}

示例#12
文件: zerotemp.go 项目: tflovorn/polecalc

// D1 = -1/(2N) \sum_k (1 - xi(k)/E(k)) * sin(kx) * sin(ky)
func ZeroTempD1AbsError(env Environment) float64 {
	worker := func(k Vector2) float64 {
		sx, sy := math.Sin(k.X), math.Sin(k.Y)
		return -0.5 * (1 - Xi(env, k)/ZeroTempPairEnergy(env, k)) * sx * sy
	}
	return env.D1 - Average(env.GridLength, worker)
}

示例#13
文件: cartconvert.go 项目: hailocab/cartconvert

// Convert Cartesian coordinates to polar.
// The reference ellipsoid is copied verbatim to the result.
// The resulting polar coordinates are in decimal degrees.
// Inspired by http://www.movable-type.co.uk/scripts/latlong-convert-coords.html
func CartesianToPolar(pt *CartPoint) *PolarCoord {

	var gc PolarCoord

	el := pt.El

	esq := (el.a*el.a - el.b*el.b) / (el.a * el.a)
	p := math.Hypot(pt.X, pt.Y)

	lat := math.Atan2(pt.Z, p*(1-esq))
	lat0 := 2.0 * math.Pi

	precision := 4.0 / el.a
	var v float64
	for math.Abs(lat-lat0) > precision {
		v = el.a / math.Sqrt(1-esq*math.Pow(math.Sin(lat), 2))

		lat0 = lat
		lat = math.Atan2(pt.Z+esq*v*math.Sin(lat), p)
	}

	gc.Height = p/math.Cos(lat) - v
	gc.Latitude = radtodeg(lat)
	gc.Longitude = radtodeg(math.Atan2(pt.Y, pt.X))

	gc.El = el

	return &gc
}

示例#14
文件: main.go 项目: perryh/gopl.io

func lissajous(out http.ResponseWriter, r *http.Request) {
	const (
		res     = 0.001 // angular resolution
		size    = 500   // image canvas covers [-size..+size]
		nframes = 64    // number of animation frames
		delay   = 8     // delay between frames in 10ms units
	)
	if err := r.ParseForm(); err != nil {
		os.Exit(1)
	}
	for k, v := range r.Form {
		fmt.Printf("%s:%s\n", k, v)
	}
	cyclesInt, _ := strconv.Atoi(r.Form.Get("cycles")) // number of complete x oscillator revolutions
	cycles := float64(cyclesInt)
	fmt.Printf("Float is: %f\n", cycles)
	freq := rand.Float64() * 3.0 // relative frequency of y oscillator
	anim := gif.GIF{LoopCount: nframes}
	phase := 0.0 // phase difference
	for i := 0; i < nframes; i++ {
		rect := image.Rect(0, 0, 2*size+1, 2*size+1)
		img := image.NewPaletted(rect, palette)
		for t := 0.0; t < cycles*2*math.Pi; t += res {
			x := math.Sin(t)
			y := math.Sin(t*freq + phase)
			img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
				blackIndex)
		}
		phase += 0.1
		anim.Delay = append(anim.Delay, delay)
		anim.Image = append(anim.Image, img)
	}
	gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors
}

示例#15
文件: writter.go 项目: flyinprogrammer/golangbluewhite

// Writter writes a Gif
// @param out a Writter
// @param cycles number of complete x oscillator revolutions
func Writter(out io.Writer, cycles float64) {
	const (
		res     = 0.001 // angular resolution
		size    = 100   // image canvas covers [-size..+size]
		nframes = 64    // number of animation frames
		delay   = 8     // delay between frames in 10ms units
	)
	freq := rand.Float64() * 12.0 // relative frequency of y oscillator
	anim := gif.GIF{LoopCount: nframes}
	phase := 0.0 // phase difference
	for i := 0; i < nframes; i++ {
		rect := image.Rect(0, 0, 2*size+1, 2*size+1)
		img := image.NewPaletted(rect, pallete)
		for t := 0.0; t < cycles*2*math.Pi; t += res {
			x := math.Sin(t)
			y := math.Sin(t*freq + phase)
			if i%2 == 0 {
				img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), greenIndex)
			} else {
				img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), blueIndex)
			}

		}
		phase += 0.1
		anim.Delay = append(anim.Delay, delay)
		anim.Image = append(anim.Image, img)
	}
	gif.EncodeAll(out, &anim)
}

示例#16
文件: lissajous.go 项目: aoeu/d-k

func lissajous(out io.Writer) {
	const (
		cycles  = 5
		res     = 0.001
		size    = 100
		nframes = 128
		delay   = 24
	)
	genGradientPalette(nframes)
	freq := rand.Float64() * 3.0
	anim := gif.GIF{LoopCount: nframes}
	phase := 0.0
	for i := 0; i < nframes; i++ {
		rect := image.Rect(0, 0, 2*size+1, 2*size+1)
		img := image.NewPaletted(rect, palette)
		for t := 0.0; t < cycles*2*math.Pi; t += res {
			x := math.Sin(t)
			y := math.Sin(t*freq + phase)
			img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), uint8(i+1))
		}
		phase += 0.1
		anim.Delay = append(anim.Delay, delay)
		anim.Image = append(anim.Image, img)
	}
	gif.EncodeAll(out, &anim)
}

示例#17
文件: vgpdf.go 项目: jackielii/go-plotinum

// Approximate a circular arc of fewer than π/2
// radians with cubic Bézier curve.
func partialArc(p *pdf.Path, x, y, r vg.Length, a1, a2 float64) {
	a := (a2 - a1) / 2
	x4 := r * vg.Length(math.Cos(a))
	y4 := r * vg.Length(math.Sin(a))
	x1 := x4
	y1 := -y4

	const k = 0.5522847498 // some magic constant
	f := k * vg.Length(math.Tan(a))
	x2 := x1 + f*y4
	y2 := y1 + f*x4
	x3 := x2
	y3 := -y2

	// Rotate and translate points into position.
	ar := a + a1
	sinar := vg.Length(math.Sin(ar))
	cosar := vg.Length(math.Cos(ar))
	x2r := x2*cosar - y2*sinar + x
	y2r := x2*sinar + y2*cosar + y
	x3r := x3*cosar - y3*sinar + x
	y3r := x3*sinar + y3*cosar + y
	x4 = r*vg.Length(math.Cos(a2)) + x
	y4 = r*vg.Length(math.Sin(a2)) + y
	p.Curve(pdfPoint(x2r, y2r), pdfPoint(x3r, y3r), pdfPoint(x4, y4))
}

示例#18
文件: elastic.go 项目: essentialkaos/ek

// ElasticInOut Acceleration until halfway, then deceleration
func ElasticInOut(t, b, c, d float64) float64 {
	if t > d {
		return c
	}

	s := math.SqrtPi
	p := d * (0.3 * 1.5)
	a := c

	if t == 0 {
		return b
	}

	t /= d / 2

	if t == 2 {
		return b + c
	}

	if a < math.Abs(c) {
		s = p / 4
	} else {
		s = p / DoublePi * math.Asin(c/a)
	}

	t--

	if t < 0 {
		return -0.5*(a*math.Pow(2, 10*t)*math.Sin((t*d-s)*DoublePi/p)) + b
	}

	return a*math.Pow(2, -10*t)*math.Sin((t*d-s)*DoublePi/p)*0.5 + c + b
}

示例#19
文件: draw.go 项目: reiver/lifepack

func drawCircle(x, y, r float64) {

	//@TODO: Should speed this up by pre-computing all these cos() and sin() calculations and storing in a lookup table.
	gl.Begin(gl.TRIANGLE_FAN)
	//		gl.Color3d(me.red, me.green, me.blue)

	gl.Vertex2d(x, y)

	pi2 := 2 * math.Pi

	num := 36
	dTheta := pi2 / float64(num)
	theta := float64(0)
	for i := 0; i < num; i++ {

		theta += dTheta

		dx := math.Cos(theta) * r
		dy := math.Sin(theta) * r

		gl.Vertex2d(x+dx, y+dy)
	}

	theta += dTheta

	dx := math.Cos(theta) * r
	dy := math.Sin(theta) * r

	gl.Vertex2d(x+dx, y+dy)

	gl.End()
}

示例#20
文件: camera.go 项目: vron/fm

func getCamPos() gls.Vec3 {
	pos := gls.Vec3{latpt[0], latpt[1], latpt[2]}
	pos[0] += float32(r * math.Cos(phi) * math.Sin(theta))
	pos[1] += float32(r * math.Sin(phi) * math.Sin(theta))
	pos[2] += float32(r * math.Cos(theta))
	return pos
}

示例#21
文件: saturnmoons.go 项目: soniakeys/meeus

func newQs(JDE float64) *qs {
	var q qs
	q.t1 = JDE - 2411093
	q.t2 = q.t1 / 365.25
	q.t3 = (JDE-2433282.423)/365.25 + 1950
	q.t4 = JDE - 2411368
	q.t5 = q.t4 / 365.25
	q.t6 = JDE - 2415020
	q.t7 = q.t6 / 36525
	q.t8 = q.t6 / 365.25
	q.t9 = (JDE - 2442000.5) / 365.25
	q.t10 = JDE - 2409786
	q.t11 = q.t10 / 36525
	q.W0 = 5.095 * d * (q.t3 - 1866.39)
	q.W1 = 74.4*d + 32.39*d*q.t2
	q.W2 = 134.3*d + 92.62*d*q.t2
	q.W3 = 42*d - .5118*d*q.t5
	q.W4 = 276.59*d + .5118*d*q.t5
	q.W5 = 267.2635*d + 1222.1136*d*q.t7
	q.W6 = 175.4762*d + 1221.5515*d*q.t7
	q.W7 = 2.4891*d + .002435*d*q.t7
	q.W8 = 113.35*d - .2597*d*q.t7
	q.s1, q.c1 = math.Sincos(28.0817 * d)
	q.s2, q.c2 = math.Sincos(168.8112 * d)
	q.e1 = .05589 - .000346*q.t7
	q.sW0 = math.Sin(q.W0)
	q.s3W0 = math.Sin(3 * q.W0)
	q.s5W0 = math.Sin(5 * q.W0)
	q.sW1 = math.Sin(q.W1)
	q.sW2 = math.Sin(q.W2)
	q.sW3, q.cW3 = math.Sincos(q.W3)
	q.sW4, q.cW4 = math.Sincos(q.W4)
	q.sW7, q.cW7 = math.Sincos(q.W7)
	return &q
}

示例#22
文件: utils.go 项目: nsec/the-internet

func delayGPS(source string, destination string) (int, error) {
	src := strings.Split(source, "/")
	lat1, err := strconv.ParseFloat(src[0], 64)
	if err != nil {
		return -1, err
	}

	lon1, err := strconv.ParseFloat(src[1], 64)
	if err != nil {
		return -1, err
	}

	dest := strings.Split(destination, "/")
	lat2, err := strconv.ParseFloat(dest[0], 64)
	if err != nil {
		return -1, err
	}

	lon2, err := strconv.ParseFloat(dest[1], 64)
	if err != nil {
		return -1, err
	}

	radius := float64(6371)

	dlat := (lat2 - lat1) / (180 / math.Pi)
	dlon := (lon2 - lon1) / (180 / math.Pi)

	a := math.Sin(dlat/2)*math.Sin(dlat/2) + math.Cos(lat1/(180*math.Pi))*math.Cos(lat2/(180*math.Pi))*math.Sin(dlon/2)*math.Sin(dlon/2)
	c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
	d := radius * c
	speed := float64(139)

	return int(math.Floor(d/speed + 0.5)), nil
}

示例#23
文件: path.go 项目: eswdd/bosun

// ArcTo adds an arc to the path
func (p *Path) ArcTo(cx, cy, rx, ry, startAngle, angle float64) {
	endAngle := startAngle + angle
	clockWise := true
	if angle < 0 {
		clockWise = false
	}
	// normalize
	if clockWise {
		for endAngle < startAngle {
			endAngle += math.Pi * 2.0
		}
	} else {
		for startAngle < endAngle {
			startAngle += math.Pi * 2.0
		}
	}
	startX := cx + math.Cos(startAngle)*rx
	startY := cy + math.Sin(startAngle)*ry
	if len(p.Components) > 0 {
		p.LineTo(startX, startY)
	} else {
		p.MoveTo(startX, startY)
	}
	p.appendToPath(ArcToCmp, cx, cy, rx, ry, startAngle, angle)
	p.x = cx + math.Cos(endAngle)*rx
	p.y = cy + math.Sin(endAngle)*ry
}

示例#24
文件: osgridconverter.go 项目: thingful/osgridconverter

// toCartesian converts lat/lon coordinates to cartesian x/y/z coordinates.
// It returns a vector representing a lat/lon point on x-y-z axes in metres
// from the earth centre.
func toCartesian(coords Coordinates, datum Datum) Vector3d {

	aa := datum.a
	bb := datum.b

	φ := toRadians(coords.Lat)
	λ := toRadians(coords.Lon)
	sinφ := math.Sin(φ)
	cosφ := math.Cos(φ)
	sinλ := math.Sin(λ)
	cosλ := math.Cos(λ)

	eSq := (aa*aa - bb*bb) / (aa * aa) // make a and b const datum properties
	ν := aa / math.Sqrt(1-eSq*sinφ*sinφ)
	x := ν * cosφ * cosλ
	y := ν * cosφ * sinλ
	z := (1 - eSq) * ν * sinφ

	// apply Helmert transform
	// move to another function
	rx := toRadians((datum.rx * -1) / 3600) // normalise seconds to radians
	ry := toRadians((datum.ry * -1) / 3600) // normalise seconds to radians
	rz := toRadians((datum.rz * -1) / 3600) // normalise seconds to radians
	s1 := (datum.s*-1)/1e6 + 1              // normalise ppm to (s+1)

	// apply transform
	x2 := (datum.tx * -1) + x*s1 - y*rz + z*ry
	y2 := (datum.ty * -1) + x*rz + y*s1 - z*rx
	z2 := (datum.tz * -1) - x*ry + y*rx + z*s1

	// instantiate new vector
	vect := Vector3d{x: x2, y: y2, z: z2}

	return vect
}

示例#25
文件: lissajous-server.go 项目: jimlawton/gopl

func lissajous(out io.Writer) {
	palette := []color.Color{color.Black}
	for i := 0; i < nFrames; i++ {
		red := (uint8)((i * 255) / nFrames)
		green := (uint8)(((i + nFrames/2) * 255) / nFrames)
		blue := (uint8)(((nFrames/2 - i) * 255) / nFrames)
		palette = append(palette, color.RGBA{red, green, blue, 0xff})
	}
	freq := rand.Float64() * 3.0 // relative frequency of y oscillator
	anim := gif.GIF{LoopCount: nFrames}
	phase := 0.0
	for i := 0; i < nFrames; i++ {
		rect := image.Rect(0, 0, 2*size+1, 2*size+1)
		img := image.NewPaletted(rect, palette)
		for t := 0.0; t < cycles*2*math.Pi; t += res {
			x := math.Sin(t)
			y := math.Sin(t*freq + phase)
			xcoord := size + int(x*(float64)(size)+0.5)
			ycoord := size + int(y*(float64)(size)+0.5)
			img.SetColorIndex(xcoord, ycoord, (uint8)(i+1))
		}
		phase += 0.1
		anim.Delay = append(anim.Delay, delay)
		anim.Image = append(anim.Image, img)
	}
	gif.EncodeAll(out, &anim)
}

示例#26
文件: linear_test.go 项目: runningwild/linear

func ComplexOperationsSpec(c gospec.Context) {
	c.Specify("Vec2.DistToLine() works.", func() {
		centers := []linear.Vec2{
			linear.Vec2{10, 12},
			linear.Vec2{1, -9},
			linear.Vec2{-100, -42},
			linear.Vec2{0, 1232},
		}
		radiuses := []float64{3, 10.232, 435, 1}
		thetas := []float64{0.001, 0.1, 1, 1.01, 0.034241, 0.789, 90, 179, 180}
		angles := []float64{1.01, 1.0, 1.11111, 930142}
		for _, center := range centers {
			for _, radius := range radiuses {
				for _, angle := range angles {
					for _, theta := range thetas {
						a := linear.Vec2{math.Cos(angle), math.Sin(angle)}
						b := linear.Vec2{math.Cos(angle + theta), math.Sin(angle + theta)}
						seg := linear.Seg2{a.Scale(radius).Add(center), b.Scale(radius).Add(center)}
						dist := center.DistToLine(seg)
						real_dist := radius * math.Cos(theta/2)
						if real_dist < 0 {
							real_dist = -real_dist
						}
						c.Expect(dist, IsWithin(1e-9), real_dist)
					}
				}
			}
		}
	})
}

示例#27
文件: geometry.go 项目: maleck13/jigsaws

// Arc draws an arc with a positive angle (clockwise)
func Arc(gc draw2d.GraphicContext, xc, yc, width, height float64) {
	// draw an arc
	xc += width / 2
	yc += height / 2
	radiusX, radiusY := width/2, height/2
	startAngle := 45 * (math.Pi / 180.0) /* angles are specified */
	angle := 135 * (math.Pi / 180.0)     /* clockwise in radians           */
	gc.SetLineWidth(width / 10)
	gc.SetLineCap(draw2d.ButtCap)
	gc.SetStrokeColor(image.Black)
	gc.MoveTo(xc+math.Cos(startAngle)*radiusX, yc+math.Sin(startAngle)*radiusY)
	gc.ArcTo(xc, yc, radiusX, radiusY, startAngle, angle)
	gc.Stroke()

	// fill a circle
	gc.SetStrokeColor(color.NRGBA{255, 0x33, 0x33, 0x80})
	gc.SetFillColor(color.NRGBA{255, 0x33, 0x33, 0x80})
	gc.SetLineWidth(width / 20)

	gc.MoveTo(xc+math.Cos(startAngle)*radiusX, yc+math.Sin(startAngle)*radiusY)
	gc.LineTo(xc, yc)
	gc.LineTo(xc-radiusX, yc)
	gc.Stroke()

	gc.MoveTo(xc, yc)
	gc.ArcTo(xc, yc, width/10.0, height/10.0, 0, 2*math.Pi)
	gc.Fill()
}

示例#28
文件: fftw_test.go 项目: jtc25/go-fftw

func FFTR2CSpec(c gospec.Context) {
	signal := make([]float64, 16)
	F_signal := make([]complex128, 9)

	for i := range signal {
		signal[i] = math.Sin(float64(i) / float64(len(signal)) * math.Pi * 2)
	}
	forward := PlanDftR2C1d(signal, F_signal, Estimate)
	forward.Execute()
	c.Specify("Running a R2C transform doesn't destroy the input.", func() {
		for i := range signal {
			c.Expect(signal[i], gospec.Equals, math.Sin(float64(i)/float64(len(signal))*math.Pi*2))
		}
	})

	c.Specify("Forward 1d Real to Complex FFT  works properly.", func() {
		c.Expect(real(F_signal[0]), gospec.IsWithin(1e-9), 0.0)
		c.Expect(imag(F_signal[0]), gospec.IsWithin(1e-9), 0.0)
		c.Expect(real(F_signal[1]), gospec.IsWithin(1e-9), 0.0)
		c.Expect(imag(F_signal[1]), gospec.IsWithin(1e-9), -float64(len(signal))/2)
		for i := 2; i < len(F_signal)-1; i++ {
			c.Expect(real(F_signal[i]), gospec.IsWithin(1e-9), 0.0)
			c.Expect(imag(F_signal[i]), gospec.IsWithin(1e-9), 0.0)
		}
	})
}

示例#29
文件: ellipsoid.go 项目: peter-edge/flights-go

/* ToECEF takes the latitude, longitude, elevation list and
   returns three cartesian coordinates x, y, z */
func (ellipsoid Ellipsoid) ToECEF(lat1, lon1, alt1 float64) (x, y, z float64) {
	a := ellipsoid.Ellipse.Equatorial
	f := 1 / ellipsoid.Ellipse.InvFlattening

	b := a * (1.0 - f)
	e := math.Sqrt((a*a - b*b) / (a * a))
	esq := e * e // e squared

	if ellipsoid.Units == Degrees {
		lat1 = deg2rad(lat1)
		lon1 = deg2rad(lon1)
	}

	h := alt1 // renamed for convenience
	phi := lat1
	lambda := lon1

	cphi := math.Cos(phi)
	sphi := math.Sin(phi)
	sphisq := sphi * sphi
	clam := math.Cos(lambda)
	slam := math.Sin(lambda)
	N := a / (math.Sqrt(1 - esq*sphisq))
	x = (N + h) * cphi * clam
	y = (N + h) * cphi * slam
	z = ((b*b*N)/(a*a) + h) * sphi

	return x, y, z
}

示例#30
文件: rect.go 项目: calendreco/gos2

// Area returns the surface area of the Rect.
func (r Rect) Area() float64 {
	if r.IsEmpty() {
		return 0
	}
	capDiff := math.Abs(math.Sin(r.Lat.Hi) - math.Sin(r.Lat.Lo))
	return r.Lng.Length() * capDiff
}

展开阅读全文