home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xfig2.8 / part05 / arc.c next >
Encoding:
C/C++ Source or Header  |  1990-07-02  |  5.2 KB  |  221 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : Aug 1985.
  7.  *
  8.  *    %W%    %G%
  9. */
  10. #include "fig.h"
  11. #include "resources.h"
  12. #include "alloc.h"
  13. #include "func.h"
  14. #include "object.h"
  15. #include "paintop.h"
  16.  
  17. #define            PI        3.14159
  18.  
  19. extern int        foreground_color, background_color;
  20. extern int        fix_x, fix_y, cur_x, cur_y;
  21.  
  22. extern int        autoforwardarrow_mode, autobackwardarrow_mode;
  23. extern int        line_style, line_thickness;
  24. extern float        cur_styleval;
  25. extern int        cur_color;
  26. extern int        cur_areafill;
  27. extern int        fill_mode;
  28. extern float        cur_dashlength;
  29. extern int        num_point;
  30. extern F_compound    objects;
  31. extern appresStruct    appres;
  32.  
  33. extern            freehand_elasticline();
  34. extern            (*canvas_kbd_proc)();
  35. extern            (*canvas_locmove_proc)();
  36. extern            (*canvas_leftbut_proc)();
  37. extern            (*canvas_middlebut_proc)();
  38. extern            (*canvas_rightbut_proc)();
  39. extern            null_proc();
  40. extern            set_popupmenu();
  41.  
  42. F_pos            point[3];
  43.  
  44. extern int        create_arcobject();
  45. extern int        get_arcpoint();
  46. extern int        init_arc_drawing();
  47.  
  48. arc_drawing_selected()
  49. {
  50.     canvas_kbd_proc = null_proc;
  51.     canvas_locmove_proc = null_proc;
  52.     canvas_leftbut_proc = init_arc_drawing;
  53.     canvas_middlebut_proc = null_proc;
  54.     canvas_rightbut_proc = set_popupmenu;
  55.     set_cursor(&arrow_cursor);
  56.     reset_action_on();
  57.     }
  58.  
  59. init_arc_drawing(x, y)
  60. int    x, y;
  61. {
  62.     num_point = 0;
  63.     point[num_point].x = fix_x = cur_x = x;
  64.     point[num_point++].y = fix_y = cur_y = y;
  65.     canvas_locmove_proc = freehand_elasticline;    /* in line.c  */
  66.     canvas_leftbut_proc = get_arcpoint;
  67.     canvas_middlebut_proc = create_arcobject;
  68.     draw_elasticline();        /* in line.c  */
  69.     set_temp_cursor(&null_cursor);
  70.     set_action_on();
  71.     }
  72.  
  73. get_arcpoint(x, y)
  74. int    x, y;
  75. {
  76.     if (x == fix_x && y == fix_y) return;
  77.  
  78.     if (num_point == 2)
  79.     {
  80.         create_arcobject(x, y);
  81.         return;
  82.     }
  83.     draw_elasticline();        /* in line.c  */
  84.     cur_x = x;  cur_y = y;
  85.     draw_elasticline();        /* in line.c  */
  86.     point[num_point].x = fix_x = x;
  87.     point[num_point++].y = fix_y = y;
  88.     draw_elasticline();        /* in line.c  */
  89.     }
  90.  
  91. create_arcobject(lx, ly)
  92. int    lx, ly;
  93. {
  94.     extern F_arrow    *forward_arrow(), *backward_arrow();
  95.     F_arc        *arc;
  96.     int        x, y, i;
  97.     float        xx, yy;
  98.  
  99.     draw_elasticline();
  100.     cur_x = lx;  cur_y = ly;
  101.     draw_elasticline();        /* in line.c  */
  102.     if (num_point == 1) {
  103.         arc_drawing_selected();
  104.         return;
  105.         }
  106.     else if (num_point == 2) {
  107.         point[num_point].x = lx;
  108.         point[num_point++].y = ly;
  109.         }
  110.  
  111.     x = point[0].x;  y = point[0].y;
  112.     for (i = 1; i < num_point; i++) {
  113.         pw_vector(canvas_win, x, y, point[i].x, point[i].y, INV_PAINT,
  114.             1, SOLID_LINE, 0.0);
  115.         x = point[i].x;  y = point[i].y;
  116.         }
  117.     if (num_point < 3) {
  118.         arc_drawing_selected();
  119.         return;
  120.         }
  121.     if (! compute_arccenter(point[0], point[1], point[2], &xx, &yy)) {
  122.         arc_drawing_selected();
  123.         return;
  124.         }
  125.     Arc_malloc(arc);
  126.     if (arc == NULL) {
  127.         blink_msg();
  128.         put_msg(Err_mem);
  129.         arc_drawing_selected();
  130.         return;
  131.         }
  132.     arc->type = T_3_POINTS_ARC;
  133.     arc->style = line_style;
  134.     arc->thickness = line_thickness;
  135.     arc->style_val = cur_styleval;
  136.     arc->pen = 0;
  137.     arc->area_fill = fill_mode? cur_areafill : 0;
  138.     arc->color = cur_color;
  139.     arc->depth = 0;
  140.     arc->direction = compute_direction(point[0], point[1], point[2]);
  141.     if (autoforwardarrow_mode)
  142.         arc->for_arrow = forward_arrow();
  143.     else
  144.         arc->for_arrow = NULL;
  145.     if (autobackwardarrow_mode)
  146.         arc->back_arrow = backward_arrow();
  147.     else
  148.         arc->back_arrow = NULL;
  149.     arc->center.x = xx;
  150.     arc->center.y = yy;
  151.     arc->point[0].x = point[0].x;
  152.     arc->point[0].y = point[0].y;
  153.     arc->point[1].x = point[1].x;
  154.     arc->point[1].y = point[1].y;
  155.     arc->point[2].x = point[2].x;
  156.     arc->point[2].y = point[2].y;
  157.     arc->next = NULL;
  158.     draw_arc(arc, foreground_color);
  159.     if (appres.DEBUG) {
  160.         int        xmin, ymin, xmax, ymax;
  161.         arc_bound(arc, &xmin, &ymin, &xmax, &ymax);
  162.         draw_rectbox(xmin, ymin, xmax, ymax, PAINT);
  163.         }
  164.     clean_up();
  165.     set_action_object(F_CREATE, O_ARC);
  166.     insert_arc(&objects.arcs, arc);
  167.     set_latestarc(arc);
  168.     set_modifiedflag();
  169.     arc_drawing_selected();
  170.     }
  171.  
  172. #define             round(x)    ((int)((x) + .5))
  173.  
  174. draw_arc(a, op)
  175. F_arc    *a;
  176. int    op;
  177. {
  178.     int radius, rx, ry;
  179.  
  180.     rx = a->point[0].x - a->center.x;
  181.     ry = a->center.y - a->point[0].y;
  182.     radius = round(sqrt((double) (rx*rx + ry*ry)));
  183.  
  184.     curve(canvas_win, round(a->point[0].x - a->center.x),
  185.         round(a->center.y - a->point[0].y),
  186.         round(a->point[2].x - a->center.x),
  187.         round(a->center.y - a->point[2].y),
  188.         a->direction, radius, radius,
  189.         round(a->center.x), round(a->center.y), op,
  190.         a->thickness, a->style, a->style_val, a->area_fill);
  191.     draw_arcarrow(a, op);
  192.     }
  193.  
  194. draw_arcarrow(a, op)
  195. F_arc    *a;
  196. int    op;
  197. {
  198.     int    x, y;
  199.  
  200.     if (a->for_arrow) {
  201.         compute_normal(a->center.x, a->center.y, a->point[2].x, 
  202.             a->point[2].y, a->direction, &x, &y);
  203.         if (op == foreground_color)
  204.         draw_arrow(x, y, a->point[2].x, a->point[2].y,
  205.                 a->for_arrow, PAINT);
  206.         else
  207.         draw_arrow(x, y, a->point[2].x, a->point[2].y,
  208.                 a->for_arrow, ERASE);
  209.         }
  210.     if (a->back_arrow) {
  211.         compute_normal(a->center.x, a->center.y, a->point[0].x, 
  212.             a->point[0].y, a->direction ^ 1, &x, &y);
  213.         if (op == foreground_color)
  214.         draw_arrow(x, y, a->point[0].x, a->point[0].y,
  215.                 a->back_arrow, PAINT);
  216.         else
  217.         draw_arrow(x, y, a->point[0].x, a->point[0].y,
  218.                 a->back_arrow, ERASE);
  219.         }
  220.     }
  221.