"lazy man" (good enough for schematics) aproximated polygon clipping using the underlying xorg 16 bit integer poly clipping engine, by projecting outer vertices to the 16 bit signed coordinate system edges

This commit is contained in:
Stefan Frederik 2020-12-05 13:58:44 +01:00
parent f9f9de3371
commit fe1f7c57a7
3 changed files with 42 additions and 5 deletions

View File

@ -69,6 +69,38 @@ int clip( double *xa,double *ya,double *xb,double *yb)
}
}
void clip_xy_to_short(double x, double y, short *sx, short *sy)
{
double ax, ay;
double r;
ax = fabs(x);
ay = fabs(y);
if(ax > ay) {
if( x > SHRT_MAX) {
r = SHRT_MAX / x;
} else if(x < SHRT_MIN) {
r = SHRT_MIN / x;
} else {
r = 1.0;
}
} else {
if( y > SHRT_MAX) {
r = SHRT_MAX / y;
} else if(y < SHRT_MIN) {
r = SHRT_MIN / y;
} else {
r = 1.0;
}
}
*sx = x * r;
*sy = y * r;
}
short clip_to_short(double n)
{
return n > SHRT_MAX ? SHRT_MAX : n < SHRT_MIN ? SHRT_MIN : n;
}
int rectclip(int x1,int y1,int x2,int y2,
double *xa,double *ya,double *xb,double *yb)

View File

@ -1361,6 +1361,7 @@ void drawpolygon(int c, int what, double *x, double *y, int points, int poly_fil
double x1,y1,x2,y2;
XPoint *p;
int i;
short sx, sy;
if(!has_x) return;
polygon_bbox(x, y, points, &x1,&y1,&x2,&y2);
@ -1375,8 +1376,9 @@ void drawpolygon(int c, int what, double *x, double *y, int points, int poly_fil
p = my_malloc(38, sizeof(XPoint) * points);
for(i=0;i<points; i++) {
p[i].x = X_TO_SCREEN(x[i]);
p[i].y = Y_TO_SCREEN(y[i]);
clip_xy_to_short(X_TO_SCREEN(x[i]), Y_TO_SCREEN(y[i]), &sx, &sy);
p[i].x = sx;
p[i].y = sy;
}
if(dash) {
char dash_arr[2];
@ -1406,6 +1408,7 @@ void drawtemppolygon(GC g, int what, double *x, double *y, int points)
double x1,y1,x2,y2;
XPoint *p;
int i;
short sx, sy;
if(!has_x) return;
polygon_bbox(x, y, points, &x1,&y1,&x2,&y2);
x1=X_TO_SCREEN(x1);
@ -1415,8 +1418,9 @@ void drawtemppolygon(GC g, int what, double *x, double *y, int points)
p = my_malloc(39, sizeof(XPoint) * points);
if( rectclip(xctx->areax1,xctx->areay1,xctx->areax2,xctx->areay2,&x1,&y1,&x2,&y2) ) {
for(i=0;i<points; i++) {
p[i].x = X_TO_SCREEN(x[i]);
p[i].y = Y_TO_SCREEN(y[i]);
clip_xy_to_short(X_TO_SCREEN(x[i]), Y_TO_SCREEN(y[i]), &sx, &sy);
p[i].x = sx;
p[i].y = sy;
}
XDrawLines(display, xctx->window, g, p, points, CoordModeOrigin);
}

View File

@ -860,8 +860,9 @@ extern void draw_temp_symbol(int what, GC gc, int n,int layer,
extern void draw_temp_string(GC gc,int what, const char *str, short rot, short flip, int hcenter, int vcenter,
double x1, double y1, double xscale, double yscale);
extern void draw(void);
extern short clip_to_short(double n);
extern void clip_xy_to_short(double x, double y, short *sx, short *sy);
extern int clip( double*,double*,double*,double*);
extern int textclip(int x1,int y1,int x2,int y2,
double xa,double ya,double xb,double yb);