mirror of
https://github.com/make-42/xyosc
synced 2024-11-22 17:20:09 +01:00
fix: add extra fast sqrt stuff
This commit is contained in:
parent
61ab70dd2b
commit
54bb20aa07
27
fastsqrt/fastsqrt.go
Normal file
27
fastsqrt/fastsqrt.go
Normal file
@ -0,0 +1,27 @@
|
||||
package fastsqrt
|
||||
|
||||
import "math"
|
||||
|
||||
const magic32 = 0x5F375A86
|
||||
|
||||
func FastInvSqrt32(n float32) float32 {
|
||||
// If n is negative return NaN
|
||||
if n < 0 {
|
||||
return float32(math.NaN())
|
||||
} // n2 and th are for one iteration of Newton's method later
|
||||
n2, th := n*0.5, float32(1.5) // Use math.Float32bits to represent the float32, n, as
|
||||
// an uint32 without modification.
|
||||
b := math.Float32bits(n) // Use the new uint32 view of the float32 to shift the bits
|
||||
// of the float32 1 to the right, chopping off 1 bit from
|
||||
// the fraction part of the float32.
|
||||
b = magic32 - (b >> 1) // Use math.Float32frombits to convert the uint32 bits back
|
||||
// into their float32 representation, again no actual change
|
||||
// in the bits, just a change in how we treat them in memory.
|
||||
// f is now our answer of 1 / sqrt(n)
|
||||
f := math.Float32frombits(b) // Perform one iteration of Newton's method on f to improve
|
||||
// accuracy
|
||||
f *= th - (n2 * f * f)
|
||||
|
||||
// And return our fast inverse square root result
|
||||
return f
|
||||
}
|
2
main.go
2
main.go
@ -44,6 +44,8 @@ func main() {
|
||||
fAY := -float32(AY) * config.Config.Gain * float32(scale)
|
||||
fBX := float32(BX) * config.Config.Gain * float32(scale)
|
||||
fBY := -float32(BY) * config.Config.Gain * float32(scale)
|
||||
//inv := fastsqrt.FastInvSqrt32((fBX-fAX)*(fBX-fAX) + (fBY-fBY)*(fBY-fBY))
|
||||
//colorAdjusted := color.RGBA{config.AccentColor.R, config.AccentColor.G, config.AccentColor.B, uint8(255 * inv * config.Config.LineOpacity)}
|
||||
rl.DrawLineEx(rl.NewVector2(float32(config.Config.WindowWidth/2)+fAX, float32(config.Config.WindowWidth/2)+fAY), rl.NewVector2(float32(config.Config.WindowWidth/2)+fBX, float32(config.Config.WindowWidth/2)+fBY), config.Config.LineThickness, config.AccentColor)
|
||||
AX = BX
|
||||
AY = BY
|
||||
|
Loading…
Reference in New Issue
Block a user