/* Edouard.Thiel@lif.univ-mrs.fr */ #include #define NBP 4 #define SUBDIV 2 int pt_x[NBP] = { 10, 60, 20, 35 }, pt_y[NBP] = { 90, 80, 40, 70 }; He_node *princ, *canvas; int canvas_w, canvas_h, coul1, coul2, coul3; void dessin_curseur (Window win, int x, int y) { int l = 4; XDrawRectangle (he_display, win, he_gc, x-l, y-l, l*2, l*2); } void dessin_ligne (Window win, int i, int j, int a, int b, int c, int d) { double r = (double) i / (double) SUBDIV; double s = (double) j / (double) SUBDIV; double lx = (pt_x[b] - pt_x[a]) * r + (pt_x[d] - pt_x[a]) * s; double ly = (pt_y[b] - pt_y[a]) * r + (pt_y[d] - pt_y[a]) * s; XDrawLine (he_display, win, he_gc, pt_x[a]+lx, pt_y[a]+ly, pt_x[c]+lx, pt_y[c]+ly); } void canvas_repaint (He_node *canvas, Window win) { int i, j; XSetForeground (he_display, he_gc, he_white); XFillRectangle (he_display, win, he_gc, 0, 0, canvas_w, canvas_h ); XSetForeground (he_display, he_gc, he_black); for (i = 0; i < NBP; i++) dessin_curseur (win, pt_x[i], pt_y[i]); XSetForeground (he_display, he_gc, coul1); for (i = 0; i <= SUBDIV; i++) for (j = 0; j <= SUBDIV; j++) dessin_ligne (win, i, j, 0, 1, 2, 3); XSetForeground (he_display, he_gc, coul2); for (i = 0; i <= SUBDIV; i++) for (j = 0; j <= SUBDIV; j++) dessin_ligne (win, i, j, 0, 2, 3, 1); XSetForeground (he_display, he_gc, coul3); for (i = 0; i <= SUBDIV; i++) for (j = 0; j <= SUBDIV; j++) dessin_ligne (win, i, j, 0, 3, 1, 2); } int cherche_clic (int xo, int yo) { int i, d = 5; for (i = 0; i < NBP; i++) if ( abs(pt_x[i] - xo) < d && abs(pt_y[i] - yo) < d) return i; return -1; } void canvas_event (He_node *canvas, He_event *hev) { static int n = -1; switch (hev->type) { case ButtonPress : n = cherche_clic (hev->sx, hev->sy); break; case MotionNotify : if (n >= 0) { pt_x[n] = hev->sx; pt_y[n] = hev->sy; HePostRepaint (canvas); } break; case ButtonRelease : if (n >= 0) { if (pt_x[n] < 0) pt_x[n] = 0; else if (pt_x[n] >= canvas_w) pt_x[n] = canvas_w-1; if (pt_y[n] < 0) pt_y[n] = 0; else if (pt_y[n] >= canvas_h) pt_y[n] = canvas_h-1; n = -1; HePostRepaint (canvas); } break; } } void princ_resize (He_node *princ, int width, int height) { HeExpand (canvas, NULL, HE_BOTTOM_RIGHT); } void canvas_resize (He_node *canvas, int width, int height) { int i; for (i = 0; i < NBP; i++) { pt_x[i] = (pt_x[i] * width ) / canvas_w; pt_y[i] = (pt_y[i] * height) / canvas_h; } canvas_w = width; canvas_h = height; } int main (int argc, char *argv[]) { int i; HeInit (&argc, &argv); princ = HeCreateFrame (); HeSetFrameLabel (princ, "Grille cubique"); HeSetFrameResizeProc (princ, princ_resize); canvas = HeCreateCanvas (princ); HeSetCanvasDBuf (canvas, TRUE); HeSetCanvasResizeProc (canvas, canvas_resize); HeSetCanvasRepaintProc (canvas, canvas_repaint); HeSetCanvasEventProc (canvas, canvas_event); HeSetTip (canvas, "Bouton 1 : déplacer les points de contrôle"); coul1 = HeAllocRgb ( 0, 255, 0, he_black); coul2 = HeAllocRgb ( 255, 0, 0, he_black); coul3 = HeAllocRgb ( 0, 0, 255, he_black); /* Init points */ canvas_w = HeGetWidth (canvas); canvas_h = HeGetHeight(canvas); for (i = 0; i < NBP; i++) { pt_x[i] *= canvas_w; pt_x[i] /= 100; pt_y[i] *= canvas_h; pt_y[i] /= 100; } return HeMainLoop (princ); }