Skip to content

Commit

Permalink
drawing: move coord_list* functions to coordlist_ops
Browse files Browse the repository at this point in the history
  • Loading branch information
bk138 committed Mar 25, 2024
1 parent 70e8f69 commit 91a8dff
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 116 deletions.
104 changes: 104 additions & 0 deletions src/coordlist_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,110 @@ static GList *build_section_list(GList *const coords,
return section_list;
}

void coord_list_prepend (GromitData *data,
GdkDevice* dev,
gint x,
gint y,
gint width)
{
/* get the data for this device */
GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev);

GromitStrokeCoordinate *point;

point = g_malloc (sizeof (GromitStrokeCoordinate));
point->x = x;
point->y = y;
point->width = width;

devdata->coordlist = g_list_prepend (devdata->coordlist, point);
}


void coord_list_free (GromitData *data,
GdkDevice* dev)
{
/* get the data for this device */
GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev);

GList *ptr;
ptr = devdata->coordlist;

while (ptr)
{
g_free (ptr->data);
ptr = ptr->next;
}

g_list_free (devdata->coordlist);

devdata->coordlist = NULL;
}

/*
* for double-ended arrows, two separate calls are required
*/

gboolean coord_list_get_arrow_param (GromitData *data,
GdkDevice *dev,
gint search_radius,
GromitArrowType arrow_end,
gint *x0,
gint *y0,
gint *ret_width,
gfloat *ret_direction)
{
gint r2, dist;
gboolean success = FALSE;
GromitStrokeCoordinate *cur_point, *valid_point;
/* get the data for this device */
GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev);
GList *ptr = devdata->coordlist;
gfloat width;

valid_point = NULL;

if (ptr)
{
if (arrow_end == GROMIT_ARROW_START)
ptr = g_list_last(ptr);
cur_point = ptr->data;
*x0 = cur_point->x;
*y0 = cur_point->y;
r2 = search_radius * search_radius;
dist = 0;

while (ptr && dist < r2)
{
if (arrow_end == GROMIT_ARROW_END)
ptr = ptr->next;
else
ptr = ptr->prev;
if (ptr)
{
cur_point = ptr->data;
dist = (cur_point->x - *x0) * (cur_point->x - *x0) +
(cur_point->y - *y0) * (cur_point->y - *y0);
width = cur_point->width * devdata->cur_context->arrowsize;
if (width * 2 <= dist &&
(!valid_point || valid_point->width < cur_point->width))
valid_point = cur_point;
}
}

if (valid_point)
{
*ret_width = MAX (valid_point->width * devdata->cur_context->arrowsize,
2);
*ret_direction = atan2 (*y0 - valid_point->y, *x0 - valid_point->x);
success = TRUE;
}
}

return success;
}


// ----------------------- orthogonalize path ------------------------

void orthogonalize(GList *const coords,
Expand Down
10 changes: 10 additions & 0 deletions src/coordlist_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@

#include "main.h"

gboolean coord_list_get_arrow_param (GromitData *data,
GdkDevice *dev,
gint search_radius,
GromitArrowType arrow_end,
gint *x0,
gint *y0,
gint *ret_width,
gfloat *ret_direction);
void coord_list_prepend (GromitData *data, GdkDevice* dev, gint x, gint y, gint width);
void coord_list_free (GromitData *data, GdkDevice* dev);
gboolean snap_ends(GList *coords, gint max_distance);
void orthogonalize(GList *coords, gint max_angular_deviation, gint min_ortho_len);
void add_points(GList *coords, gfloat max_distance);
Expand Down
104 changes: 0 additions & 104 deletions src/drawing.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,107 +105,3 @@ void draw_arrow (GromitData *data,
data->painted = 1;
}


void coord_list_prepend (GromitData *data,
GdkDevice* dev,
gint x,
gint y,
gint width)
{
/* get the data for this device */
GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev);

GromitStrokeCoordinate *point;

point = g_malloc (sizeof (GromitStrokeCoordinate));
point->x = x;
point->y = y;
point->width = width;

devdata->coordlist = g_list_prepend (devdata->coordlist, point);
}


void coord_list_free (GromitData *data,
GdkDevice* dev)
{
/* get the data for this device */
GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev);

GList *ptr;
ptr = devdata->coordlist;

while (ptr)
{
g_free (ptr->data);
ptr = ptr->next;
}

g_list_free (devdata->coordlist);

devdata->coordlist = NULL;
}

/*
* for double-ended arrows, two separate calls are required
*/

gboolean coord_list_get_arrow_param (GromitData *data,
GdkDevice *dev,
gint search_radius,
GromitArrowType arrow_end,
gint *x0,
gint *y0,
gint *ret_width,
gfloat *ret_direction)
{
gint r2, dist;
gboolean success = FALSE;
GromitStrokeCoordinate *cur_point, *valid_point;
/* get the data for this device */
GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev);
GList *ptr = devdata->coordlist;
gfloat width;

valid_point = NULL;

if (ptr)
{
if (arrow_end == GROMIT_ARROW_START)
ptr = g_list_last(ptr);
cur_point = ptr->data;
*x0 = cur_point->x;
*y0 = cur_point->y;
r2 = search_radius * search_radius;
dist = 0;

while (ptr && dist < r2)
{
if (arrow_end == GROMIT_ARROW_END)
ptr = ptr->next;
else
ptr = ptr->prev;
if (ptr)
{
cur_point = ptr->data;
dist = (cur_point->x - *x0) * (cur_point->x - *x0) +
(cur_point->y - *y0) * (cur_point->y - *y0);
width = cur_point->width * devdata->cur_context->arrowsize;
if (width * 2 <= dist &&
(!valid_point || valid_point->width < cur_point->width))
valid_point = cur_point;
}
}

if (valid_point)
{
*ret_width = MAX (valid_point->width * devdata->cur_context->arrowsize,
2);
*ret_direction = atan2 (*y0 - valid_point->y, *x0 - valid_point->x);
success = TRUE;
}
}

return success;
}

12 changes: 0 additions & 12 deletions src/drawing.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,4 @@ typedef struct
void draw_line (GromitData *data, GdkDevice *dev, gint x1, gint y1, gint x2, gint y2);
void draw_arrow (GromitData *data, GdkDevice *dev, gint x1, gint y1, gint width, gfloat direction);

gboolean coord_list_get_arrow_param (GromitData *data,
GdkDevice *dev,
gint search_radius,
GromitArrowType arrow_end,
gint *x0,
gint *y0,
gint *ret_width,
gfloat *ret_direction);
void coord_list_prepend (GromitData *data, GdkDevice* dev, gint x, gint y, gint width);
void coord_list_free (GromitData *data, GdkDevice* dev);


#endif

0 comments on commit 91a8dff

Please sign in to comment.