2006-08-29 22:46:32 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* gimprectangle.c
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This library is free software: you can redistribute it and/or
|
2006-08-29 22:46:32 +08:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2009-01-18 06:28:01 +08:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
2006-08-29 22:46:32 +08:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-01-18 06:28:01 +08:00
|
|
|
* License along with this library. If not, see
|
2018-07-12 05:27:07 +08:00
|
|
|
* <https://www.gnu.org/licenses/>.
|
2006-08-29 22:46:32 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "gimprectangle.h"
|
|
|
|
|
|
|
|
|
2010-06-30 01:13:40 +08:00
|
|
|
/**
|
|
|
|
* SECTION: gimprectangle
|
|
|
|
* @title: gimprectangle
|
|
|
|
* @short_description: Utility functions dealing with rectangle extents.
|
|
|
|
*
|
|
|
|
* Utility functions dealing with rectangle extents.
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
2006-08-29 22:46:32 +08:00
|
|
|
/**
|
|
|
|
* gimp_rectangle_intersect:
|
|
|
|
* @x1: origin of first rectangle
|
|
|
|
* @y1: origin of first rectangle
|
|
|
|
* @width1: width of first rectangle
|
|
|
|
* @height1: height of first rectangle
|
|
|
|
* @x2: origin of second rectangle
|
|
|
|
* @y2: origin of second rectangle
|
|
|
|
* @width2: width of second rectangle
|
|
|
|
* @height2: height of second rectangle
|
2020-05-03 23:35:01 +08:00
|
|
|
* @dest_x: (out) (optional): return location for origin of intersection,
|
|
|
|
* or %NULL
|
|
|
|
* @dest_y: (out) (optional): return location for origin of intersection,
|
|
|
|
* or %NULL
|
|
|
|
* @dest_width: (out) (optional): return location for width of intersection,
|
|
|
|
* or %NULL
|
|
|
|
* @dest_height: (out) (optional): return location for height of intersection,
|
|
|
|
* or %NULL
|
2006-08-29 22:46:32 +08:00
|
|
|
*
|
|
|
|
* Calculates the intersection of two rectangles.
|
|
|
|
*
|
2019-08-03 06:10:14 +08:00
|
|
|
* Returns: %TRUE if the intersection is non-empty, %FALSE otherwise
|
2006-08-29 22:46:32 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2009-08-24 04:58:36 +08:00
|
|
|
**/
|
2006-08-29 22:46:32 +08:00
|
|
|
gboolean
|
|
|
|
gimp_rectangle_intersect (gint x1,
|
|
|
|
gint y1,
|
|
|
|
gint width1,
|
|
|
|
gint height1,
|
|
|
|
gint x2,
|
|
|
|
gint y2,
|
|
|
|
gint width2,
|
|
|
|
gint height2,
|
|
|
|
gint *dest_x,
|
|
|
|
gint *dest_y,
|
|
|
|
gint *dest_width,
|
|
|
|
gint *dest_height)
|
|
|
|
{
|
|
|
|
gint d_x, d_y;
|
|
|
|
gint d_w, d_h;
|
|
|
|
|
|
|
|
d_x = MAX (x1, x2);
|
|
|
|
d_y = MAX (y1, y2);
|
|
|
|
d_w = MIN (x1 + width1, x2 + width2) - d_x;
|
|
|
|
d_h = MIN (y1 + height1, y2 + height2) - d_y;
|
|
|
|
|
|
|
|
if (dest_x) *dest_x = d_x;
|
|
|
|
if (dest_y) *dest_y = d_y;
|
|
|
|
if (dest_width) *dest_width = d_w;
|
|
|
|
if (dest_height) *dest_height = d_h;
|
|
|
|
|
|
|
|
return (d_w > 0 && d_h > 0);
|
|
|
|
}
|
2009-08-24 04:58:36 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_rectangle_union:
|
|
|
|
* @x1: origin of first rectangle
|
|
|
|
* @y1: origin of first rectangle
|
|
|
|
* @width1: width of first rectangle
|
|
|
|
* @height1: height of first rectangle
|
|
|
|
* @x2: origin of second rectangle
|
|
|
|
* @y2: origin of second rectangle
|
|
|
|
* @width2: width of second rectangle
|
|
|
|
* @height2: height of second rectangle
|
2020-05-03 23:35:01 +08:00
|
|
|
* @dest_x: (out) (optional): return location for origin of union, or %NULL
|
|
|
|
* @dest_y: (out) (optional): return location for origin of union, or %NULL
|
|
|
|
* @dest_width: (out) (optional): return location for width of union, or %NULL
|
|
|
|
* @dest_height: (out) (optional): return location for height of union, or %NULL
|
2009-08-24 04:58:36 +08:00
|
|
|
*
|
|
|
|
* Calculates the union of two rectangles.
|
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.8
|
2009-08-24 04:58:36 +08:00
|
|
|
**/
|
|
|
|
void
|
|
|
|
gimp_rectangle_union (gint x1,
|
|
|
|
gint y1,
|
|
|
|
gint width1,
|
|
|
|
gint height1,
|
|
|
|
gint x2,
|
|
|
|
gint y2,
|
|
|
|
gint width2,
|
|
|
|
gint height2,
|
|
|
|
gint *dest_x,
|
|
|
|
gint *dest_y,
|
|
|
|
gint *dest_width,
|
|
|
|
gint *dest_height)
|
|
|
|
{
|
|
|
|
gint d_x, d_y;
|
|
|
|
gint d_w, d_h;
|
|
|
|
|
|
|
|
d_x = MIN (x1, x2);
|
|
|
|
d_y = MIN (y1, y2);
|
|
|
|
d_w = MAX (x1 + width1, x2 + width2) - d_x;
|
|
|
|
d_h = MAX (y1 + height1, y2 + height2) - d_y;
|
|
|
|
|
|
|
|
if (dest_x) *dest_x = d_x;
|
|
|
|
if (dest_y) *dest_y = d_y;
|
|
|
|
if (dest_width) *dest_width = d_w;
|
|
|
|
if (dest_height) *dest_height = d_h;
|
|
|
|
}
|