2003-11-12 05:23:33 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2007-06-06 16:44:52 +08:00
|
|
|
* This is a plug-in for GIMP.
|
2003-11-12 05:23:33 +08:00
|
|
|
*
|
|
|
|
* Generates images containing vector type drawings.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1997 Andy Thomas alt@picnic.demon.co.uk
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <libgimp/gimp.h>
|
|
|
|
|
|
|
|
#include "gfig.h"
|
|
|
|
#include "gfig-grid.h"
|
|
|
|
|
|
|
|
#include "libgimp/stdplugins-intl.h"
|
|
|
|
|
2005-03-04 21:23:32 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
/* For the isometric grid */
|
|
|
|
#define SQRT3 1.73205080756887729353 /* Square root of 3 */
|
|
|
|
#define SIN_1o6PI_RAD 0.5 /* Sine 1/6 Pi Radians */
|
|
|
|
#define COS_1o6PI_RAD SQRT3 / 2 /* Cosine 1/6 Pi Radians */
|
|
|
|
#define TAN_1o6PI_RAD 1 / SQRT3 /* Tangent 1/6 Pi Radians == SIN / COS */
|
|
|
|
#define RECIP_TAN_1o6PI_RAD SQRT3 /* Reciprocal of Tangent 1/6 Pi Radians */
|
|
|
|
|
2005-07-20 17:40:32 +08:00
|
|
|
static GdkGC *grid_hightlight_drawgc = NULL;
|
|
|
|
gint grid_gc_type = GTK_STATE_NORMAL;
|
2003-11-12 05:23:33 +08:00
|
|
|
|
2005-07-20 17:40:32 +08:00
|
|
|
static void draw_grid_polar (GdkGC *drawgc);
|
|
|
|
static void draw_grid_sq (GdkGC *drawgc);
|
|
|
|
static void draw_grid_iso (GdkGC *drawgc);
|
2004-11-30 07:02:39 +08:00
|
|
|
|
2005-07-20 17:40:32 +08:00
|
|
|
static GdkGC * gfig_get_grid_gc (GtkWidget *widget,
|
|
|
|
gint gctype);
|
|
|
|
|
|
|
|
static void find_grid_pos_polar (GdkPoint *p,
|
|
|
|
GdkPoint *gp);
|
2005-07-14 04:05:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
/********** PrimeFactors for Shaneyfelt-style Polar Grid **********
|
|
|
|
* Quickly factor any number up to 17160
|
|
|
|
* Correctly factors numbers up to 131 * 131 - 1
|
|
|
|
*/
|
2005-07-20 17:40:32 +08:00
|
|
|
typedef struct
|
2005-07-14 04:05:41 +08:00
|
|
|
{
|
2005-07-20 17:40:32 +08:00
|
|
|
gint product;
|
|
|
|
gint remaining;
|
|
|
|
gint current;
|
|
|
|
gint next;
|
|
|
|
gint index;
|
2005-07-14 04:05:41 +08:00
|
|
|
} PrimeFactors;
|
|
|
|
|
2005-07-20 17:40:32 +08:00
|
|
|
static gchar primes[] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,
|
|
|
|
59,61,67,71,73,79,83,89,97,101,103,107,109,113,127 };
|
|
|
|
|
|
|
|
#define PRIMES_MAX_INDEX 30
|
2005-07-14 04:05:41 +08:00
|
|
|
|
2005-07-20 17:40:32 +08:00
|
|
|
|
|
|
|
static gint
|
2005-07-14 04:05:41 +08:00
|
|
|
prime_factors_get (PrimeFactors *this)
|
|
|
|
{
|
2005-07-20 17:40:32 +08:00
|
|
|
this->current = this->next;
|
|
|
|
while (this->index <= PRIMES_MAX_INDEX)
|
2005-07-14 04:05:41 +08:00
|
|
|
{
|
2005-12-31 01:50:19 +08:00
|
|
|
if (this->remaining % primes[this->index] == 0) /* divisible */
|
2005-07-20 17:40:32 +08:00
|
|
|
{
|
|
|
|
this->remaining /= primes[this->index];
|
|
|
|
this->next = primes[this->index];
|
|
|
|
return this->current;
|
2005-07-14 04:05:41 +08:00
|
|
|
}
|
2005-07-20 17:40:32 +08:00
|
|
|
this->index++;
|
2005-07-14 04:05:41 +08:00
|
|
|
}
|
2005-07-20 17:40:32 +08:00
|
|
|
this->next = this->remaining;
|
|
|
|
this->remaining = 1;
|
|
|
|
|
|
|
|
return this->current;
|
2005-07-14 04:05:41 +08:00
|
|
|
}
|
|
|
|
|
2005-07-20 17:40:32 +08:00
|
|
|
static gint
|
2005-07-14 04:05:41 +08:00
|
|
|
prime_factors_lookahead (PrimeFactors *this)
|
|
|
|
{
|
2005-07-20 17:40:32 +08:00
|
|
|
return this->next;
|
2005-07-14 04:05:41 +08:00
|
|
|
}
|
|
|
|
|
2005-07-20 17:40:32 +08:00
|
|
|
static void
|
2005-07-14 04:05:41 +08:00
|
|
|
prime_factors_reset (PrimeFactors *this)
|
|
|
|
{
|
2005-07-20 17:40:32 +08:00
|
|
|
this->remaining = this->product;
|
|
|
|
this->index = 0;
|
|
|
|
prime_factors_get (this);
|
2005-07-14 04:05:41 +08:00
|
|
|
}
|
|
|
|
|
2005-07-20 17:40:32 +08:00
|
|
|
static PrimeFactors *
|
|
|
|
prime_factors_new (gint n)
|
|
|
|
{
|
|
|
|
PrimeFactors *this = g_new (PrimeFactors, 1);
|
|
|
|
|
|
|
|
this->product = n;
|
|
|
|
prime_factors_reset (this);
|
|
|
|
|
|
|
|
return this;
|
2005-07-14 04:05:41 +08:00
|
|
|
}
|
|
|
|
|
2005-07-20 17:40:32 +08:00
|
|
|
static void
|
|
|
|
prime_factors_delete (PrimeFactors* this)
|
|
|
|
{
|
|
|
|
g_free (this);
|
2005-07-14 04:05:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/********** ********** **********/
|
|
|
|
|
2005-07-20 17:40:32 +08:00
|
|
|
static gdouble
|
2005-07-14 04:05:41 +08:00
|
|
|
sector_size_at_radius (gdouble inner_radius)
|
|
|
|
{
|
2005-07-20 17:40:32 +08:00
|
|
|
PrimeFactors *factors = prime_factors_new (selvals.opts.grid_sectors_desired);
|
|
|
|
gint current_sectors = 1;
|
|
|
|
gdouble sector_size = 2 * G_PI / current_sectors;
|
|
|
|
|
|
|
|
while ((current_sectors < selvals.opts.grid_sectors_desired)
|
|
|
|
&& (inner_radius*sector_size
|
|
|
|
> (prime_factors_lookahead (factors) *
|
|
|
|
selvals.opts.grid_granularity)))
|
|
|
|
{
|
|
|
|
current_sectors *= prime_factors_get (factors);
|
|
|
|
sector_size = 2 * G_PI / current_sectors;
|
2005-07-14 04:05:41 +08:00
|
|
|
}
|
2005-07-20 17:40:32 +08:00
|
|
|
|
|
|
|
prime_factors_delete(factors);
|
|
|
|
|
|
|
|
return sector_size;
|
2005-07-14 04:05:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
find_grid_pos_polar (GdkPoint *p,
|
|
|
|
GdkPoint *gp)
|
|
|
|
{
|
2005-07-20 17:40:32 +08:00
|
|
|
gdouble cx = preview_width / 2.0;
|
|
|
|
gdouble cy = preview_height / 2.0;
|
|
|
|
gdouble px = p->x - cx;
|
|
|
|
gdouble py = p->y - cy;
|
|
|
|
gdouble x = 0;
|
|
|
|
gdouble y = 0;
|
|
|
|
gdouble r = sqrt (SQR (px) + SQR (py));
|
|
|
|
|
|
|
|
if (r >= selvals.opts.grid_radius_min * 0.5)
|
|
|
|
{
|
|
|
|
gdouble t;
|
|
|
|
gdouble sectorSize;
|
|
|
|
|
|
|
|
r = selvals.opts.grid_radius_interval
|
|
|
|
* (gint) (0.5 + ((r - selvals.opts.grid_radius_min) /
|
|
|
|
selvals.opts.grid_radius_interval))
|
|
|
|
+ selvals.opts.grid_radius_min;
|
|
|
|
|
|
|
|
t = atan2 (py, px) + 2 * G_PI;
|
|
|
|
sectorSize = sector_size_at_radius (r);
|
|
|
|
t = selvals.opts.grid_rotation
|
|
|
|
+ (gint) (0.5 + ((t - selvals.opts.grid_rotation) / sectorSize))
|
|
|
|
* sectorSize;
|
|
|
|
x = r * cos (t);
|
|
|
|
y = r * sin (t);
|
2005-07-14 04:05:41 +08:00
|
|
|
}
|
2005-07-20 17:40:32 +08:00
|
|
|
|
|
|
|
gp->x = x + cx;
|
|
|
|
gp->y = y + cy;
|
2005-07-14 04:05:41 +08:00
|
|
|
}
|
2003-11-12 05:23:33 +08:00
|
|
|
|
|
|
|
/* find_grid_pos - Given an x, y point return the grid position of it */
|
|
|
|
/* return the new position in the passed point */
|
|
|
|
|
|
|
|
void
|
|
|
|
gfig_grid_colours (GtkWidget *widget)
|
|
|
|
{
|
|
|
|
GdkColormap *colormap;
|
|
|
|
GdkGCValues values;
|
|
|
|
GdkColor col1;
|
|
|
|
GdkColor col2;
|
|
|
|
guchar stipple[8] =
|
|
|
|
{
|
|
|
|
0xAA, /* ####---- */
|
|
|
|
0x55, /* ###----# */
|
|
|
|
0xAA, /* ##----## */
|
|
|
|
0x55, /* #----### */
|
|
|
|
0xAA, /* ----#### */
|
|
|
|
0x55, /* ---####- */
|
|
|
|
0xAA, /* --####-- */
|
|
|
|
0x55, /* -####--- */
|
|
|
|
};
|
|
|
|
|
|
|
|
colormap = gdk_screen_get_rgb_colormap (gtk_widget_get_screen (widget));
|
|
|
|
|
|
|
|
gdk_color_parse ("gray50", &col1);
|
|
|
|
gdk_colormap_alloc_color (colormap, &col1, FALSE, TRUE);
|
|
|
|
|
|
|
|
gdk_color_parse ("gray80", &col2);
|
|
|
|
gdk_colormap_alloc_color (colormap, &col2, FALSE, TRUE);
|
|
|
|
|
|
|
|
values.background.pixel = col1.pixel;
|
|
|
|
values.foreground.pixel = col2.pixel;
|
|
|
|
values.fill = GDK_OPAQUE_STIPPLED;
|
|
|
|
values.stipple = gdk_bitmap_create_from_data (widget->window,
|
2004-07-05 04:50:25 +08:00
|
|
|
(gchar *) stipple, 4, 4);
|
2003-11-12 05:23:33 +08:00
|
|
|
grid_hightlight_drawgc = gdk_gc_new_with_values (widget->window,
|
2004-07-05 04:50:25 +08:00
|
|
|
&values,
|
|
|
|
GDK_GC_FOREGROUND |
|
|
|
|
GDK_GC_BACKGROUND |
|
|
|
|
GDK_GC_FILL |
|
|
|
|
GDK_GC_STIPPLE);
|
2003-11-12 05:23:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
find_grid_pos (GdkPoint *p,
|
2004-07-05 04:50:25 +08:00
|
|
|
GdkPoint *gp,
|
|
|
|
guint is_butt3)
|
2003-11-12 05:23:33 +08:00
|
|
|
{
|
2004-10-29 05:09:33 +08:00
|
|
|
gint16 x = p->x;
|
|
|
|
gint16 y = p->y;
|
2003-11-12 05:23:33 +08:00
|
|
|
static GdkPoint cons_pnt;
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
if (selvals.opts.gridtype == RECT_GRID)
|
|
|
|
{
|
|
|
|
if (p->x % selvals.opts.gridspacing > selvals.opts.gridspacing/2)
|
2004-07-05 04:50:25 +08:00
|
|
|
x += selvals.opts.gridspacing;
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
if (p->y % selvals.opts.gridspacing > selvals.opts.gridspacing/2)
|
2004-07-05 04:50:25 +08:00
|
|
|
y += selvals.opts.gridspacing;
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
gp->x = (x/selvals.opts.gridspacing)*selvals.opts.gridspacing;
|
|
|
|
gp->y = (y/selvals.opts.gridspacing)*selvals.opts.gridspacing;
|
|
|
|
|
|
|
|
if (is_butt3)
|
2004-07-05 04:50:25 +08:00
|
|
|
{
|
|
|
|
if (abs (gp->x - cons_pnt.x) < abs (gp->y - cons_pnt.y))
|
|
|
|
gp->x = cons_pnt.x;
|
|
|
|
else
|
|
|
|
gp->y = cons_pnt.y;
|
|
|
|
}
|
2003-11-12 05:23:33 +08:00
|
|
|
else
|
2004-07-05 04:50:25 +08:00
|
|
|
{
|
|
|
|
/* Store the point since might be used later */
|
|
|
|
cons_pnt = *gp; /* Structure copy */
|
|
|
|
}
|
2003-11-12 05:23:33 +08:00
|
|
|
}
|
|
|
|
else if (selvals.opts.gridtype == POLAR_GRID)
|
2004-10-27 14:43:52 +08:00
|
|
|
{
|
2005-07-14 04:05:41 +08:00
|
|
|
find_grid_pos_polar (p,gp);
|
2003-11-12 05:23:33 +08:00
|
|
|
}
|
|
|
|
else if (selvals.opts.gridtype == ISO_GRID)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This really needs a picture to show the math...
|
|
|
|
*
|
2005-07-20 17:40:32 +08:00
|
|
|
* Consider an isometric grid with one of the sets of lines
|
|
|
|
* parallel to the y axis (vertical alignment). Further define
|
|
|
|
* that the origin of a Cartesian grid is at a isometric vertex.
|
|
|
|
* For simplicity consider the first quadrant only.
|
2004-10-27 14:43:52 +08:00
|
|
|
*
|
2003-11-12 05:23:33 +08:00
|
|
|
* - Let one line segment between vertices be r
|
|
|
|
* - Define the value of r as the grid spacing
|
2005-07-20 17:40:32 +08:00
|
|
|
* - Assign an integer n identifier to each vertical grid line
|
|
|
|
* along the x axis. with n=0 being the y axis. n can be any
|
|
|
|
* integer
|
2003-11-12 05:23:33 +08:00
|
|
|
* - Let m to be any integer
|
2005-07-20 17:40:32 +08:00
|
|
|
|
|
|
|
* - Let h be the spacing between vertical grid lines measured
|
|
|
|
* along the x axis. It follows from the isometric grid that
|
|
|
|
* h has a value of r * COS(1/6 Pi Rad)
|
2004-10-27 14:43:52 +08:00
|
|
|
*
|
2003-11-12 05:23:33 +08:00
|
|
|
* Consider a Vertex V at the Cartesian location [Xv, Yv]
|
|
|
|
*
|
|
|
|
* It follows that vertices belong to the set...
|
|
|
|
* V[Xv, Yv] = [ [ n * h ] ,
|
|
|
|
* [ m * r + ( 0.5 * r (n % 2) ) ] ]
|
|
|
|
* for all integers n and m
|
|
|
|
*
|
|
|
|
* Who cares? Me. It's useful in solving this problem:
|
2005-07-20 17:40:32 +08:00
|
|
|
* Consider an arbitrary point P[Xp,Yp], find the closest vertex
|
|
|
|
* in the set V.
|
2003-11-12 05:23:33 +08:00
|
|
|
*
|
2005-07-20 17:40:32 +08:00
|
|
|
* Restated this problem is "find values for m and n that are
|
|
|
|
* drive V closest to P"
|
2004-10-27 14:43:52 +08:00
|
|
|
*
|
2003-11-12 05:23:33 +08:00
|
|
|
* A Solution method (there may be a better one?):
|
2004-10-27 14:43:52 +08:00
|
|
|
*
|
2003-11-12 05:23:33 +08:00
|
|
|
* Step 1) bound n to the two closest values for Xp
|
2004-10-27 14:43:52 +08:00
|
|
|
* n_lo = (int) (Xp / h)
|
2003-11-12 05:23:33 +08:00
|
|
|
* n_hi = n_lo + 1
|
2004-10-27 14:43:52 +08:00
|
|
|
*
|
2005-07-20 17:40:32 +08:00
|
|
|
* Step 2) Consider the two closes vertices for each n_lo and
|
|
|
|
* n_hi. The further of the vertices in each pair can
|
|
|
|
* readily be discarded.
|
|
|
|
*
|
2003-11-12 05:23:33 +08:00
|
|
|
* m_lo_n_lo = (int) ( (Yp / r) - 0.5 (n_lo % 2) )
|
|
|
|
* m_hi_n_lo = m_lo_n_lo + 1
|
|
|
|
*
|
|
|
|
* m_lo_n_hi = (int) ( (Yp / r) - 0.5 (n_hi % 2) )
|
|
|
|
* m_hi_n_hi = m_hi_n_hi
|
2004-10-27 14:43:52 +08:00
|
|
|
*
|
2005-07-20 17:40:32 +08:00
|
|
|
* Step 3) compute the distance from P to V1 and V2. Snap to the
|
|
|
|
* closer point.
|
2003-11-12 05:23:33 +08:00
|
|
|
*/
|
2005-07-20 17:40:32 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
gint n_lo;
|
|
|
|
gint n_hi;
|
|
|
|
gint m_lo_n_lo;
|
|
|
|
gint m_hi_n_lo;
|
|
|
|
gint m_lo_n_hi;
|
|
|
|
gint m_hi_n_hi;
|
|
|
|
gint m_n_lo;
|
|
|
|
gint m_n_hi;
|
|
|
|
gdouble r;
|
|
|
|
gdouble h;
|
|
|
|
gint x1;
|
|
|
|
gint x2;
|
|
|
|
gint y1;
|
|
|
|
gint y2;
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
r = selvals.opts.gridspacing;
|
|
|
|
h = COS_1o6PI_RAD * r;
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
n_lo = (gint) x / h;
|
|
|
|
n_hi = n_lo + 1;
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
/* evaluate m candidates for n_lo */
|
2005-07-20 17:40:32 +08:00
|
|
|
m_lo_n_lo = (gint) ((y / r) - 0.5 * (n_lo % 2));
|
2003-11-12 05:23:33 +08:00
|
|
|
m_hi_n_lo = m_lo_n_lo + 1;
|
2005-07-20 17:40:32 +08:00
|
|
|
|
|
|
|
/* figure out which is the better candidate */
|
|
|
|
if (abs ((m_lo_n_lo * r + (0.5 * r * (n_lo % 2))) - y) <
|
|
|
|
abs ((m_hi_n_lo * r + (0.5 * r * (n_lo % 2))) - y))
|
|
|
|
{
|
|
|
|
m_n_lo = m_lo_n_lo;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_n_lo = m_hi_n_lo;
|
|
|
|
}
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
/* evaluate m candidates for n_hi */
|
|
|
|
m_lo_n_hi = (gint) ( (y / r) - 0.5 * (n_hi % 2) );
|
|
|
|
m_hi_n_hi = m_lo_n_hi + 1;
|
2005-07-20 17:40:32 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
/* figure out which is the better candidate */
|
|
|
|
if (abs((m_lo_n_hi * r + (0.5 * r * (n_hi % 2))) - y) <
|
2005-07-20 17:40:32 +08:00
|
|
|
abs((m_hi_n_hi * r + (0.5 * r * (n_hi % 2))) - y))
|
|
|
|
{
|
|
|
|
m_n_hi = m_lo_n_hi;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_n_hi = m_hi_n_hi;
|
|
|
|
}
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2005-07-20 17:40:32 +08:00
|
|
|
/* Now, which is closer to [x,y]? we can use a somewhat
|
|
|
|
* abbreviated form of the distance formula since we only care
|
|
|
|
* about relative values.
|
|
|
|
*/
|
2003-11-12 05:23:33 +08:00
|
|
|
|
|
|
|
x1 = (gint) (n_lo * h);
|
|
|
|
y1 = (gint) (m_n_lo * r + (0.5 * r * (n_lo % 2)));
|
|
|
|
x2 = (gint) (n_hi * h);
|
|
|
|
y2 = (gint) (m_n_hi * r + (0.5 * r * (n_hi % 2)));
|
2004-10-27 14:43:52 +08:00
|
|
|
|
|
|
|
if (((x - x1) * (x - x1) + (y - y1) * (y - y1)) <
|
2005-07-20 17:40:32 +08:00
|
|
|
((x - x2) * (x - x2) + (y - y2) * (y - y2)))
|
|
|
|
{
|
|
|
|
gp->x = x1;
|
|
|
|
gp->y = y1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gp->x = x2;
|
|
|
|
gp->y = y2;
|
|
|
|
}
|
2003-11-12 05:23:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
draw_grid_polar (GdkGC *drawgc)
|
|
|
|
{
|
2005-07-20 17:40:32 +08:00
|
|
|
gdouble inner_radius;
|
|
|
|
gdouble outer_radius;
|
|
|
|
gdouble max_radius = sqrt (SQR (preview_width) + SQR (preview_height));
|
|
|
|
gint current_sectors = 1;
|
|
|
|
PrimeFactors *factors = prime_factors_new (selvals.opts.grid_sectors_desired);
|
|
|
|
for (inner_radius = 0, outer_radius = selvals.opts.grid_radius_min;
|
|
|
|
outer_radius <= max_radius;
|
|
|
|
inner_radius = outer_radius, outer_radius += selvals.opts.grid_radius_interval)
|
|
|
|
{
|
2005-07-14 04:05:41 +08:00
|
|
|
gdouble t;
|
2005-07-20 17:40:32 +08:00
|
|
|
gdouble sector_size = 2 * G_PI / current_sectors;
|
|
|
|
|
2005-07-14 04:05:41 +08:00
|
|
|
gdk_draw_arc (gfig_context->preview->window,
|
|
|
|
drawgc,
|
|
|
|
0,
|
|
|
|
0.5 + (preview_width / 2 - outer_radius),
|
|
|
|
0.5 + (preview_height / 2 - outer_radius),
|
|
|
|
0.5 + (outer_radius * 2),
|
|
|
|
0.5 + (outer_radius * 2),
|
|
|
|
0,
|
|
|
|
360 * 64);
|
2005-07-20 17:40:32 +08:00
|
|
|
|
|
|
|
while ((current_sectors < selvals.opts.grid_sectors_desired)
|
|
|
|
&& (inner_radius * sector_size
|
|
|
|
> prime_factors_lookahead (factors) * selvals.opts.grid_granularity ))
|
|
|
|
{
|
|
|
|
current_sectors *= prime_factors_get (factors);
|
|
|
|
sector_size = 2 * G_PI / current_sectors;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (t = 0 ; t < 2 * G_PI ; t += sector_size)
|
|
|
|
{
|
2005-07-14 04:05:41 +08:00
|
|
|
gdouble normal_x = cos (selvals.opts.grid_rotation+t);
|
|
|
|
gdouble normal_y = sin (selvals.opts.grid_rotation+t);
|
|
|
|
|
|
|
|
gdk_draw_line (gfig_context->preview->window,
|
|
|
|
drawgc,
|
|
|
|
0.5 + (preview_width / 2 + inner_radius * normal_x),
|
|
|
|
0.5 + (preview_height / 2 - inner_radius * normal_y),
|
|
|
|
0.5 + (preview_width / 2 + outer_radius * normal_x),
|
|
|
|
0.5 + (preview_height / 2 - outer_radius * normal_y) );
|
2005-07-20 17:40:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-14 04:05:41 +08:00
|
|
|
prime_factors_delete (factors);
|
2003-11-12 05:23:33 +08:00
|
|
|
}
|
|
|
|
|
2005-07-14 04:05:41 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
static void
|
|
|
|
draw_grid_sq (GdkGC *drawgc)
|
|
|
|
{
|
|
|
|
gint step;
|
|
|
|
gint loop;
|
|
|
|
|
|
|
|
/* Draw the horizontal lines */
|
|
|
|
step = selvals.opts.gridspacing;
|
|
|
|
|
|
|
|
for (loop = 0 ; loop < preview_height ; loop += step)
|
|
|
|
{
|
2004-07-05 04:50:25 +08:00
|
|
|
gdk_draw_line (gfig_context->preview->window,
|
|
|
|
drawgc,
|
|
|
|
0,
|
|
|
|
loop,
|
|
|
|
preview_width,
|
|
|
|
loop);
|
2003-11-12 05:23:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Draw the vertical lines */
|
|
|
|
|
|
|
|
for (loop = 0 ; loop < preview_width ; loop += step)
|
|
|
|
{
|
2004-07-05 04:50:25 +08:00
|
|
|
gdk_draw_line (gfig_context->preview->window,
|
|
|
|
drawgc,
|
|
|
|
loop,
|
|
|
|
0,
|
|
|
|
loop,
|
|
|
|
preview_height);
|
2003-11-12 05:23:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
draw_grid_iso (GdkGC *drawgc)
|
|
|
|
{
|
|
|
|
/* vstep is an int since it's defined from grid size */
|
2004-10-29 05:09:33 +08:00
|
|
|
gint vstep;
|
2003-11-12 05:23:33 +08:00
|
|
|
gdouble loop;
|
|
|
|
gdouble hstep;
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
gdouble diagonal_start;
|
|
|
|
gdouble diagonal_end;
|
|
|
|
gdouble diagonal_width;
|
|
|
|
gdouble diagonal_height;
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
vstep = selvals.opts.gridspacing;
|
|
|
|
hstep = selvals.opts.gridspacing * COS_1o6PI_RAD;
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
/* Draw the vertical lines - These are easy */
|
|
|
|
for (loop = 0 ; loop < preview_width ; loop += hstep){
|
2004-07-05 04:50:25 +08:00
|
|
|
gdk_draw_line (gfig_context->preview->window,
|
|
|
|
drawgc,
|
|
|
|
(gint)loop,
|
|
|
|
(gint)0,
|
|
|
|
(gint)loop,
|
|
|
|
(gint)preview_height);
|
2003-11-12 05:23:33 +08:00
|
|
|
}
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
/* draw diag lines at a Theta of +/- 1/6 Pi Rad */
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
diagonal_start = -(((int)preview_width * TAN_1o6PI_RAD) - (((int)(preview_width * TAN_1o6PI_RAD)) % vstep));
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
diagonal_end = preview_height + (preview_width * TAN_1o6PI_RAD);
|
|
|
|
diagonal_end -= ((int)diagonal_end) % vstep;
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
diagonal_width = preview_width;
|
|
|
|
diagonal_height = preview_width * TAN_1o6PI_RAD;
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
/* Draw diag lines */
|
|
|
|
for (loop = diagonal_start ; loop < diagonal_end ; loop += vstep)
|
|
|
|
{
|
2004-07-05 04:50:25 +08:00
|
|
|
gdk_draw_line (gfig_context->preview->window,
|
|
|
|
drawgc,
|
|
|
|
(gint)0,
|
|
|
|
(gint)loop,
|
|
|
|
(gint)diagonal_width,
|
|
|
|
(gint)loop + diagonal_height);
|
2004-10-27 14:43:52 +08:00
|
|
|
|
2004-07-05 04:50:25 +08:00
|
|
|
gdk_draw_line (gfig_context->preview->window,
|
|
|
|
drawgc,
|
|
|
|
(gint)0,
|
|
|
|
(gint)loop,
|
|
|
|
(gint)diagonal_width,
|
|
|
|
(gint)loop - diagonal_height);
|
2003-11-12 05:23:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GdkGC *
|
|
|
|
gfig_get_grid_gc (GtkWidget *w, gint gctype)
|
|
|
|
{
|
2008-06-29 21:34:05 +08:00
|
|
|
GtkStyle *style = gtk_widget_get_style (w);
|
2003-11-12 05:23:33 +08:00
|
|
|
switch (gctype)
|
|
|
|
{
|
|
|
|
case GFIG_BLACK_GC:
|
2008-06-29 21:34:05 +08:00
|
|
|
return style->black_gc;
|
2003-11-12 05:23:33 +08:00
|
|
|
case GFIG_WHITE_GC:
|
2008-06-29 21:34:05 +08:00
|
|
|
return style->white_gc;
|
2003-11-12 05:23:33 +08:00
|
|
|
case GFIG_GREY_GC:
|
2008-06-29 21:34:05 +08:00
|
|
|
return grid_hightlight_drawgc;
|
2003-11-12 05:23:33 +08:00
|
|
|
case GTK_STATE_NORMAL:
|
2008-06-29 21:34:05 +08:00
|
|
|
return style->bg_gc[GTK_STATE_NORMAL];
|
2003-11-12 05:23:33 +08:00
|
|
|
case GTK_STATE_ACTIVE:
|
2008-06-29 21:34:05 +08:00
|
|
|
return style->bg_gc[GTK_STATE_ACTIVE];
|
2003-11-12 05:23:33 +08:00
|
|
|
case GTK_STATE_PRELIGHT:
|
2008-06-29 21:34:05 +08:00
|
|
|
return style->bg_gc[GTK_STATE_PRELIGHT];
|
2003-11-12 05:23:33 +08:00
|
|
|
case GTK_STATE_SELECTED:
|
2008-06-29 21:34:05 +08:00
|
|
|
return style->bg_gc[GTK_STATE_SELECTED];
|
2003-11-12 05:23:33 +08:00
|
|
|
case GTK_STATE_INSENSITIVE:
|
2008-06-29 21:34:05 +08:00
|
|
|
return style->bg_gc[GTK_STATE_INSENSITIVE];
|
2003-11-12 05:23:33 +08:00
|
|
|
default:
|
|
|
|
g_warning ("Unknown type for grid colouring\n");
|
2008-06-29 21:34:05 +08:00
|
|
|
return style->bg_gc[GTK_STATE_PRELIGHT];
|
2003-11-12 05:23:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
draw_grid (void)
|
|
|
|
{
|
|
|
|
GdkGC *drawgc;
|
2005-07-20 17:40:32 +08:00
|
|
|
|
2003-11-12 05:23:33 +08:00
|
|
|
/* Get the size of the preview and calc where the lines go */
|
|
|
|
/* Draw in prelight to start with... */
|
|
|
|
/* Always start in the upper left corner for rect.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((preview_width < selvals.opts.gridspacing &&
|
2004-07-05 04:50:25 +08:00
|
|
|
preview_height < selvals.opts.gridspacing))
|
2003-11-12 05:23:33 +08:00
|
|
|
{
|
|
|
|
/* Don't draw if they don't fit */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selvals.opts.drawgrid)
|
2004-07-05 04:50:25 +08:00
|
|
|
drawgc = gfig_get_grid_gc (gfig_context->preview, grid_gc_type);
|
2003-11-12 05:23:33 +08:00
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (selvals.opts.gridtype == RECT_GRID)
|
|
|
|
draw_grid_sq (drawgc);
|
|
|
|
else if (selvals.opts.gridtype == POLAR_GRID)
|
|
|
|
draw_grid_polar (drawgc);
|
|
|
|
else if (selvals.opts.gridtype == ISO_GRID)
|
|
|
|
draw_grid_iso (drawgc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|