//GridToGeodetic converts RT90 coordinates to WGS84
func GridToGeodetic(x, y float64) (float64, float64) {

	if CentralMeridian == 31337.0 {
		return 0.0, 0.0
	}

	e2 := Flattening * (2.0 - Flattening)
	n := Flattening / (2.0 - Flattening)
	a_roof := Axis / (1.0 + n) * (1.0 + n*n/4.0 + n*n*n*n/64.0)
	delta1 := n/2.0 - 2.0*n*n/3.0 + 37.0*n*n*n/96.0 - n*n*n*n/360.0
	delta2 := n*n/48.0 + n*n*n/15.0 - 437.0*n*n*n*n/1440.0
	delta3 := 17.0*n*n*n/480.0 - 37*n*n*n*n/840.0
	delta4 := 4397.0 * n * n * n * n / 161280.0

	Astar := e2 + e2*e2 + e2*e2*e2 + e2*e2*e2*e2
	Bstar := -(7.0*e2*e2 + 17.0*e2*e2*e2 + 30.0*e2*e2*e2*e2) / 6.0
	Cstar := (224.0*e2*e2*e2 + 889.0*e2*e2*e2*e2) / 120.0
	Dstar := -(4279.0 * e2 * e2 * e2 * e2) / 1260.0

	DegToRad := math.Pi / 180
	LambdaZero := CentralMeridian * DegToRad
	xi := (x - FalseNorthing) / (Scale * a_roof)
	eta := (y - FalseEasting) / (Scale * a_roof)
	xi_prim := xi - delta1*math.Sin(2.0*xi)*math.Cosh(2.0*eta) - delta2*math.Sin(4.0*xi)*math.Cosh(4.0*eta) - delta3*math.Sin(6.0*xi)*math.Cosh(6.0*eta) - delta4*math.Sin(8.0*xi)*math.Cosh(8.0*eta)
	eta_prim := eta - delta1*math.Cos(2.0*xi)*math.Sinh(2.0*eta) - delta2*math.Cos(4.0*xi)*math.Sinh(4.0*eta) - delta3*math.Cos(6.0*xi)*math.Sinh(6.0*eta) - delta4*math.Cos(8.0*xi)*math.Sinh(8.0*eta)
	phi_star := math.Asin(math.Sin(xi_prim) / math.Cosh(eta_prim))
	delta_lambda := math.Atan(math.Sinh(eta_prim) / math.Cos(xi_prim))
	lon_radian := LambdaZero + delta_lambda
	lat_radian := phi_star + math.Sin(phi_star)*math.Cos(phi_star)*(Astar+Bstar*math.Pow(math.Sin(phi_star), 2)+Cstar*math.Pow(math.Sin(phi_star), 4)+Dstar*math.Pow(math.Sin(phi_star), 6))

	return lat_radian * 180.0 / math.Pi, lon_radian * 180.0 / math.Pi
}
Exemple #2
0
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()
}
Exemple #3
0
// 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()
}
Exemple #4
0
// Returns a Point populated with the lat and lng coordinates of transposing the origin point the distance (in meters) supplied by the compass bearing (in degrees) supplied.
// Original Implementation from: http://www.movable-type.co.uk/scripts/latlong.html
func (p *Point) PointAtDistanceAndBearing(dist float64, bearing float64) *Point {

	dr := dist / EARTH_RADIUS

	bearing = (bearing * (math.Pi / 180.0))

	lat1 := (p.lat * (math.Pi / 180.0))
	lng1 := (p.lng * (math.Pi / 180.0))

	lat2_part1 := math.Sin(lat1) * math.Cos(dr)
	lat2_part2 := math.Cos(lat1) * math.Sin(dr) * math.Cos(bearing)

	lat2 := math.Asin(lat2_part1 + lat2_part2)

	lng2_part1 := math.Sin(bearing) * math.Sin(dr) * math.Cos(lat1)
	lng2_part2 := math.Cos(dr) - (math.Sin(lat1) * math.Sin(lat2))

	lng2 := lng1 + math.Atan2(lng2_part1, lng2_part2)
	lng2 = math.Mod((lng2+3*math.Pi), (2*math.Pi)) - math.Pi

	lat2 = lat2 * (180.0 / math.Pi)
	lng2 = lng2 * (180.0 / math.Pi)

	return &Point{lat: lat2, lng: lng2}
}
Exemple #5
0
// 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))
}
Exemple #6
0
func (m *mbody) Initialize() (y0 []float64) {
	n := len(m.mass)
	y0 = make([]float64, n*6)

	rf1 := 4 * math.Pi / 8
	rf2 := 2 * math.Pi / float64(n)

	for i := range m.mass {
		i1 := float64(i + 1)
		m.mass[i] = (0.3 + 0.1*(math.Cos(i1*rf1)+1.0)) / float64(n)
		rad := 1.7 + math.Cos(i1*0.75)
		v := 0.22 * math.Sqrt(rad)
		ci := math.Cos(i1 * rf2)
		si := math.Sin(i1 * rf2)

		ip := 6 * i
		y0[ip] = rad * ci
		y0[ip+1] = rad * si
		y0[ip+2] = 0.4 * si
		y0[ip+3] = -v * si
		y0[ip+4] = v * ci
		y0[ip+5] = 0
	}
	return
}
Exemple #7
0
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
}
Exemple #8
0
func gradient(x float64, y float64) (xd float64, yd float64) {
	xd = (2 * x * math.Cos(math.Pow(x, 2)+math.Pow(y, 2)) * math.Cos(y+math.Exp(x))) -
		(math.Exp(x) * math.Sin(math.Pow(x, 2)+math.Pow(y, 2)) * math.Sin(y+math.Exp(x)))
	yd = (2 * y * math.Cos(math.Pow(x, 2)+math.Pow(y, 2)) * math.Cos(y+math.Exp(x))) -
		(math.Sin(math.Pow(x, 2)+math.Pow(y, 2)) * math.Sin(y+math.Exp(x)))
	return
}
Exemple #9
0
func (c *OutlineCone) Paths() Paths {
	center := Vector{0, 0, 0}
	hyp := center.Sub(c.Eye).Length()
	opp := c.Radius
	theta := math.Asin(opp / hyp)
	adj := opp / math.Tan(theta)
	d := math.Cos(theta) * adj
	// r := math.Sin(theta) * adj
	w := center.Sub(c.Eye).Normalize()
	u := w.Cross(c.Up).Normalize()
	c0 := c.Eye.Add(w.MulScalar(d))
	a0 := c0.Add(u.MulScalar(c.Radius * 1.01))
	b0 := c0.Add(u.MulScalar(-c.Radius * 1.01))

	var p0 Path
	for a := 0; a < 360; a++ {
		x := c.Radius * math.Cos(Radians(float64(a)))
		y := c.Radius * math.Sin(Radians(float64(a)))
		p0 = append(p0, Vector{x, y, 0})
	}
	return Paths{
		p0,
		{{a0.X, a0.Y, 0}, {0, 0, c.Height}},
		{{b0.X, b0.Y, 0}, {0, 0, c.Height}},
	}
}
Exemple #10
0
// AssignEulerRotation assigns Euler angle rotations to the rotation part of the matrix and sets the remaining elements to their ident value.
func (mat *T) AssignEulerRotation(yHead, xPitch, zRoll float64) *T {
	sinH := math.Sin(yHead)
	cosH := math.Cos(yHead)
	sinP := math.Sin(xPitch)
	cosP := math.Cos(xPitch)
	sinR := math.Sin(zRoll)
	cosR := math.Cos(zRoll)

	mat[0][0] = cosR*cosH - sinR*sinP*sinH
	mat[1][0] = -sinR * cosP
	mat[2][0] = cosR*sinH + sinR*sinP*cosH
	mat[3][0] = 0

	mat[0][1] = sinR*cosH + cosR*sinP*sinH
	mat[1][1] = cosR * cosP
	mat[2][1] = sinR*sinH - cosR*sinP*cosH
	mat[3][1] = 0

	mat[0][2] = -cosP * sinH
	mat[1][2] = sinP
	mat[2][2] = cosP * cosH
	mat[3][2] = 0

	mat[0][3] = 0
	mat[1][3] = 0
	mat[2][3] = 0
	mat[3][3] = 1

	return mat
}
Exemple #11
0
func readSphereNormal(r io.Reader) (Vec3, error) {
	var result Vec3
	var zenith uint8
	var azimuth uint8
	var err error

	zenith, err = readU8(r)
	if err != nil {
		return result, err
	}

	azimuth, err = readU8(r)
	if err != nil {
		return result, err
	}

	latitude := float64(zenith) * (math.Pi * 2.0) / 255.0
	longitude := float64(azimuth) * (math.Pi * 2.0) / 255.0
	latsin := math.Sin(latitude)

	result.X = float32(math.Cos(longitude) * latsin)
	result.Y = float32(math.Sin(longitude) * latsin)
	result.Z = float32(math.Cos(latitude))

	return result, nil
}
Exemple #12
0
func distance(p1, p2 location) float64 {
	var dlat float64 = p1.lat - p2.lat
	var dlong float64 = p1.long - p2.long

	var a float64 = math.Pow(math.Sin(dlat/2), 2) + math.Cos(p1.lat)*math.Cos(p2.lat)*math.Pow(math.Sin(dlong/2), 2)
	return 2 * R * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
}
Exemple #13
0
func (i img) drawCurve(c curve) {
	red := byte((c.color >> 16))
	green := byte((c.color >> 8))
	blue := byte(c.color)

	startAngle := c.start * 2 * math.Pi
	angle := (c.end - c.start) * 2 * math.Pi
	endAngle := startAngle + angle
	radius := float64(c.level * i.levelWidth)
	radiusOuter := radius + float64(i.levelWidth)
	i.ctx.SetStrokeColor(color.RGBA{0, 0, 0, 0})
	i.ctx.SetFillColor(color.RGBA{red, green, blue, 0xFF})

	i.ctx.MoveTo(
		i.centerX+math.Cos(startAngle)*radius,
		i.centerY+math.Sin(startAngle)*radius,
	)
	i.ctx.ArcTo(i.centerX, i.centerY, radius, radius, startAngle, angle)

	i.ctx.LineTo(
		i.centerX+math.Cos(endAngle)*radiusOuter,
		i.centerY+math.Sin(endAngle)*radiusOuter,
	)
	i.ctx.ArcTo(i.centerX, i.centerY, radiusOuter, radiusOuter, endAngle, -angle)

	i.ctx.LineTo(
		i.centerX+math.Cos(startAngle)*radius,
		i.centerY+math.Sin(startAngle)*radius,
	)

	i.ctx.FillStroke()
}
// from: http://stackoverflow.com/questions/7316963/drawing-a-circle-google-static-maps
func (t *MealServlet) generate_polyline_code(lat, lng float64) string {
	r_earth := 6371000
	pi := math.Pi

	lat = (lat * pi) / 180
	lng = (lng * pi) / 180
	d := float64(250) / float64(r_earth) // diameter of circle

	points := make([]*Point, 0)
	// generate the points. Adjust granularity using the incrementor
	for i := 0; i <= 360; i++ {
		brng := float64(i) * pi / float64(180)
		log.Println(i)
		p_lat := math.Asin(math.Sin(lat)*math.Cos(d) + math.Cos(lat)*math.Sin(d)*math.Cos(brng))
		p_lng := ((lng + math.Atan2(math.Sin(brng)*math.Sin(d)*math.Cos(lat),
			math.Cos(d)-math.Sin(lat)*math.Sin(p_lat))) * 180) / pi
		p_lat = (p_lat * 180) / pi
		point := new(Point)
		point.Lat = p_lat
		point.Lng = p_lng
		log.Println(p_lat)
		log.Println(p_lng)
		points = append(points, point)
	}
	return EncodePolyline(points)
}
Exemple #15
0
// RotY returns a rotation matrix rotating r degrees around the y axis.
func RotY(r float64) Mat4 {
	r *= deg
	return Mat4{[4]float64{math.Cos(r), 0, math.Sin(r), 0},
		[4]float64{0, 1, 0, 0},
		[4]float64{-math.Sin(r), 0, math.Cos(r), 0},
		[4]float64{0, 0, 0, 1}}
}
Exemple #16
0
func argsCheckAndAddColor(org float32, index int, anivalue float32) float32 {
	if len(os.Args) < index*2+1 {
		return org
	}
	targ, err := strconv.ParseFloat(os.Args[index*2], 32)
	if err != nil {
		return org
	}
	anivalue += 1
	switch os.Args[index*2-1] {
	case "+":
		return org + float32(targ)*anivalue
	case "*":
		return org * float32(targ) * anivalue
	case "/":
		return org / (float32(targ) * anivalue)
	case "^":
		return float32(math.Pow(float64(org), targ*float64(anivalue)))
	case "sin+":
		return 255 * float32(math.Sin((float64(org)+targ*float64(anivalue))/255*math.Pi*2))
	case "sin*":
		return 255 * float32(math.Sin((float64(org)*targ*float64(anivalue))/255*math.Pi*2))
	case "cos+":
		return 255 * float32(math.Cos((float64(org)+targ*float64(anivalue))/255*math.Pi*2))
	case "cos*":
		return 255 * float32(math.Cos((float64(org)*targ*float64(anivalue))/255*math.Pi*2))
	case "tan+":
		return 255 * float32(math.Tan((float64(org)+targ*float64(anivalue))/255*math.Pi*2))
	case "tan*":
		return 255 * float32(math.Tan((float64(org)*targ*float64(anivalue))/255*math.Pi*2))
	}
	return org
}
Exemple #17
0
func Cylinder(r, h gl.Float, slices int, hollow, solid bool) {
	res := 2 * math.Pi / float64(slices)
	mode := gl.LINE_LOOP
	if solid {
		mode = gl.QUADS
	}
	gl.Begin(gl.Enum(mode))
	for a := 0.0; a < 2*math.Pi; a += res {
		gl.Normal3f(gl.Float(math.Cos(a)), gl.Float(math.Sin(a)), 0)
		gl.Vertex3f(r*gl.Float(math.Cos(a)), r*gl.Float(math.Sin(a)), 0)
		gl.Vertex3f(r*gl.Float(math.Cos(a)), r*gl.Float(math.Sin(a)), h)
		a += res
		gl.Vertex3f(r*gl.Float(math.Cos(a)), r*gl.Float(math.Sin(a)), h)
		gl.Vertex3f(r*gl.Float(math.Cos(a)), r*gl.Float(math.Sin(a)), 0)
	}
	gl.End()
	if !hollow {
		// X Y plane
		if h < 0 {
			gl.Normal3f(0, 0, 1)
		} else {
			gl.Normal3f(0, 0, -1)
		}
		Circle(r, slices, solid)
		// Top (or bottom)
		if h < 0 {
			gl.Normal3f(0, 0, -1)
		} else {
			gl.Normal3f(0, 0, 1)
		}
		gl.Translatef(0, 0, h)
		Circle(r, slices, solid)
	}
}
Exemple #18
0
// Distance returns the approximate distance from another point in kilometers.
func (p Point) Distance(p2 Point) float64 {
	r := 6371.01
	return math.Acos((math.Sin(p.RadLat())*
		math.Sin(p2.RadLat()))+
		(math.Cos(p.RadLat())*math.Cos(p2.RadLat())*
			math.Cos(p.RadLon()-p2.RadLon()))) * r
}
Exemple #19
0
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
}
Exemple #20
0
// GCJ-02转百度坐标
func GCJ02_BD09(longitudeStr, latitudeStr string) (lon, lat string, err error) {
	longitude, err1 := strconv.ParseFloat(longitudeStr, 64)
	latitude, err2 := strconv.ParseFloat(latitudeStr, 64)

	if err1 != nil {
		err = err1
		return
	}

	if err2 != nil {
		err = err2
		return
	}

	xPI := 3.14159265358979324 * 3000.0 / 180.0
	x := longitude
	y := latitude
	z := math.Sqrt(x*x+y*y) + 0.00002*math.Sin(y*xPI)
	t := math.Atan2(y, x) + 0.000003*math.Cos(x*xPI)
	newLon := z*math.Cos(t) + 0.0065
	newLat := z*math.Sin(t) + 0.006

	lon = strconv.FormatFloat(newLon, 'f', 10, 64) //  取10个小位数
	lat = strconv.FormatFloat(newLat, 'f', 10, 64)
	return
}
// 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
}
Exemple #22
0
func (cs *CoordinateSystem) GeodeticToGeocentric(plh *globals.PLH) *globals.XYZ {
	xyz := &globals.XYZ{}
	if _, ok := cs.FloatParams["e2"]; !ok {
		cs.SetGeocentricParameters()
	}
	e2 := cs.FloatParams["e2"]
	a := cs.FloatParams["a_orig"]
	if plh.Phi < -globals.PI_OVER_2 && plh.Phi > -1.001*globals.PI_OVER_2 {
		plh.Phi = -globals.PI_OVER_2
	} else if plh.Phi > globals.PI_OVER_2 && plh.Phi < 1.001*globals.PI_OVER_2 {
		plh.Phi = globals.PI_OVER_2
	}

	if plh.Lam > globals.PI {
		plh.Lam = plh.Lam - 2*globals.PI
	}
	sin_lat := math.Sin(plh.Phi)
	cos_lat := math.Cos(plh.Phi)
	sin2_lat := sin_lat * sin_lat
	rn := a / (math.Sqrt(1.0 - e2*sin2_lat))
	xyz.X = (rn + plh.Height) * cos_lat * math.Cos(plh.Lam)
	xyz.Y = (rn + plh.Height) * cos_lat * math.Sin(plh.Lam)
	xyz.Z = ((rn * (1 - e2)) + plh.Height) * sin_lat
	return xyz
}
Exemple #23
0
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)
					}
				}
			}
		}
	})
}
Exemple #24
0
func arc(t VertexConverter, x, y, rx, ry, start, angle, scale float64) (lastX, lastY float64) {
	end := start + angle
	clockWise := true
	if angle < 0 {
		clockWise = false
	}
	ra := (math.Abs(rx) + math.Abs(ry)) / 2
	da := math.Acos(ra/(ra+0.125/scale)) * 2
	//normalize
	if !clockWise {
		da = -da
	}
	angle = start + da
	var curX, curY float64
	for {
		if (angle < end-da/4) != clockWise {
			curX = x + math.Cos(end)*rx
			curY = y + math.Sin(end)*ry
			return curX, curY
		}
		curX = x + math.Cos(angle)*rx
		curY = y + math.Sin(angle)*ry

		angle += da
		t.Vertex(curX, curY)
	}
	return curX, curY
}
Exemple #25
0
// 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)
}
Exemple #26
0
func arcAdder(adder raster.Adder, x, y, rx, ry, start, angle, scale float64) raster.Point {
	end := start + angle
	clockWise := true
	if angle < 0 {
		clockWise = false
	}
	ra := (math.Abs(rx) + math.Abs(ry)) / 2
	da := math.Acos(ra/(ra+0.125/scale)) * 2
	//normalize
	if !clockWise {
		da = -da
	}
	angle = start + da
	var curX, curY float64
	for {
		if (angle < end-da/4) != clockWise {
			curX = x + math.Cos(end)*rx
			curY = y + math.Sin(end)*ry
			return raster.Point{raster.Fix32(curX * 256), raster.Fix32(curY * 256)}
		}
		curX = x + math.Cos(angle)*rx
		curY = y + math.Sin(angle)*ry

		angle += da
		adder.Add1(raster.Point{raster.Fix32(curX * 256), raster.Fix32(curY * 256)})
	}
	return raster.Point{raster.Fix32(curX * 256), raster.Fix32(curY * 256)}
}
Exemple #27
0
// 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
}
Exemple #28
0
func (self *Picker) DrawPlayerItems(t int64) {

	gl.PushMatrix()
	gl.LoadIdentity()

	for i := 0; i < 5; i++ {

		item := ThePlayer.equippedItems[i]
		if item != ITEM_NONE {
			angle := -(float64(i) + 1.5) * math.Pi / 4
			gl.LoadIdentity()
			x := self.x - self.actionItemRadius*float32(math.Sin(angle))
			y := self.y + self.actionItemRadius*float32(math.Cos(angle))
			gl.Translatef(x, y, 0)

			gl.Rotatef(360*float32(math.Sin(float64(t)/1e10+float64(i))), 1.0, 0.0, 0.0)
			gl.Rotatef(360*float32(math.Cos(float64(t)/1e10+float64(i))), 0.0, 1.0, 0.0)
			gl.Rotatef(360*float32(math.Sin(float64(t)/1e10+float64(i))), 0.0, 0.0, 1.0)
			gl.Scalef(blockscale, blockscale, blockscale)
			gVertexBuffer.Reset()
			TerrainCube(gVertexBuffer, 0, 0, 0, [18]uint16{}, item, FACE_NONE)
			gVertexBuffer.RenderDirect(false)

			gl.LoadIdentity()
			gl.Translatef(x-17*PIXEL_SCALE, y-19*PIXEL_SCALE, 20)
			consoleFont.Print(fmt.Sprintf("%d", ThePlayer.inventory[item]))

		}
	}
	gl.PopMatrix()

}
Exemple #29
0
/* 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
}
/*
	SphericalDistance calculates the distance (in meters) between two WGS84 points
*/
func SphericalDistance(lat1, lon1, lat2, lon2 float64) int {

	// Convert latitude and longitude to
	// spherical coordinates in radians.
	degrees_to_radians := math.Pi / 180.0

	// phi = 90 - latitude
	phi1 := (90.0 - lat1) * degrees_to_radians
	phi2 := (90.0 - lat2) * degrees_to_radians

	// theta = longitude
	theta1 := lon1 * degrees_to_radians
	theta2 := lon2 * degrees_to_radians

	/*
	   Compute spherical distance from spherical coordinates.

	   For two locations in spherical coordinates
	   	(1, theta, phi) and (1, theta, phi)
	   	cosine( arc length ) =
	   		sin phi sin phi' cos(theta-theta') + cos phi cos phi'
	   	distance = rho * arc length
	*/
	cos := (math.Sin(phi1)*math.Sin(phi2)*math.Cos(theta1-theta2) +
		math.Cos(phi1)*math.Cos(phi2))
	arc := math.Acos(cos)

	// Remember to multiply arc by the radius of the earth
	//in your favorite set of units to get length.
	return int(6373 * 1000 * arc)
}