Sfoglia il codice sorgente

Added a "degrees" and "radians" property to Int and Float

These will be used to convert the value to degrees or radians
respectively.
Steven Hall 8 anni fa
parent
commit
c268135105

+ 6 - 0
docs/types.html

@@ -91,6 +91,9 @@
 	var d = 0B0101;		// binary
 
 	var e = Int.random(1, 10) // Returns a random integer between 1 and 10 inclusive
+
+	var f = 30.radians // returns the result of converting 30 degrees to radians
+	var f = 3.degrees // returns the result of converting 3 radians to degrees
 			</code></pre>
 			
 			<h4 class="section-h4">Float</h4>
@@ -98,6 +101,9 @@
 			<pre><code class="swift">
 	var a = 3.1415;		// float
 	var b = 1.25e2;		// scientific notation
+
+	var f = 30.5.radians // returns the result of converting 30.5 degrees to radians
+	var f = 3.14.degrees // returns the result of converting 3.14 radians to degrees
 			</code></pre>
 			
 			<h4 class="section-h4">String</h4>

+ 36 - 0
src/runtime/gravity_core.c

@@ -1415,6 +1415,18 @@ static bool float_exec (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, u
     RETURN_VALUE(v, rindex);
 }
 
+static bool float_degrees (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
+	#pragma unused(vm, nargs)
+	// Convert the float from radians to degrees
+	RETURN_VALUE(VALUE_FROM_FLOAT(GET_VALUE(0).f*180/3.141592653589793), rindex);
+}
+
+static bool float_radians (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
+	#pragma unused(vm, nargs)
+	// Convert the float from degrees to radians
+	RETURN_VALUE(VALUE_FROM_FLOAT(GET_VALUE(0).f*3.141592653589793/180), rindex);
+}
+
 // MARK: - Int Class -
 
 // binary operators
@@ -1550,6 +1562,18 @@ static bool int_exec (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uin
     RETURN_VALUE(v, rindex);
 }
 
+static bool int_degrees (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
+	#pragma unused(vm, nargs)
+	// Convert the int from radians to degrees
+	RETURN_VALUE(VALUE_FROM_FLOAT(GET_VALUE(0).n*180/3.141592653589793), rindex);
+}
+
+static bool int_radians (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
+	#pragma unused(vm, nargs)
+	// Convert the int from degrees to radians
+	RETURN_VALUE(VALUE_FROM_FLOAT(GET_VALUE(0).n*3.141592653589793/180), rindex);
+}
+
 // MARK: - Bool Class -
 
 static bool operator_bool_add (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
@@ -2452,6 +2476,10 @@ static void gravity_core_init (void) {
 	gravity_class_bind(gravity_class_int, GRAVITY_OPERATOR_NEG_NAME, NEW_CLOSURE_VALUE(operator_int_neg));
 	gravity_class_bind(gravity_class_int, GRAVITY_OPERATOR_NOT_NAME, NEW_CLOSURE_VALUE(operator_int_not));
 	gravity_class_bind(gravity_class_int, GRAVITY_INTERNAL_LOOP_NAME, NEW_CLOSURE_VALUE(int_loop));
+    closure = computed_property_create(NULL, NEW_FUNCTION(int_radians), NULL);
+	gravity_class_bind(gravity_class_int, "radians", VALUE_FROM_OBJECT(closure));
+    closure = computed_property_create(NULL, NEW_FUNCTION(int_degrees), NULL);
+	gravity_class_bind(gravity_class_int, "degrees", VALUE_FROM_OBJECT(closure));
 	// Meta
 	gravity_class_t *int_meta = gravity_class_get_meta(gravity_class_int);
 	gravity_class_bind(int_meta, "random", NEW_CLOSURE_VALUE(int_random));
@@ -2471,6 +2499,10 @@ static void gravity_core_init (void) {
 	gravity_class_bind(gravity_class_float, "round", NEW_CLOSURE_VALUE(function_float_round));
 	gravity_class_bind(gravity_class_float, "floor", NEW_CLOSURE_VALUE(function_float_floor));
 	gravity_class_bind(gravity_class_float, "ceil", NEW_CLOSURE_VALUE(function_float_ceil));
+    closure = computed_property_create(NULL, NEW_FUNCTION(float_radians), NULL);
+	gravity_class_bind(gravity_class_float, "radians", VALUE_FROM_OBJECT(closure));
+    closure = computed_property_create(NULL, NEW_FUNCTION(float_degrees), NULL);
+	gravity_class_bind(gravity_class_float, "degrees", VALUE_FROM_OBJECT(closure));
     // Meta
     gravity_class_t *float_meta = gravity_class_get_meta(gravity_class_float);
     gravity_class_bind(float_meta, GRAVITY_INTERNAL_EXEC_NAME, NEW_CLOSURE_VALUE(float_exec));
@@ -2597,6 +2629,10 @@ void gravity_core_free (void) {
     computed_property_free(gravity_class_map, "count", true);
     computed_property_free(gravity_class_range, "count", true);
     computed_property_free(gravity_class_string, "length", true);
+    computed_property_free(gravity_class_int, "radians", true);
+    computed_property_free(gravity_class_int, "degrees", true);
+    computed_property_free(gravity_class_float, "radians", true);
+    computed_property_free(gravity_class_float, "degrees", true);
     gravity_class_t *system_meta = gravity_class_get_meta(gravity_class_system);
     computed_property_free(system_meta, "gcenabled", true);
         

+ 26 - 0
test/unittest/radian_degree_conversion.gravity

@@ -0,0 +1,26 @@
+#unittest {
+	name: "Test Int.radians, Int.degrees";
+	error: NONE;
+	result: true;
+};
+
+func main() {
+	// 360 degrees in radians is 2*pi
+	var ir1 = 360.radians == 2*Math.PI
+	// 180 degrees in radians is pi
+	var ir2 = 180.radians == Math.PI
+	// 30 degrees in radians is pi/6
+	var ir3 = 30.radians == Math.PI/6
+	var ir = ir1 and ir2 and ir3
+
+	// Test Int.degrees
+	// pi/3 radians in degrees is 60
+	var id1 = (Math.PI/3).degrees <= 60.000001 and (Math.PI/3).degrees >= 59.999999
+	// pi/4 radians in degrees is 45
+	var id2 = (Math.PI/4).degrees <= 45.000001 and (Math.PI/4).degrees >= 44.999999
+	// pi/2 radians in degrees is 90
+	var id3 = (Math.PI/2).degrees <= 90.000001 and (Math.PI/2).degrees >= 89.999999
+	var id = id1 and id2 and id3
+
+	return id and ir
+}