Exemplo n.º 1
0
d_define_method(line, intersect_point)(struct s_object *self, struct s_object *other, struct s_object *intersection) {
	d_using(line);
	struct s_line_attributes *other_attributes = d_cast(other, line);
	struct s_point_attributes *intersection_attributes;
	double projection_A_ending_x, projection_A_ending_y, projection_B_starting_x, projection_B_starting_y, projection_B_ending_x, projection_B_ending_y,
	      size_A, cos, sin, rotated_B_starting_x, rotated_B_starting_y, rotated_B_ending_x, rotated_B_ending_y, intersection_position;
	t_boolean result = d_false;
	projection_A_ending_x = line_attributes->ending_x - line_attributes->starting_x;
	projection_A_ending_y = line_attributes->ending_y - line_attributes->starting_y;
	projection_B_starting_x = other_attributes->starting_x - line_attributes->starting_x;
	projection_B_starting_y = other_attributes->starting_y - line_attributes->starting_y;
	projection_B_ending_x = other_attributes->ending_x - line_attributes->starting_x;
	projection_B_ending_y = other_attributes->ending_y - line_attributes->starting_y;
	size_A = f_math_sqrt((projection_A_ending_x*projection_A_ending_x) + (projection_B_ending_y*projection_B_ending_y), d_math_default_precision);
	cos = projection_A_ending_x/size_A;
	sin = projection_A_ending_y/size_A;
	rotated_B_starting_x = (projection_B_starting_x * cos) + (projection_B_starting_y * sin);
	rotated_B_starting_y = (projection_B_starting_y * cos) - (projection_B_starting_x * sin);
	rotated_B_ending_x = (projection_B_ending_x * cos) + (projection_B_ending_y * sin);
	rotated_B_ending_y = (projection_B_ending_y * cos) - (projection_B_ending_x * sin);
	if (((rotated_B_starting_y >= 0) && (rotated_B_ending_y < 0)) || ((rotated_B_starting_y < 0) && (rotated_B_ending_y >= 0))) {
		intersection_position = rotated_B_ending_x + (rotated_B_starting_x - rotated_B_ending_x) * rotated_B_ending_y /
			(rotated_B_ending_y - rotated_B_starting_y);
		if ((intersection_position >= 0) && (intersection_position <= size_A)) {
			if (intersection)
				if ((intersection_attributes = d_cast(intersection, point))) {
					intersection_attributes->x = line_attributes->starting_x + (intersection_position * cos);
					intersection_attributes->y = line_attributes->starting_y + (intersection_position * sin);
				}
			result = d_true;
		}
	}
	d_cast_return(result);
}
Exemplo n.º 2
0
float f_math_rms(float *values, size_t elements, float precision) {
  float mean = 0.0f, mean_square = 0.0f;
  int index;
  for (index = 0; index < elements; ++index) {
    mean += values[index];
    mean_square += (values[index] * values[index]);
  }
  mean /= (float)elements;
  mean_square /= (float)elements;
  return f_math_sqrt(fabs(mean_square - (mean * mean)), precision);
}