1 /* Pioneers - Implementation of the excellent Settlers of Catan board game.
2  *   Go buy a copy.
3  *
4  * Copyright (C) 1999 Dave Cole
5  * Copyright (C) 2003 Bas Wijnen <shevek@fmf.nl>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #include "config.h"
23 #include <math.h>
24 #include <gdk/gdk.h>
25 
26 #include "polygon.h"
27 
poly_offset(Polygon * poly,gint x_offset,gint y_offset)28 void poly_offset(Polygon * poly, gint x_offset, gint y_offset)
29 {
30 	int idx;
31 	GdkPoint *points;
32 
33 	for (idx = 0, points = poly->points; idx < poly->num_points;
34 	     idx++, points++) {
35 		points->x += x_offset;
36 		points->y += y_offset;
37 	}
38 }
39 
poly_bound_rect(const Polygon * poly,int pad,GdkRectangle * rect)40 void poly_bound_rect(const Polygon * poly, int pad, GdkRectangle * rect)
41 {
42 	int idx;
43 	GdkPoint tl;
44 	GdkPoint br;
45 	GdkPoint *points;
46 
47 	points = poly->points;
48 	tl = points[0];
49 	br = points[0];
50 	for (idx = 1, points++; idx < poly->num_points; idx++, points++) {
51 		if (points->x < tl.x)
52 			tl.x = points->x;
53 		else if (points->x > br.x)
54 			br.x = points->x;
55 		if (points->y < tl.y)
56 			tl.y = points->y;
57 		else if (points->y > br.y)
58 			br.y = points->y;
59 	}
60 	rect->x = tl.x - pad;
61 	rect->y = tl.y - pad;
62 	rect->width = br.x - tl.x + pad + 1;
63 	rect->height = br.y - tl.y + pad + 1;
64 }
65 
poly_draw(cairo_t * cr,gboolean filled,const Polygon * poly)66 void poly_draw(cairo_t * cr, gboolean filled, const Polygon * poly)
67 {
68 	gint i;
69 
70 	if (poly->num_points > 0) {
71 		cairo_move_to(cr, poly->points[poly->num_points - 1].x,
72 			      poly->points[poly->num_points - 1].y);
73 		for (i = 0; i < poly->num_points; i++) {
74 			cairo_line_to(cr, poly->points[i].x,
75 				      poly->points[i].y);
76 		}
77 		if (filled) {
78 			cairo_fill(cr);
79 		} else {
80 			cairo_stroke(cr);
81 		}
82 	}
83 }
84 
poly_draw_with_border(cairo_t * cr,const GdkRGBA * border_color,const Polygon * poly)85 void poly_draw_with_border(cairo_t * cr,
86 			   const GdkRGBA * border_color,
87 			   const Polygon * poly)
88 {
89 	poly_draw(cr, TRUE, poly);
90 	gdk_cairo_set_source_rgba(cr, border_color);
91 	poly_draw(cr, FALSE, poly);
92 }
93