diff --git a/coord.c b/coord.c index b9ee3e2..9bf2b90 100644 --- a/coord.c +++ b/coord.c @@ -1,8 +1,8 @@ /* * coord.c - Coordinate representation and basic operations * - * Written 2009 by Werner Almesberger - * Copyright 2009 by Werner Almesberger + * Written 2009, 2010 by Werner Almesberger + * Copyright 2009, 2010 by Werner Almesberger * * 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 @@ -13,6 +13,7 @@ #include +#include "util.h" #include "coord.h" @@ -153,22 +154,12 @@ double theta(struct coord c, struct coord p) /* ----- sorting coordinates ----------------------------------------------- */ -void swap_coord(unit_type *a, unit_type *b) -{ - unit_type tmp; - - tmp = *a; - *a = *b; - *b = tmp; -} - - void sort_coord(struct coord *min, struct coord *max) { if (min->x > max->x) - swap_coord(&min->x, &max->x); + swap(min->x, max->x); if (min->y > max->y) - swap_coord(&min->y, &max->y); + swap(min->y, max->y); } diff --git a/coord.h b/coord.h index 6955160..dd9f235 100644 --- a/coord.h +++ b/coord.h @@ -1,8 +1,8 @@ /* * coord.h - Coordinate representation and basic operations * - * Written 2009 by Werner Almesberger - * Copyright 2009 by Werner Almesberger + * Written 2009, 2010 by Werner Almesberger + * Copyright 2009, 2010 by Werner Almesberger * * 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 @@ -87,7 +87,6 @@ struct coord rotate_r(struct coord c, unit_type r, double angle); double theta_vec(struct coord v); double theta(struct coord c, struct coord p); -void swap_coord(unit_type *a, unit_type *b); void sort_coord(struct coord *min, struct coord *max); unit_type dist_point(struct coord a, struct coord b); diff --git a/gui_meas.c b/gui_meas.c index 5776534..89b1f5f 100644 --- a/gui_meas.c +++ b/gui_meas.c @@ -189,7 +189,8 @@ static void meas_highlight_b(void) struct pix_buf *draw_move_meas(struct inst *inst, struct coord pos, int i) { - return draw_move_line_common(inst, inst->u.meas.end, pos, i); + return draw_move_line_common(inst, inst->u.meas.end, pos, + inst->obj->u.meas.inverted ? 1-i : i); } @@ -349,6 +350,7 @@ static struct inst *vec_at(const struct vec *vec, struct coord pos) void begin_drag_move_meas(struct inst *inst, int i) { const struct meas *meas = &inst->obj->u.meas; + struct coord a, b; switch (meas->type) { case mt_xy_next: @@ -367,14 +369,24 @@ void begin_drag_move_meas(struct inst *inst, int i) abort(); } highlight = meas_highlight_b; + + /* + * We're setting up the same conditions as after picking the first + * point when making a new measurement. Thus, we set meas_inst to the + * vector to the endpoint we're not moving. + */ + a = inst->base; + b = inst->u.meas.end; + if (inst->obj->u.meas.inverted) + swap(a, b); switch (i) { case 0: mode = meas->type < 3 ? next_to_min : max_to_min; - meas_inst = vec_at(inst->obj->u.meas.high, inst->u.meas.end); + meas_inst = vec_at(inst->obj->u.meas.high, b); break; case 1: mode = min_to_next_or_max; - meas_inst = vec_at(inst->obj->base, inst->base); + meas_inst = vec_at(inst->obj->base, a); break; default: abort(); diff --git a/gui_util.c b/gui_util.c index f22c009..36fe66d 100644 --- a/gui_util.c +++ b/gui_util.c @@ -65,19 +65,12 @@ struct pix_buf *save_pix_buf(GdkDrawable *da, int xa, int ya, int xb, int yb, int border) { struct pix_buf *buf; - int tmp; int w, h; - if (xa > xb) { - tmp = xa; - xa = xb; - xb = tmp; - } - if (ya > yb) { - tmp = ya; - ya = yb; - yb = tmp; - } + if (xa > xb) + swap(xa, xb); + if (ya > yb) + swap(ya, yb); buf = alloc_type(struct pix_buf); buf->da = da; buf->x = xa-border; diff --git a/kicad.c b/kicad.c index 4eb4b50..9ab863e 100644 --- a/kicad.c +++ b/kicad.c @@ -30,23 +30,13 @@ static void kicad_centric(struct coord a, struct coord b, struct coord *center, struct coord *size) { struct coord min, max; - unit_type tmp; min.x = units_to_kicad(a.x); min.y = units_to_kicad(a.y); max.x = units_to_kicad(b.x); max.y = units_to_kicad(b.y); - if (min.x > max.x) { - tmp = min.x; - min.x = max.x; - max.x = tmp; - } - if (min.y > max.y) { - tmp = min.y; - min.y = max.y; - max.y = tmp; - } + sort_coord(&min, &max); size->x = max.x-min.x; size->y = max.y-min.y; diff --git a/util.h b/util.h index 426f0ae..6574030 100644 --- a/util.h +++ b/util.h @@ -1,8 +1,8 @@ /* * util.h - Common utility functions * - * Written 2006, 2009 by Werner Almesberger - * Copyright 2006, 2009 Werner Almesberger + * Written 2006, 2009, 2010 by Werner Almesberger + * Copyright 2006, 2009, 2010 Werner Almesberger * * 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 @@ -51,6 +51,11 @@ strnalloc_tmp[n] = 0; \ strnalloc_tmp; }) +#define swap(a, b) \ + ({ typeof(a) swap_tmp = (a); \ + (a) = (b); \ + (b) = swap_tmp; }) + char *stralloc_vprintf(const char *fmt, va_list ap); char *stralloc_printf(const char *fmt, ...)