Permalink
Browse files

Added Angle:SnapTo( component, degrees ) where component is p/y/r / p…

…itch/yaw/roll, and degrees is the interval; will return an Angle for all cases so multiple snaps can be strung on one line.

Examples, Angle( 0, 92, 0 ):SnapTo( "y", 90 ); will return Angle( 0, 90, 0 );
Angle( 0, 115, 0 ):SnapTo( "y", 45 ); will return Angle( 0, 137, 0 );
Angle( 12, 98, 167 ):SnapTo( "p", 30 ):SnapTo( "y", 45 ):SnapTo( "r", 45 ); will return Angle( 0, 90, -180 );

Added Vector:ToColor( ) to compliment other addition Color:ToVector( )
Added Color:ToVector( ) to compliment other addition Vector:ToColor( )

3 simple yet useful functions; and we may as well add to the 3 meta files...
  • Loading branch information...
1 parent a224dfd commit 6cc5352ebc2efb3a01bcb2cb05476c49097558d7 @Acecool Acecool committed Jun 23, 2014
@@ -1,3 +1,18 @@
local meta = FindMetaTable( "Angle" )
-- Nothing in here, still leaving this file here just in case
+
+--[[---------------------------------------------------------
+ Angle Snap to nearest interval of degrees
+-----------------------------------------------------------]]
+function meta:SnapTo( component, degrees )
+
+ if ( degrees == 0 ) then ErrorNoHalt( "The snap degrees must be non-zero.\n" ); return self; end
+ if ( !self[ component ] ) then ErrorNoHalt( "You must choose a valid component of Angle( p || pitch, y || yaw, r || roll ) to snap such as Angle( 80, 40, 30 ):SnapTo( \"p\", 90 ):SnapTo( \"y\", 45 ):SnapTo( \"r\", 40 ); and yes, you can keep adding snaps.\n" ); return self; end
+
+ self[ component ] = math.Round( self[ component ] / degrees ) * degrees
+ self[ component ] = math.NormalizeAngle( self[ component ]
+
+ return self
+
+end
@@ -1,3 +1,12 @@
local meta = FindMetaTable( "Vector" )
-- Nothing in here, still leaving this file here just in case
+
+--[[---------------------------------------------------------
+ Converts Vector To Color - alpha precision lost, must reset
+-----------------------------------------------------------]]
+function meta:ToColor( )
+
+ return Vector( self.x * 255, self.y * 255, self.z * 255, 255 )
+
+end
@@ -1,20 +1,19 @@
-
local COLOR = {}
-local COLOR_M = { __index = COLOR }
+COLOR.__index = COLOR
--[[---------------------------------------------------------
Register our metatable to make it accessible using FindMetaTable
-----------------------------------------------------------]]
-debug.getregistry().Color = COLOR_M
+debug.getregistry().Color = COLOR
--[[---------------------------------------------------------
To easily create a colour table
-----------------------------------------------------------]]
function Color( r, g, b, a )
a = a or 255
- return setmetatable( { r = math.min( tonumber(r), 255 ), g = math.min( tonumber(g), 255 ), b = math.min( tonumber(b), 255 ), a = math.min( tonumber(a), 255 ) }, COLOR_M )
+ return setmetatable( { r = math.min( tonumber(r), 255 ), g = math.min( tonumber(g), 255 ), b = math.min( tonumber(b), 255 ), a = math.min( tonumber(a), 255 ) }, COLOR )
end
@@ -30,7 +29,7 @@ end
--[[---------------------------------------------------------
Returns color as a string
-----------------------------------------------------------]]
-function COLOR_M:__tostring()
+function COLOR:__tostring()
return string.format( "%d %d %d", self.r, self.g, self.b )
@@ -39,7 +38,7 @@ end
--[[---------------------------------------------------------
Compares two colors
-----------------------------------------------------------]]
-function COLOR_M:__eq( c )
+function COLOR:__eq( c )
return self.r == c.r and self.g == c.g and self.b == c.b and self.a == c.a
@@ -53,3 +52,12 @@ function COLOR:ToHSV()
return ColorToHSV( self )
end
+
+--[[---------------------------------------------------------
+ Converts Color To Vector - loss of precision / alpha lost
+-----------------------------------------------------------]]
+function COLOR:ToVector( )
+
+ return Vector( self.r / 255, self.g / 255, self.b / 255 )
+
+end

1 comment on commit 6cc5352

@Grocel
Grocel commented on 6cc5352 Jun 23, 2014

This Angle:SnapTo() should be done for Vectors and Colors too.

Please sign in to comment.