Ensure no two sides are of the same length (makes selecting the answer really, really annoying lol)
This commit is contained in:
+15
-10
@@ -45,21 +45,26 @@ RightTriangleLayer *right_triangle_layer_create(GRect frame, GColor color) {
|
||||
tri->color = color;
|
||||
|
||||
// determine angles
|
||||
tri->angle_A = 90; // right angle at apex
|
||||
tri->angle_B = 5 + rand() % 81; // acute angle at left base vertex
|
||||
tri->angle_C = 90 - tri->angle_B; // acute angle at right base vertex
|
||||
tri->angle_A = 90; // right angle at apex
|
||||
tri->theta_angle_is_left = rand() % 2;
|
||||
|
||||
// choose hypotenuse length randomly
|
||||
uint16_t min_hyp = (frame.size.w < frame.size.h) ? frame.size.w / 2 : frame.size.h / 2;
|
||||
uint16_t max_hyp = (frame.size.w < frame.size.h) ? frame.size.w : frame.size.h;
|
||||
tri->side_a = min_hyp + (rand() % (max_hyp - min_hyp + 1));
|
||||
|
||||
// legs: side_b = hyp × sin(B), side_c = hyp × sin(C) = hyp × cos(B)
|
||||
int32_t sin_B = sin_lookup(((int32_t)tri->angle_B * TRIG_MAX_ANGLE) / 360);
|
||||
int32_t sin_C = sin_lookup(((int32_t)tri->angle_C * TRIG_MAX_ANGLE) / 360);
|
||||
tri->side_b = (uint16_t)(((uint32_t)sin_B * tri->side_a) / TRIG_MAX_RATIO);
|
||||
tri->side_c = (uint16_t)(((uint32_t)sin_C * tri->side_a) / TRIG_MAX_RATIO);
|
||||
// regenerate until no two sides share a length
|
||||
do {
|
||||
tri->angle_B = 5 + rand() % 81; // acute angle at left base vertex
|
||||
tri->angle_C = 90 - tri->angle_B; // acute angle at right base vertex
|
||||
|
||||
// choose hypotenuse length randomly
|
||||
tri->side_a = min_hyp + (rand() % (max_hyp - min_hyp + 1));
|
||||
|
||||
// legs: side_b = hyp × sin(B), side_c = hyp × sin(C) = hyp × cos(B)
|
||||
int32_t sin_B = sin_lookup(((int32_t)tri->angle_B * TRIG_MAX_ANGLE) / 360);
|
||||
int32_t sin_C = sin_lookup(((int32_t)tri->angle_C * TRIG_MAX_ANGLE) / 360);
|
||||
tri->side_b = (uint16_t)(((uint32_t)sin_B * tri->side_a) / TRIG_MAX_RATIO);
|
||||
tri->side_c = (uint16_t)(((uint32_t)sin_C * tri->side_a) / TRIG_MAX_RATIO);
|
||||
} while (tri->side_a == tri->side_b || tri->side_a == tri->side_c || tri->side_b == tri->side_c);
|
||||
|
||||
// create layer with room for a pointer-sized data slot & store the RightTriangleLayer* there
|
||||
tri->layer = layer_create_with_data(frame, sizeof(RightTriangleLayer *));
|
||||
|
||||
Reference in New Issue
Block a user