home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xfig2.8 / part10 / ellipse.c next >
C/C++ Source or Header  |  1990-07-03  |  10KB  |  427 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. extern int        line_style, line_thickness;
  18. extern float        cur_styleval;
  19. extern float        cur_angle;
  20. extern int        cur_color;
  21. extern int        cur_areafill;
  22. extern int        fill_mode;
  23. extern int        fix_x, fix_y, cur_x, cur_y;
  24.  
  25. extern            (*canvas_kbd_proc)();
  26. extern            (*canvas_locmove_proc)();
  27. extern            (*canvas_leftbut_proc)();
  28. extern            (*canvas_middlebut_proc)();
  29. extern            (*canvas_rightbut_proc)();
  30. extern            null_proc();
  31. extern            set_popupmenu();
  32.  
  33. extern F_compound    objects;
  34.  
  35. /*************************  local procedures  ********************/
  36.  
  37. extern int        init_ellipsebyradius_drawing();
  38. extern int        init_ellipsebydiameter_drawing();
  39. extern int        init_circlebyradius_drawing();
  40. extern int        init_circlebydiameter_drawing();
  41. extern int        move_ebrbox(), move_ebdbox();
  42. extern int        move_cbrbox(), move_cbdbox();
  43. extern int        create_ellipsebydia();
  44. extern int        create_ellipsebyrad();
  45. extern int        create_circlebyrad();
  46. extern int        create_circlebydia();
  47.  
  48. #define        round(z)    (int)((z)+.5)
  49.  
  50. center_marker(x, y)
  51. int    x, y;
  52. {
  53.     pw_vector(canvas_win, x, y-2, x, y+2, INV_PAINT, 1, SOLID_LINE, 0.0);
  54.     pw_vector(canvas_win, x-2, y, x+2, y, INV_PAINT, 1, SOLID_LINE, 0.0);
  55.     }
  56.  
  57. ellipsebyradius_drawing_selected()
  58. {
  59.     canvas_kbd_proc = null_proc;
  60.     canvas_locmove_proc = null_proc;
  61.     canvas_leftbut_proc = init_ellipsebyradius_drawing;
  62.     canvas_middlebut_proc = null_proc;
  63.     canvas_rightbut_proc = set_popupmenu;
  64.     set_cursor(&arrow_cursor);
  65.     reset_action_on();
  66.     }
  67.  
  68. init_ellipsebyradius_drawing(x, y)
  69. int    x, y;
  70. {
  71.     cur_x = fix_x = x; 
  72.     cur_y = fix_y = y;
  73.     center_marker(fix_x, fix_y);
  74.     canvas_locmove_proc = move_ebrbox;
  75.     canvas_leftbut_proc = canvas_rightbut_proc = null_proc;
  76.     canvas_middlebut_proc = create_ellipsebyrad;
  77.     set_temp_cursor(&null_cursor);
  78.     ellipsebyrad_box(INV_PAINT);
  79.     set_action_on();
  80.     }
  81.  
  82. move_ebrbox(x, y)
  83. int    x, y;
  84. {
  85.     ellipsebyrad_box(INV_PAINT);
  86.     cur_x = x;
  87.     cur_y = y;
  88.     ellipsebyrad_box(INV_PAINT);
  89.     }
  90.  
  91. ellipsebyrad_box(op)
  92. int    op;
  93. {
  94.     register int    x1, y1, x2, y2;
  95.     int        rx, ry;
  96.  
  97.     rx = cur_x - fix_x;
  98.     ry = cur_y - fix_y;
  99.     x1 = fix_x + rx;
  100.     x2 = fix_x - rx;
  101.     y1 = fix_y + ry;
  102.     y2 = fix_y - ry;
  103.     draw_rectbox(x1, y1, x2, y2, op);
  104.     }
  105.  
  106. create_ellipsebyrad(x, y)
  107. int    x, y;
  108. {
  109.     F_ellipse    *ellipse;
  110.  
  111.     ellipsebyrad_box(INV_PAINT);
  112.     center_marker(fix_x, fix_y);
  113.     if (NULL == (Ellipse_malloc(ellipse))) {
  114.         blink_msg();
  115.         put_msg(Err_mem);
  116.         return;
  117.         }
  118.     ellipse->type = T_ELLIPSE_BY_RAD;
  119.     ellipse->style = line_style;
  120.     ellipse->thickness = line_thickness;
  121.     ellipse->style_val = cur_styleval;
  122.     ellipse->angle = cur_angle;
  123.     ellipse->color = cur_color;
  124.     ellipse->depth = 0;
  125.     ellipse->pen = 0;
  126.     ellipse->area_fill = fill_mode? cur_areafill : 0;
  127.     ellipse->direction = 1;
  128.     ellipse->center.x = fix_x;
  129.     ellipse->center.y = fix_y;
  130.     ellipse->radiuses.x = abs(x - fix_x) +1;
  131.     ellipse->radiuses.y = abs(y - fix_y) +1;
  132.     ellipse->start.x = fix_x;
  133.     ellipse->start.y = fix_y;
  134.     ellipse->end.x = x;
  135.     ellipse->end.y = y;
  136.     ellipse->next = NULL;
  137.     draw_ellipse(ellipse, DRAW);
  138.     clean_up();
  139.     set_action_object(F_CREATE, O_ELLIPSE);
  140.     insert_ellipse(&objects.ellipses, ellipse);
  141.     set_latestellipse(ellipse);
  142.     set_modifiedflag();
  143.     ellipsebyradius_drawing_selected();
  144.     }
  145.  
  146. ellipsebydiameter_drawing_selected()
  147. {
  148.     canvas_kbd_proc = null_proc;
  149.     canvas_locmove_proc = null_proc;
  150.     canvas_leftbut_proc = init_ellipsebydiameter_drawing;
  151.     canvas_middlebut_proc = null_proc;
  152.     canvas_rightbut_proc = set_popupmenu;
  153.     set_cursor(&arrow_cursor);
  154.     reset_action_on();
  155.     }
  156.  
  157. init_ellipsebydiameter_drawing(x, y)
  158. int    x, y;
  159. {
  160.     cur_x = fix_x = x; 
  161.     cur_y = fix_y = y;
  162.     center_marker(fix_x, fix_y);
  163.     canvas_locmove_proc = move_ebdbox;
  164.     canvas_leftbut_proc = null_proc;
  165.     canvas_middlebut_proc = create_ellipsebydia;
  166.     set_temp_cursor(&null_cursor);
  167.     ellipsebydia_box(INV_PAINT);
  168.     set_action_on();
  169.     }
  170.  
  171. move_ebdbox(x, y)
  172. int    x, y;
  173. {
  174.     ellipsebydia_box(INV_PAINT);
  175.     cur_x = x;
  176.     cur_y = y;
  177.     ellipsebydia_box(INV_PAINT);
  178.     }
  179.  
  180. ellipsebydia_box(op)
  181. int    op;
  182. {
  183.     draw_rectbox(fix_x, fix_y, cur_x, cur_y, op);
  184.     }
  185.  
  186. create_ellipsebydia(x, y)
  187. int    x, y;
  188. {
  189.     F_ellipse    *ellipse;
  190.  
  191.     ellipsebydia_box(INV_PAINT);
  192.     center_marker(fix_x, fix_y);
  193.     if (NULL == (Ellipse_malloc(ellipse))) {
  194.         blink_msg();
  195.         put_msg(Err_mem);
  196.         return;
  197.         }
  198.     ellipse->type = T_ELLIPSE_BY_DIA;
  199.     ellipse->style = line_style;
  200.     ellipse->thickness = line_thickness;
  201.     ellipse->style_val = cur_styleval;
  202.     ellipse->angle = cur_angle;
  203.     ellipse->color = cur_color;
  204.     ellipse->depth = 0;
  205.     ellipse->pen = NULL;
  206.     /* added 3/1/89 B.V.Smith */
  207.     ellipse->area_fill = fill_mode? cur_areafill : 0;
  208.     ellipse->direction = 1;
  209.     ellipse->center.x = (fix_x + x) / 2;
  210.     ellipse->center.y = (fix_y + y) / 2;
  211.     ellipse->radiuses.x = abs(ellipse->center.x - fix_x);
  212.     ellipse->radiuses.y = abs(ellipse->center.y - fix_y);
  213.     ellipse->start.x = fix_x;
  214.     ellipse->start.y = fix_y;
  215.     ellipse->end.x = x;
  216.     ellipse->end.y = y;
  217.     ellipse->next = NULL;
  218.     draw_ellipse(ellipse, DRAW);
  219.     clean_up();
  220.     set_action_object(F_CREATE, O_ELLIPSE);
  221.     insert_ellipse(&objects.ellipses, ellipse);
  222.     set_latestellipse(ellipse);
  223.     set_modifiedflag();
  224.     ellipsebydiameter_drawing_selected();
  225.     }
  226.  
  227. /***************************  circle  section  ************************/
  228.  
  229. circlebyradius_drawing_selected()
  230. {
  231.     canvas_kbd_proc = null_proc;
  232.     canvas_locmove_proc = null_proc;
  233.     canvas_leftbut_proc = init_circlebyradius_drawing;
  234.     canvas_middlebut_proc = null_proc;
  235.     canvas_rightbut_proc = set_popupmenu;
  236.     set_cursor(&arrow_cursor);
  237.     reset_action_on();
  238.     }
  239.  
  240. init_circlebyradius_drawing(x, y)
  241. int    x, y;
  242. {
  243.     cur_x = fix_x = x; 
  244.     cur_y = fix_y = y;
  245.     center_marker(fix_x, fix_y);
  246.     canvas_locmove_proc = move_cbrbox;
  247.     canvas_leftbut_proc = null_proc;
  248.     canvas_middlebut_proc = create_circlebyrad;
  249.     set_temp_cursor(&null_cursor);
  250.     circlebyrad_box(INV_PAINT);
  251.     set_action_on();
  252.     }
  253.  
  254. move_cbrbox(x, y)
  255. int    x, y;
  256. {
  257.     circlebyrad_box(INV_PAINT);
  258.     cur_x = x;
  259.     cur_y = y;
  260.     circlebyrad_box(INV_PAINT);
  261.     }
  262.  
  263. circlebyrad_box(op)
  264. int    op;
  265. {
  266.     register int    x1, y1, x2, y2;
  267.     int        radius, rx, ry;
  268.  
  269.     rx = cur_x - fix_x;
  270.     ry = cur_y - fix_y;
  271.     radius = round(sqrt((double) (rx*rx + ry*ry)));
  272.     x1 = fix_x + radius;
  273.     x2 = fix_x - radius;
  274.     y1 = fix_y + radius;
  275.     y2 = fix_y - radius;
  276.     draw_rectbox(x1, y1, x2, y2, op);
  277.     pw_vector(canvas_win, fix_x, fix_y, cur_x, cur_y, op, 1, SOLID_LINE, 0.0);
  278.     }
  279.  
  280. create_circlebyrad(x, y)
  281. int    x, y;
  282. {
  283.     F_ellipse    *c;
  284.     int        rx, ry;
  285.  
  286.     circlebyrad_box(INV_PAINT);
  287.     center_marker(fix_x, fix_y);
  288.     if (NULL == (Ellipse_malloc(c))) {
  289.         blink_msg();
  290.         put_msg(Err_mem);
  291.         return;
  292.         }
  293.     c->type = T_CIRCLE_BY_RAD;
  294.     c->style = line_style;
  295.     c->thickness = line_thickness;
  296.     c->style_val = cur_styleval;
  297.     c->angle = 0.0;
  298.     c->color = cur_color;
  299.     c->depth = 0;
  300.     c->pen = NULL;
  301.     /* added 3/1/89 B.V.Smith */
  302.     c->area_fill = fill_mode? cur_areafill : 0;
  303.     c->direction = 1;
  304.     c->center.x = fix_x;
  305.     c->center.y = fix_y;
  306.     rx = fix_x - x;  ry = fix_y - y;
  307.     c->radiuses.x = c->radiuses.y = round(sqrt((double)(rx*rx + ry*ry)));
  308.     c->start.x = fix_x;
  309.     c->start.y = fix_y;
  310.     c->end.x = x;
  311.     c->end.y = y;
  312.     c->next = NULL;
  313.     draw_ellipse(c, DRAW);
  314.     clean_up();
  315.     set_action_object(F_CREATE, O_ELLIPSE);
  316.     insert_ellipse(&objects.ellipses, c);
  317.     set_latestellipse(c);
  318.     set_modifiedflag();
  319.     circlebyradius_drawing_selected();
  320.     }
  321.  
  322. circlebydiameter_drawing_selected()
  323. {
  324.     canvas_kbd_proc = null_proc;
  325.     canvas_locmove_proc = null_proc;
  326.     canvas_leftbut_proc = init_circlebydiameter_drawing;
  327.     canvas_middlebut_proc = null_proc;
  328.     canvas_rightbut_proc = set_popupmenu;
  329.     set_cursor(&arrow_cursor);
  330.     reset_action_on();
  331.     }
  332.  
  333. init_circlebydiameter_drawing(x, y)
  334. int    x, y;
  335. {
  336.     cur_x = fix_x = x; 
  337.     cur_y = fix_y = y;
  338.     center_marker(fix_x, fix_y);
  339.     canvas_locmove_proc = move_cbdbox;
  340.     canvas_leftbut_proc = null_proc;
  341.     canvas_middlebut_proc = create_circlebydia;
  342.     set_temp_cursor(&null_cursor);
  343.     circlebydia_box(INV_PAINT);
  344.     set_action_on();
  345.     }
  346.  
  347. move_cbdbox(x, y)
  348. int    x, y;
  349. {
  350.     circlebydia_box(INV_PAINT);
  351.     cur_x = x;
  352.     cur_y = y;
  353.     circlebydia_box(INV_PAINT);
  354.     }
  355.  
  356. circlebydia_box(op)
  357. int    op;
  358. {
  359.     register int    x1, y1, x2, y2;
  360.     int        radius, rx, ry;
  361.  
  362.     rx = (cur_x - fix_x) / 2;
  363.     ry = (cur_y - fix_y) / 2;
  364.     radius = round(sqrt((double) (rx*rx + ry*ry)));
  365.     x1 = fix_x + rx + radius;
  366.     x2 = fix_x + rx - radius;
  367.     y1 = fix_y + ry + radius;
  368.     y2 = fix_y + ry - radius;
  369.     draw_rectbox(x1, y1, x2, y2, op);
  370.     pw_vector(canvas_win, fix_x, fix_y, cur_x, cur_y, op, 1, SOLID_LINE, 0.0);
  371.     }
  372.  
  373. create_circlebydia(x, y)
  374. int    x, y;
  375. {
  376.     F_ellipse    *c;
  377.     int        rx, ry;
  378.  
  379.     circlebydia_box(INV_PAINT);
  380.     center_marker(fix_x, fix_y);
  381.     if (NULL == (Ellipse_malloc(c))) {
  382.         blink_msg();
  383.         put_msg(Err_mem);
  384.         return;
  385.         }
  386.     c->type = T_CIRCLE_BY_DIA;
  387.     c->style = line_style;
  388.     c->thickness = line_thickness;
  389.     c->style_val = cur_styleval;
  390.     c->angle = 0.0;
  391.     c->color = cur_color;
  392.     c->depth = 0;
  393.     c->pen = NULL;
  394.     /* added 3/1/89 B.V.Smith */
  395.     c->area_fill = fill_mode? cur_areafill : 0;
  396.     c->direction = 1;
  397.     c->center.x = (fix_x + x) / 2 + .5;
  398.     c->center.y = (fix_y + y) / 2 + .5;
  399.     rx = x - c->center.x;  ry = y - c->center.y;
  400.     c->radiuses.x = c->radiuses.y = round(sqrt((double)(rx*rx + ry*ry)));
  401.     c->start.x = fix_x;
  402.     c->start.y = fix_y;
  403.     c->end.x = x;
  404.     c->end.y = y;
  405.     c->next = NULL;
  406.     draw_ellipse(c, DRAW);
  407.     clean_up();
  408.     set_action_object(F_CREATE, O_ELLIPSE);
  409.     insert_ellipse(&objects.ellipses, c);
  410.     set_latestellipse(c);
  411.     set_modifiedflag();
  412.     circlebydiameter_drawing_selected();
  413.     }
  414.  
  415. draw_ellipse(e, op)
  416. F_ellipse    *e;
  417. int        op;
  418. {
  419.     int    a, b;
  420.  
  421.     a = e->radiuses.x;
  422.     b = e->radiuses.y;
  423.     curve(canvas_win, a, 0, a, 0, e->direction, (b*b), (a*a),
  424.         e->center.x, e->center.y, op, 
  425.         e->thickness, e->style, e->style_val, e->area_fill);
  426.     }
  427.