my_round() function instead of ROUND() macro

This commit is contained in:
Stefan Frederik 2021-12-17 15:40:19 +01:00
parent 993c3a24c2
commit 0c9eff16d9
6 changed files with 30 additions and 21 deletions

View File

@ -623,10 +623,12 @@ void enable_layers(void)
{ {
int i; int i;
char tmp[50]; char tmp[50];
const char *en;
xctx->n_active_layers = 0; xctx->n_active_layers = 0;
for(i = 0; i< cadlayers; i++) { for(i = 0; i< cadlayers; i++) {
my_snprintf(tmp, S(tmp), "enable_layer(%d)",i); my_snprintf(tmp, S(tmp), "enable_layer(%d)",i);
if(tclgetvar(tmp)[0] == '0') xctx->enable_layer[i] = 0; en = tclgetvar(tmp);
if(!en || en[0] == '0') xctx->enable_layer[i] = 0;
else { else {
xctx->enable_layer[i] = 1; xctx->enable_layer[i] = 1;
if(i>=7) { if(i>=7) {
@ -2296,6 +2298,13 @@ int text_bbox(const char *str,double xscale, double yscale,
return 1; return 1;
} }
/* does not exist in C89 */
double my_round(double a)
{
/* return 0.0 or -0.0 if a == 0.0 or -0.0 */
return (a == 0.0) ? a : (a > 0.0) ? floor(a + 0.5) : ceil(a - 0.5);
}
int place_text(int draw_text, double mx, double my) int place_text(int draw_text, double mx, double my)
{ {
char *txt; char *txt;
@ -2319,7 +2328,7 @@ int place_text(int draw_text, double mx, double my)
dbg(1, "place_text(): hsize=%s vsize=%s\n",tclgetvar("hsize"), tclgetvar("vsize") ); dbg(1, "place_text(): hsize=%s vsize=%s\n",tclgetvar("hsize"), tclgetvar("vsize") );
txt = (char *)tclgetvar("retval"); txt = (char *)tclgetvar("retval");
if(!strcmp(txt,"")) return 0; /* dont allocate text object if empty string given */ if(!txt || !strcmp(txt,"")) return 0; /* dont allocate text object if empty string given */
xctx->push_undo(); xctx->push_undo();
check_text_storage(); check_text_storage();
t->txt_ptr=NULL; t->txt_ptr=NULL;

View File

@ -220,8 +220,8 @@ int callback(const char *winpath, int event, int mx, int my, KeySym key,
} }
xctx->mousex=X_TO_XSCHEM(mx); xctx->mousex=X_TO_XSCHEM(mx);
xctx->mousey=Y_TO_XSCHEM(my); xctx->mousey=Y_TO_XSCHEM(my);
xctx->mousex_snap=ROUND(xctx->mousex / c_snap) * c_snap; xctx->mousex_snap=my_round(xctx->mousex / c_snap) * c_snap;
xctx->mousey_snap=ROUND(xctx->mousey / c_snap) * c_snap; xctx->mousey_snap=my_round(xctx->mousey / c_snap) * c_snap;
my_snprintf(str, S(str), "mouse = %.16g %.16g - selected: %d path: %s", my_snprintf(str, S(str), "mouse = %.16g %.16g - selected: %d path: %s",
xctx->mousex_snap, xctx->mousey_snap, xctx->lastsel, xctx->sch_path[xctx->currsch] ); xctx->mousex_snap, xctx->mousey_snap, xctx->lastsel, xctx->sch_path[xctx->currsch] );
statusmsg(str,1); statusmsg(str,1);
@ -597,8 +597,8 @@ int callback(const char *winpath, int event, int mx, int my, KeySym key,
if(xctx->semaphore >= 2) break; if(xctx->semaphore >= 2) break;
if(!(xctx->ui_state & STARTWIRE)){ if(!(xctx->ui_state & STARTWIRE)){
find_closest_net_or_symbol_pin(xctx->mousex, xctx->mousey, &x, &y); find_closest_net_or_symbol_pin(xctx->mousex, xctx->mousey, &x, &y);
xctx->mx_double_save = ROUND(x / c_snap) * c_snap; xctx->mx_double_save = my_round(x / c_snap) * c_snap;
xctx->my_double_save = ROUND(y / c_snap) * c_snap; xctx->my_double_save = my_round(y / c_snap) * c_snap;
new_wire(PLACE, x, y); new_wire(PLACE, x, y);
} }
else { else {
@ -1432,18 +1432,18 @@ int callback(const char *winpath, int event, int mx, int my, KeySym key,
clear_drawing(); clear_drawing();
for(i=0;i<5000; i++) { for(i=0;i<5000; i++) {
w = (1+ROUND(rand()%1200/snap))*snap; w = (1+my_round(rand()%1200/snap))*snap;
x1 = 40000+ROUND(rand()%30000/snap)*snap; x1 = 40000+my_round(rand()%30000/snap)*snap;
y1 = 40000+ROUND(rand()%30000/snap)*snap; y1 = 40000+my_round(rand()%30000/snap)*snap;
x2=x1+w; x2=x1+w;
y2=y1; y2=y1;
ORDER(x1, y1, x2, y2); ORDER(x1, y1, x2, y2);
storeobject(-1, x1, y1, x2, y2 ,WIRE,0,0,NULL); storeobject(-1, x1, y1, x2, y2 ,WIRE,0,0,NULL);
} }
for(i=0;i<5000; i++) { for(i=0;i<5000; i++) {
w = (1+ROUND(rand()%1200/snap))*snap; w = (1+my_round(rand()%1200/snap))*snap;
x1 = 40000+ROUND(rand()%30000/snap)*snap; x1 = 40000+my_round(rand()%30000/snap)*snap;
y1 = 40000+ROUND(rand()%30000/snap)*snap; y1 = 40000+my_round(rand()%30000/snap)*snap;
x2=x1; x2=x1;
y2=y1+w; y2=y1+w;
ORDER(x1, y1, x2, y2); ORDER(x1, y1, x2, y2);
@ -1681,8 +1681,8 @@ int callback(const char *winpath, int event, int mx, int my, KeySym key,
double x, y; double x, y;
find_closest_net_or_symbol_pin(xctx->mousex, xctx->mousey, &x, &y); find_closest_net_or_symbol_pin(xctx->mousex, xctx->mousey, &x, &y);
xctx->mx_double_save = ROUND(x / c_snap) * c_snap; xctx->mx_double_save = my_round(x / c_snap) * c_snap;
xctx->my_double_save = ROUND(y / c_snap) * c_snap; xctx->my_double_save = my_round(y / c_snap) * c_snap;
new_wire(PLACE, x, y); new_wire(PLACE, x, y);
xctx->ui_state &=~MENUSTARTSNAPWIRE; xctx->ui_state &=~MENUSTARTSNAPWIRE;
break; break;

View File

@ -418,12 +418,12 @@ void draw_selection(GC g, int interruptable)
drawtemparc(g, ADD, xctx->rx1, xctx->ry1, drawtemparc(g, ADD, xctx->rx1, xctx->ry1,
fabs(xctx->arc[c][n].r+xctx->deltax), angle, xctx->arc[c][n].b); fabs(xctx->arc[c][n].r+xctx->deltax), angle, xctx->arc[c][n].b);
} else if(xctx->arc[c][n].sel==SELECTED3) { } else if(xctx->arc[c][n].sel==SELECTED3) {
angle = ROUND(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+xctx->arc[c][n].b, 360.)); angle = my_round(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+xctx->arc[c][n].b, 360.));
if(angle<0.) angle +=360.; if(angle<0.) angle +=360.;
if(angle==0) angle=360.; if(angle==0) angle=360.;
drawtemparc(g, ADD, xctx->rx1, xctx->ry1, xctx->arc[c][n].r, xctx->arc[c][n].a, angle); drawtemparc(g, ADD, xctx->rx1, xctx->ry1, xctx->arc[c][n].r, xctx->arc[c][n].a, angle);
} else if(xctx->arc[c][n].sel==SELECTED2) { } else if(xctx->arc[c][n].sel==SELECTED2) {
angle = ROUND(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+angle, 360.)); angle = my_round(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+angle, 360.));
if(angle<0.) angle +=360.; if(angle<0.) angle +=360.;
drawtemparc(g, ADD, xctx->rx1, xctx->ry1, xctx->arc[c][n].r, angle, xctx->arc[c][n].b); drawtemparc(g, ADD, xctx->rx1, xctx->ry1, xctx->arc[c][n].r, angle, xctx->arc[c][n].b);
} }
@ -1324,13 +1324,13 @@ void move_objects(int what, int merge, double dx, double dy)
if(xctx->arc[c][n].r+xctx->deltax) xctx->arc[c][n].r = fabs(xctx->arc[c][n].r+xctx->deltax); if(xctx->arc[c][n].r+xctx->deltax) xctx->arc[c][n].r = fabs(xctx->arc[c][n].r+xctx->deltax);
xctx->arc[c][n].a = angle; xctx->arc[c][n].a = angle;
} else if(xctx->arc[c][n].sel == SELECTED2) { } else if(xctx->arc[c][n].sel == SELECTED2) {
angle = ROUND(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+angle, 360.)); angle = my_round(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+angle, 360.));
if(angle<0.) angle +=360.; if(angle<0.) angle +=360.;
xctx->arc[c][n].x = xctx->rx1; xctx->arc[c][n].x = xctx->rx1;
xctx->arc[c][n].y = xctx->ry1; xctx->arc[c][n].y = xctx->ry1;
xctx->arc[c][n].a = angle; xctx->arc[c][n].a = angle;
} else if(xctx->arc[c][n].sel==SELECTED3) { } else if(xctx->arc[c][n].sel==SELECTED3) {
angle = ROUND(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+xctx->arc[c][n].b, 360.)); angle = my_round(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+xctx->arc[c][n].b, 360.));
if(angle<0.) angle +=360.; if(angle<0.) angle +=360.;
if(angle==0) angle=360.; if(angle==0) angle=360.;
xctx->arc[c][n].x = xctx->rx1; xctx->arc[c][n].x = xctx->rx1;

View File

@ -2436,7 +2436,7 @@ void descend_symbol(void)
} }
/* 20111023 align selected object to current grid setting */ /* 20111023 align selected object to current grid setting */
#define SNAP_TO_GRID(a) (a=ROUND(( a)/c_snap)*c_snap ) #define SNAP_TO_GRID(a) (a=my_round(( a)/c_snap)*c_snap )
void round_schematic_to_grid(double c_snap) void round_schematic_to_grid(double c_snap)
{ {
int i, c, n, p; int i, c, n, p;

View File

@ -413,7 +413,7 @@ const char *list_tokens(const char *s, int with_quotes)
/* state machine that parses a string made up of <token>=<value> ... */ /* state machine that parses a string made up of <token>=<value> ... */
/* couples and returns the value of the given token */ /* couples and returns the value of the given token */
/* if s==NULL or no match return empty string */ /* if s==NULL or no match return empty string */
/* NULL tok NOT ALLOWED !!!!!!!! */ /* NULL tok is used with NULL s to free internal storage (destructor) */
/* never returns NULL... */ /* never returns NULL... */
/* with_quotes: */ /* with_quotes: */
/* bit 0: */ /* bit 0: */

View File

@ -303,7 +303,6 @@ extern char win_temp_dir[PATH_MAX];
#define CLIP(x,a,b) ((x) < a ? (a) : (x) > b ? (b) : (x)) #define CLIP(x,a,b) ((x) < a ? (a) : (x) > b ? (b) : (x))
#define MINOR(a,b) ( (a) <= (b) ? (a) : (b) ) #define MINOR(a,b) ( (a) <= (b) ? (a) : (b) )
#define ROUND(a) ((a) > 0.0 ? ceil((a) - 0.5) : floor((a) + 0.5))
#define IS_LABEL_SH_OR_PIN(type) (!(strcmp(type,"label") && strcmp(type,"ipin") && strcmp(type,"opin") && \ #define IS_LABEL_SH_OR_PIN(type) (!(strcmp(type,"label") && strcmp(type,"ipin") && strcmp(type,"opin") && \
strcmp(type,"show_label") && strcmp(type,"iopin"))) strcmp(type,"show_label") && strcmp(type,"iopin")))
@ -1161,6 +1160,7 @@ extern void my_realloc(int id, void *ptr,size_t size);
extern void *my_calloc(int id, size_t nmemb, size_t size); extern void *my_calloc(int id, size_t nmemb, size_t size);
extern void my_free(int id, void *ptr); extern void my_free(int id, void *ptr);
extern size_t my_strcat(int id, char **, const char *); extern size_t my_strcat(int id, char **, const char *);
extern double my_round(double a);
extern const char *subst_token(const char *s, const char *tok, const char *new_val); extern const char *subst_token(const char *s, const char *tok, const char *new_val);
extern void new_prop_string(int i, const char *old_prop,int fast, int dis_uniq_names); extern void new_prop_string(int i, const char *old_prop,int fast, int dis_uniq_names);
extern void hash_name(char *token, int remove); extern void hash_name(char *token, int remove);