static gint gimp_cage_tool_is_on_edge (GimpCageTool *ct, gdouble x, gdouble y, gint handle_size) { GimpCageOptions *options = GIMP_CAGE_TOOL_GET_OPTIONS (ct); GimpCageConfig *config = ct->config; gint i; guint n_cage_vertices; GimpVector2 A, B, C, AB, BC, AC; gdouble lAB, lBC, lAC, lEB, lEC; g_return_val_if_fail (GIMP_IS_CAGE_TOOL (ct), -1); n_cage_vertices = gimp_cage_config_get_n_points (config); if (n_cage_vertices < 2) return -1; A = gimp_cage_config_get_point_coordinate (config, options->cage_mode, n_cage_vertices-1); B = gimp_cage_config_get_point_coordinate (config, options->cage_mode, 0); C.x = x; C.y = y; for (i = 0; i < n_cage_vertices; i++) { gimp_vector2_sub (&AB, &A, &B); gimp_vector2_sub (&BC, &B, &C); gimp_vector2_sub (&AC, &A, &C); lAB = gimp_vector2_length (&AB); lBC = gimp_vector2_length (&BC); lAC = gimp_vector2_length (&AC); lEB = lAB / 2 + (SQR (lBC) - SQR (lAC)) / (2 * lAB); lEC = sqrt (SQR (lBC) - SQR (lEB)); if ((lEC < handle_size / 2) && (abs (SQR (lBC) - SQR (lAC)) <= SQR (lAB))) return i; A = B; B = gimp_cage_config_get_point_coordinate (config, options->cage_mode, (i+1) % n_cage_vertices); } return -1; }
/** * gimp_utils_point_to_line_distance: * @point: The point to calculate the distance for. * @point_on_line: A point on the line. * @line_direction: Normalized line direction vector. * @closest_line_point: Gets set to the point on the line that is * closest to @point. * * Returns: The shortest distance from @point to the line defined by * @point_on_line and @normalized_line_direction. **/ static gdouble gimp_utils_point_to_line_distance (const GimpVector2 *point, const GimpVector2 *point_on_line, const GimpVector2 *line_direction, GimpVector2 *closest_line_point) { GimpVector2 distance_vector; GimpVector2 tmp_a; GimpVector2 tmp_b; gdouble d; gimp_vector2_sub (&tmp_a, point, point_on_line); d = gimp_vector2_inner_product (&tmp_a, line_direction); tmp_b = gimp_vector2_mul_val (*line_direction, d); *closest_line_point = gimp_vector2_add_val (*point_on_line, tmp_b); gimp_vector2_sub (&distance_vector, closest_line_point, point); return gimp_vector2_length (&distance_vector); }
static void init_calculation (gint32 drawable_id) { gdouble k; gdouble alpha, beta; gdouble angle; GimpVector2 v1, v2; gint32 *image_layers; gint32 nlayers; GimpRGB color; gimp_layer_add_alpha (drawable_id); /* Image parameters */ /* Determine Position of original Layer in the Layer stack. */ image_layers = gimp_image_get_layers (image_id, &nlayers); drawable_position = 0; while (drawable_position < nlayers && image_layers[drawable_position] != drawable_id) drawable_position++; switch (curl.orientation) { case CURL_ORIENTATION_VERTICAL: sel_width = true_sel_width; sel_height = true_sel_height; break; case CURL_ORIENTATION_HORIZONTAL: sel_width = true_sel_height; sel_height = true_sel_width; break; } /* Circle parameters */ alpha = atan ((double) sel_height / sel_width); beta = alpha / 2.0; k = sel_width / ((G_PI + alpha) * sin (beta) + cos (beta)); gimp_vector2_set (¢er, k * cos (beta), k * sin (beta)); radius = center.y; /* left_tangent */ gimp_vector2_set (&left_tangent, radius * -sin (alpha), radius * cos (alpha)); gimp_vector2_add (&left_tangent, &left_tangent, ¢er); /* right_tangent */ gimp_vector2_sub (&v1, &left_tangent, ¢er); gimp_vector2_set (&v2, sel_width - center.x, sel_height - center.y); angle = -2.0 * acos (gimp_vector2_inner_product (&v1, &v2) / (gimp_vector2_length (&v1) * gimp_vector2_length (&v2))); gimp_vector2_set (&right_tangent, v1.x * cos (angle) + v1.y * -sin (angle), v1.x * sin (angle) + v1.y * cos (angle)); gimp_vector2_add (&right_tangent, &right_tangent, ¢er); /* Slopes */ diagl_slope = (double) sel_width / sel_height; diagr_slope = (sel_width - right_tangent.x) / (sel_height - right_tangent.y); diagb_slope = ((right_tangent.y - left_tangent.y) / (right_tangent.x - left_tangent.x)); diagm_slope = (sel_width - center.x) / sel_height; /* Colors */ gimp_context_get_foreground (&color); gimp_rgb_get_uchar (&color, &fore_color[0], &fore_color[1], &fore_color[2]); gimp_context_get_background (&color); gimp_rgb_get_uchar (&color, &back_color[0], &back_color[1], &back_color[2]); }