From 5316848d9c1e2a3dabcdbd098bab797b6aea688d Mon Sep 17 00:00:00 2001 From: Lilith Bryant Date: Thu, 17 Jan 2013 19:36:56 +1300 Subject: [PATCH] Fix clearance issues due to circle approximation --- src/polygon.c | 17 +++++++++++++++++ src/polygon.h | 5 ++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/polygon.c b/src/polygon.c index 5f17fd0..c6881f7 100644 --- a/src/polygon.c +++ b/src/polygon.c @@ -110,6 +110,7 @@ dicer output is used for HIDs which cannot render things with holes #define SUBTRACT_LINE_BATCH_SIZE 20 static double rotate_circle_seg[4]; +static double rotate_circle_half_seg[4]; void polygon_init (void) @@ -117,8 +118,14 @@ polygon_init (void) double cos_ang = cos (2.0 * M_PI / POLY_CIRC_SEGS_F); double sin_ang = sin (2.0 * M_PI / POLY_CIRC_SEGS_F); + double cos_half_ang = cos (2.0 * M_PI / POLY_CIRC_SEGS_F / 2); + double sin_half_ang = sin (2.0 * M_PI / POLY_CIRC_SEGS_F / 2); + rotate_circle_seg[0] = cos_ang; rotate_circle_seg[1] = -sin_ang; rotate_circle_seg[2] = sin_ang; rotate_circle_seg[3] = cos_ang; + + rotate_circle_half_seg[0] = cos_half_ang; rotate_circle_half_seg[1] = -sin_half_ang; + rotate_circle_half_seg[2] = sin_half_ang; rotate_circle_half_seg[3] = cos_half_ang; } Cardinal @@ -400,10 +407,19 @@ frac_circle (PLINE * c, Coord X, Coord Y, Vector v, int range) int i; poly_InclVertex (c->head.prev, poly_CreateNode (v)); + /* move vector to origin */ e1 = (v[0] - X) * POLY_CIRC_RADIUS_ADJ; e2 = (v[1] - Y) * POLY_CIRC_RADIUS_ADJ; + /* do a half step, and make a point there */ + t1 = rotate_circle_half_seg[0] * e1 + rotate_circle_half_seg[1] * e2; + e2 = rotate_circle_half_seg[2] * e1 + rotate_circle_half_seg[3] * e2; + e1 = t1; + v[0] = X + ROUND (e1); + v[1] = Y + ROUND (e2); + poly_InclVertex (c->head.prev, poly_CreateNode (v)); + /* NB: the caller adds the last vertex, hence the -1 */ range = POLY_CIRC_SEGS / range - 1; for (i = 0; i < range; i++) @@ -429,6 +445,7 @@ CirclePoly (Coord x, Coord y, Coord radius) return NULL; v[0] = x + radius; v[1] = y; + if ((contour = poly_NewContour (v)) == NULL) return NULL; frac_circle (contour, x, y, v, 1); diff --git a/src/polygon.h b/src/polygon.h index 64d9a36..aecaff7 100644 --- a/src/polygon.h +++ b/src/polygon.h @@ -34,14 +34,13 @@ /* Implementation constants */ -#define POLY_CIRC_SEGS 40 +#define POLY_CIRC_SEGS 36 #define POLY_CIRC_SEGS_F ((float)POLY_CIRC_SEGS) /* adjustment to make the segments outline the circle rather than connect * points on the circle: 1 - cos (\alpha / 2) < (\alpha / 2) ^ 2 / 2 */ -#define POLY_CIRC_RADIUS_ADJ (1.0 + M_PI / POLY_CIRC_SEGS_F * \ - M_PI / POLY_CIRC_SEGS_F / 2.0) +#define POLY_CIRC_RADIUS_ADJ (1.0/cos(M_PI / POLY_CIRC_SEGS_F)) /* polygon diverges from modelled arc no more than MAX_ARC_DEVIATION * thick */ #define POLY_ARC_MAX_DEVIATION 0.02 -- 1.8.0