Commit 0a29b4b5 authored by Sven Neumann's avatar Sven Neumann Committed by Sven Neumann
Browse files

declare variables at the start of the scope. Apply GIMP coding style to

2005-07-20  Sven Neumann  <sven@gimp.org>

	* plug-ins/gfig/gfig-grid.c: declare variables at the start of the
	scope. Apply GIMP coding style to the new polar grid code.
parent 3e74368a
2005-07-20 Sven Neumann <sven@gimp.org>
* plug-ins/gfig/gfig-grid.c: declare variables at the start of the
scope. Apply GIMP coding style to the new polar grid code.
2005-07-19 Sven Neumann <sven@gimp.org>
* app/tools/gimpvectortool.c (gimp_vector_tool_key_press): return
......
......@@ -41,19 +41,20 @@
#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 */
static GdkGC *grid_hightlight_drawgc;
static GdkGC *grid_hightlight_drawgc = NULL;
gint grid_gc_type = GTK_STATE_NORMAL;
static void draw_grid_polar (GdkGC *drawgc);
static void draw_grid_sq (GdkGC *drawgc);
static void draw_grid_iso (GdkGC *drawgc);
static GdkGC *gfig_get_grid_gc (GtkWidget *widget,
static GdkGC * gfig_get_grid_gc (GtkWidget *widget,
gint gctype);
static void find_grid_pos_polar (GdkPoint *p,
GdkPoint *gp);
/********** PrimeFactors for Shaneyfelt-style Polar Grid **********
* Quickly factor any number up to 17160
* Correctly factors numbers up to 131 * 131 - 1
......@@ -67,18 +68,19 @@ typedef struct
gint index;
} PrimeFactors;
static char
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};
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
static gint
prime_factors_get (PrimeFactors *this)
{
this->current = this->next;
while (this->index <= PRIMES_MAX_INDEX)
{
if (this->remaining % primes[this->index]==0) // divisible
if (this->remaining % primes[this->index] == 0) // divisible
{
this->remaining /= primes[this->index];
this->next = primes[this->index];
......@@ -88,6 +90,7 @@ prime_factors_get (PrimeFactors *this)
}
this->next = this->remaining;
this->remaining = 1;
return this->current;
}
......@@ -108,16 +111,18 @@ prime_factors_reset (PrimeFactors *this)
static PrimeFactors *
prime_factors_new (gint n)
{
PrimeFactors *this = (PrimeFactors*)g_malloc (sizeof(PrimeFactors));
PrimeFactors *this = g_new (PrimeFactors, 1);
this->product = n;
prime_factors_reset (this);
return this;
}
static void
prime_factors_delete (PrimeFactors* this)
{
free (this);
g_free (this);
}
/********** ********** **********/
......@@ -125,17 +130,21 @@ prime_factors_delete (PrimeFactors* this)
static gdouble
sector_size_at_radius (gdouble inner_radius)
{
PrimeFactors *factors = prime_factors_new(selvals.opts.grid_sectors_desired);
PrimeFactors *factors = prime_factors_new (selvals.opts.grid_sectors_desired);
gint current_sectors = 1;
gdouble sector_size = 2*G_PI / current_sectors;
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))
> (prime_factors_lookahead (factors) *
selvals.opts.grid_granularity)))
{
current_sectors *= prime_factors_get(factors);
sector_size = 2*G_PI / current_sectors;
current_sectors *= prime_factors_get (factors);
sector_size = 2 * G_PI / current_sectors;
}
prime_factors_delete(factors);
return sector_size;
}
......@@ -149,20 +158,27 @@ find_grid_pos_polar (GdkPoint *p,
gdouble py = p->y - cy;
gdouble x = 0;
gdouble y = 0;
gdouble r = sqrt (px*px+py*py);
if (r>=selvals.opts.grid_radius_min*.5)
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))
* (gint) (0.5 + ((r - selvals.opts.grid_radius_min) /
selvals.opts.grid_radius_interval))
+ selvals.opts.grid_radius_min;
gdouble t = atan2 (py, px) + 2*G_PI;
gdouble sectorSize = sector_size_at_radius (r);
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 ))
+ (gint) (0.5 + ((t - selvals.opts.grid_rotation) / sectorSize))
* sectorSize;
x = r * cos (t);
y = r * sin (t);
}
gp->x = x + cx;
gp->y = y + cy;
}
......@@ -252,17 +268,21 @@ find_grid_pos (GdkPoint *p,
/*
* This really needs a picture to show the math...
*
* 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.
* 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.
*
* - Let one line segment between vertices be r
* - Define the value of r as the grid spacing
* - 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
* - 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
* - Let m to be any integer
* - 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)
* - 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)
*
* Consider a Vertex V at the Cartesian location [Xv, Yv]
*
......@@ -272,9 +292,11 @@ find_grid_pos (GdkPoint *p,
* for all integers n and m
*
* Who cares? Me. It's useful in solving this problem:
* Consider an arbitrary point P[Xp,Yp], find the closest vertex in the set V.
* Consider an arbitrary point P[Xp,Yp], find the closest vertex
* in the set V.
*
* Restated this problem is "find values for m and n that are drive V closest to P"
* Restated this problem is "find values for m and n that are
* drive V closest to P"
*
* A Solution method (there may be a better one?):
*
......@@ -282,16 +304,20 @@ find_grid_pos (GdkPoint *p,
* n_lo = (int) (Xp / h)
* n_hi = n_lo + 1
*
* 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.
* 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.
*
* 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
*
* Step 3) compute the distance from P to V1 and V2. Snap to the closer point.
* Step 3) compute the distance from P to V1 and V2. Snap to the
* closer point.
*/
gint n_lo;
gint n_hi;
gint m_lo_n_lo;
......@@ -314,31 +340,39 @@ find_grid_pos (GdkPoint *p,
n_hi = n_lo + 1;
/* evaluate m candidates for n_lo */
m_lo_n_lo = (gint) ( (y / r) - 0.5 * (n_lo % 2) );
m_lo_n_lo = (gint) ((y / r) - 0.5 * (n_lo % 2));
m_hi_n_lo = m_lo_n_lo + 1;
/* 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)) {
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 {
else
{
m_n_lo = m_hi_n_lo;
}
/* 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;
/* figure out which is the better candidate */
if (abs((m_lo_n_hi * r + (0.5 * r * (n_hi % 2))) - y) <
abs((m_hi_n_hi * r + (0.5 * r * (n_hi % 2))) - y)) {
abs((m_hi_n_hi * r + (0.5 * r * (n_hi % 2))) - y))
{
m_n_hi = m_lo_n_hi;
}
else {
else
{
m_n_hi = m_hi_n_hi;
}
/* 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. */
/* 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.
*/
x1 = (gint) (n_lo * h);
y1 = (gint) (m_n_lo * r + (0.5 * r * (n_lo % 2)));
......@@ -346,15 +380,16 @@ find_grid_pos (GdkPoint *p,
y2 = (gint) (m_n_hi * r + (0.5 * r * (n_hi % 2)));
if (((x - x1) * (x - x1) + (y - y1) * (y - y1)) <
((x - x2) * (x - x2) + (y - y2) * (y - y2))) {
((x - x2) * (x - x2) + (y - y2) * (y - y2)))
{
gp->x = x1;
gp->y = y1;
}
else {
else
{
gp->x = x2;
gp->y = y2;
}
}
}
......@@ -363,15 +398,15 @@ draw_grid_polar (GdkGC *drawgc)
{
gdouble inner_radius;
gdouble outer_radius;
gdouble max_radius = sqrt (preview_width * preview_width + preview_height * preview_height);
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;
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 )
inner_radius = outer_radius, outer_radius += selvals.opts.grid_radius_interval)
{
gdouble t;
gdouble sector_size = 2*G_PI / current_sectors;
gdouble sector_size = 2 * G_PI / current_sectors;
gdk_draw_arc (gfig_context->preview->window,
drawgc,
......@@ -383,14 +418,15 @@ draw_grid_polar (GdkGC *drawgc)
0,
360 * 64);
while ( (current_sectors < selvals.opts.grid_sectors_desired)
&& ( inner_radius * sector_size
> prime_factors_lookahead (factors) * selvals.opts.grid_granularity ) )
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;
sector_size = 2 * G_PI / current_sectors;
}
for (t = 0 ; t < 2*G_PI ; t += sector_size)
for (t = 0 ; t < 2 * G_PI ; t += sector_size)
{
gdouble normal_x = cos (selvals.opts.grid_rotation+t);
gdouble normal_y = sin (selvals.opts.grid_rotation+t);
......@@ -403,6 +439,7 @@ draw_grid_polar (GdkGC *drawgc)
0.5 + (preview_height / 2 - outer_radius * normal_y) );
}
}
prime_factors_delete (factors);
}
......@@ -525,6 +562,7 @@ void
draw_grid (void)
{
GdkGC *drawgc;
/* 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.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment