home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xfig / part02 / d_regpoly.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-27  |  3.3 KB  |  126 lines

  1. /*
  2.  * FIG : Facility for Interactive Generation of figures
  3.  * Copyright (c) 1985 by Supoj Sutanthavibul
  4.  *
  5.  * "Permission to use, copy, modify, distribute, and sell this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both the copyright
  8.  * notice and this permission notice appear in supporting documentation. 
  9.  * No representations are made about the suitability of this software for 
  10.  * any purpose.  It is provided "as is" without express or implied warranty."
  11.  */
  12.  
  13. #include "fig.h"
  14. #include "resources.h"
  15. #include "mode.h"
  16. #include "object.h"
  17. #include "paintop.h"
  18. #include "u_create.h"
  19. #include "u_elastic.h"
  20. #include "u_list.h"
  21. #include "w_canvas.h"
  22. #include "w_mousefun.h"
  23.  
  24. extern float    compute_angle();
  25.  
  26. /*************************** local declarations *********************/
  27.  
  28. static        init_regpoly_drawing(), create_regpoly(), cancel_regpoly();
  29.  
  30. regpoly_drawing_selected()
  31. {
  32.     set_mousefun("center point", "", "");
  33.     canvas_kbd_proc = null_proc;
  34.     canvas_locmove_proc = null_proc;
  35.     canvas_leftbut_proc = init_regpoly_drawing;
  36.     canvas_middlebut_proc = null_proc;
  37.     canvas_rightbut_proc = null_proc;
  38.     set_cursor(arrow_cursor);
  39.     reset_action_on();
  40. }
  41.  
  42. static
  43. init_regpoly_drawing(x, y)
  44.     int            x, y;
  45. {
  46.     cur_x = fix_x = x;
  47.     cur_y = fix_y = y;
  48.     work_numsides = cur_numsides;
  49.     set_mousefun("final point", "", "cancel");
  50.     draw_mousefun_canvas();
  51.     canvas_locmove_proc = resizing_poly;
  52.     canvas_leftbut_proc = create_regpoly;
  53.     canvas_middlebut_proc = null_proc;
  54.     canvas_rightbut_proc = cancel_regpoly;
  55.     elastic_poly(fix_x, fix_y, cur_x, cur_y, work_numsides);
  56.     set_temp_cursor(null_cursor);
  57.     set_action_on();
  58. }
  59.  
  60. static
  61. cancel_regpoly()
  62. {
  63.     elastic_poly(fix_x, fix_y, cur_x, cur_y, work_numsides);
  64.     regpoly_drawing_selected();
  65.     draw_mousefun_canvas();
  66. }
  67.  
  68. static
  69. create_regpoly(x, y)
  70.     int            x, y;
  71. {
  72.     register float  angle;
  73.     register int    nx, ny, dx, dy, i;
  74.     float        init_angle, mag;
  75.     F_line       *poly;
  76.     F_point       *point;
  77.  
  78.     elastic_poly(fix_x, fix_y, cur_x, cur_y, work_numsides);
  79.     if (fix_x == x && fix_y == y)
  80.     return;            /* 0 size */
  81.  
  82.     if ((point = create_point()) == NULL)
  83.     return;
  84.  
  85.     point->x = x;
  86.     point->y = y;
  87.     point->next = NULL;
  88.  
  89.     if ((poly = create_line()) == NULL) {
  90.     free((char *) point);
  91.     return;
  92.     }
  93.     poly->type = T_POLYGON;
  94.     poly->style = cur_linestyle;
  95.     poly->thickness = cur_linewidth;
  96.     poly->color = cur_color;
  97.     poly->depth = cur_depth;
  98.     poly->pen = 0;
  99.     poly->fill_style = cur_fillstyle;
  100.     /* scale dash length by line thickness */
  101.     poly->style_val = cur_styleval * (cur_linewidth + 1) / 2;
  102.     poly->radius = 0;
  103.     poly->points = point;
  104.  
  105.     dx = x - fix_x;
  106.     dy = y - fix_y;
  107.     mag = sqrt((double) (dx * dx + dy * dy));
  108.     init_angle = compute_angle((float) dx, (float) dy);
  109.  
  110.     /* now append cur_numsides points */
  111.     for (i = 1; i < cur_numsides; i++) {
  112.     angle = init_angle - M_2PI * (float) i / (float) cur_numsides;
  113.     if (angle < 0)
  114.         angle += M_2PI;
  115.     nx = fix_x + round(mag * cos((double) angle));
  116.     ny = fix_y + round(mag * sin((double) angle));
  117.     append_point(nx, ny, &point);
  118.     }
  119.     append_point(x, y, &point);
  120.  
  121.     draw_line(poly, PAINT);
  122.     add_line(poly);
  123.     regpoly_drawing_selected();
  124.     draw_mousefun_canvas();
  125. }
  126.