Tracer des diagrammes dans un terminal - retour accueil
git clone git://bebou.netlib.re/boxes
Log | Files | Refs | README |
commit 2ba4be03ba59399e94a058045c1753bb8b3c68ec parent ea7598cd3e65bec11dbf95dd798e43b7888e93b0 Auteurice: Arthur Pons <arthur.pons@unistra.fr> Date: Sat, 14 Jun 2025 13:50:17 +0200 On peut tracer des flèches Le bout des flèches est pas correct Beaucoup de copier/coller, faudrait que j'apprenne à facto ce genre de trucs Diffstat:
M | README | | | 3 | ++- |
M | boxes.c | | | 94 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------ |
2 files changed, 82 insertions(+), 15 deletions(-)
diff --git a/README b/README @@ -11,7 +11,8 @@ Aujourd'hui ne permet que de tracer 100 boites maximum * ctrl+z pour supprimer la dernière boite * entrée pour quitter * clique droit sur le coin d'origine d'une boite pour la supprimer - + * a pour tracer des flèches + * b pour tracer des boites (mode par défaut) ## Ce qu'il n'y a pas diff --git a/boxes.c b/boxes.c @@ -4,7 +4,9 @@ #define MAX_BOX_NB 100 struct box { int x; int y; int h; int w; }; -struct pile { struct box* list; int count; }; +struct arrow { int startx; int starty; int endx; int endy; }; +struct boxpile { struct box* list; int count; }; +struct arrowpile { struct arrow* list; int count; }; void drawboxcoord(int x, int y, int w, int h) { if(w<0) { x=x+w; w=-w; } @@ -26,16 +28,51 @@ void drawboxcoord(int x, int y, int w, int h) { void drawbox(struct box b) { drawboxcoord(b.x,b.y,b.w,b.h); } +void drawarrowcoord(int startx, int starty, int endx, int endy) { + int i=0; int j=0; signed int nx; signed int ny; + if(endx<startx) nx=-1; else nx=1; + if(endy<starty) ny=-1; else ny=1; + for(i=0;i<abs(startx-endx);i++) { + tb_printf(startx+nx*i,starty+ny*j,0,0,"─"); + } + if(ny==1 && nx==1) tb_printf(startx+nx*i,starty,0,0,"╮"); + else if(ny==1 && nx==-1) tb_printf(startx+nx*i,starty,0,0,"╭"); + else if(ny==-1 && nx==1) tb_printf(startx+nx*i,starty,0,0,"╯"); + else tb_printf(startx+nx*i,starty,0,0,"╰"); + for(int j=1;j<abs(starty-endy);j++) { + tb_printf(startx+nx*i,starty+ny*j,0,0,"│"); + } + tb_printf(endx,endy,0,0,"→"); + tb_printf(0,0,TB_DEFAULT,TB_DEFAULT,"start: x%d,y%d, end: x%d,y%d",startx,starty,endx,endy); +} + +void drawarrow(struct arrow a) { drawarrowcoord(a.startx,a.starty,a.endx,a.endy); } + +struct arrow createarrow(int startx, int starty, int endx, int endy) { + struct arrow a; + a.startx=startx; a.starty=starty; a.endx=endx; a.endy=endy; return a; +} + +void addarrow(struct arrowpile *arrows, struct arrow a) { + arrows->list[arrows->count]=a; + arrows->count++; +} + +void deletearrow(struct arrowpile *arrows, int index) { + for (int i=index;i<arrows->count-1;i++) arrows->list[i]=arrows->list[i+1]; + if(arrows->count>0) arrows->count--; +} + struct box createbox(int x, int y, int w, int h) { struct box b; b.x=x; b.y=y; b.w=w; b.h=h; return b; } -void addbox(struct pile *boxes, struct box b) { +void addbox(struct boxpile *boxes, struct box b) { boxes->list[boxes->count]=b; boxes->count++; } -void deletebox(struct pile *boxes, int index) { +void deletebox(struct boxpile *boxes, int index) { for (int i=index;i<boxes->count-1;i++) boxes->list[i]=boxes->list[i+1]; if(boxes->count>0) boxes->count--; } @@ -44,31 +81,60 @@ int main(int argc, char **argv) { tb_init(); tb_hide_cursor(); tb_set_input_mode(TB_INPUT_ESC | TB_INPUT_MOUSE); + int mode=0; struct tb_event ev; - struct pile boxes; boxes.list=malloc(MAX_BOX_NB*sizeof(struct box)); boxes.count=0; + struct boxpile boxes; boxes.list=malloc(MAX_BOX_NB*sizeof(struct box)); boxes.count=0; + struct arrowpile arrows; arrows.list=malloc(MAX_BOX_NB*sizeof(struct arrow)); arrows.count=0; int curx; int cury; while(1) { tb_clear(); for (int i=0;i<boxes.count;i++) drawbox(boxes.list[i]); - tb_printf(0,tb_height()-1,TB_DEFAULT,TB_DEFAULT,"count: %d",boxes.count); + for (int i=0;i<arrows.count;i++) drawarrow(arrows.list[i]); + tb_printf(0,tb_height()-1,TB_DEFAULT,TB_DEFAULT,"count: %d, mode: %d",boxes.count,mode); tb_present(); tb_poll_event(&ev); + switch(ev.ch) { + case 97: /*a*/ + mode=1; + break; + case 98: /*b*/ + mode=0; + break; + } switch(ev.key) { case 13: /*Enter*/ tb_shutdown(); free(boxes.list); return 0; case 65512: /*MouseLeft*/ - if(boxes.count==MAX_BOX_NB) break; - curx=ev.x; cury=ev.y; - while(ev.key!=65509) { /*MouseRelease*/ - tb_clear(); - for (int i=0;i<boxes.count;i++) drawbox(boxes.list[i]); - drawboxcoord(curx,cury,ev.x-curx,ev.y-cury); - tb_present(); - tb_poll_event(&ev); + switch(mode) { + case 0: + if(boxes.count==MAX_BOX_NB) break; + curx=ev.x; cury=ev.y; + while(ev.key!=65509) { /*MouseRelease*/ + tb_clear(); + for (int i=0;i<boxes.count;i++) drawbox(boxes.list[i]); + for (int i=0;i<arrows.count;i++) drawarrow(arrows.list[i]); + drawboxcoord(curx,cury,ev.x-curx,ev.y-cury); + tb_present(); + tb_poll_event(&ev); + } + addbox(&boxes,createbox(curx,cury,ev.x-curx,ev.y-cury)); + break; + case 1: + if(arrows.count==MAX_BOX_NB) break; + curx=ev.x; cury=ev.y; + while(ev.key!=65509) { /*MouseRelease*/ + tb_clear(); + for (int i=0;i<boxes.count;i++) drawbox(boxes.list[i]); + for (int i=0;i<arrows.count;i++) drawarrow(arrows.list[i]); + drawarrowcoord(curx,cury,ev.x,ev.y); + tb_present(); + tb_poll_event(&ev); + } + addarrow(&arrows,createarrow(curx,cury,ev.x,ev.y)); + break; } - addbox(&boxes,createbox(curx,cury,ev.x-curx,ev.y-cury)); break; case 26: /*ctrl+z*/ deletebox(&boxes,boxes.count-1);