drawline.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /****** drawline.c *****************************************************
  2. Function to draw a line segment defined by points (row, col)
  3. and (backrow, backcol) using Bresenham's Algorithm.
  4. Note: do not draw the end point (backrow, backcol)
  5. except when it is the same as the beginning point
  6. (row, col) in order to be able utilize path segments
  7. drawn before the current path segment.
  8. ***********************************************************************/
  9. #include <grass/segment.h>
  10. void drawline(int x1, int y1, int x2, int y2)
  11. {
  12. extern char *value;
  13. extern SEGMENT out_seg;
  14. int dx, dy, i, e;
  15. int incx, incy, inc1, inc2;
  16. int x, y;
  17. int data = 1;
  18. /*debug: printf("\n(%d,%d)->(%d,%d): ", x1,y1,x2,y2); */
  19. dx = x2 - x1;
  20. dy = y2 - y1;
  21. incx = 1;
  22. incy = 1;
  23. if (dx < 0) {
  24. dx = -dx;
  25. incx = -1;
  26. }
  27. if (dy < 0) {
  28. dy = -dy;
  29. incy = -1;
  30. }
  31. x = x1;
  32. y = y1;
  33. value = (char *)&data;
  34. if (dx > dy) {
  35. Segment_put(&out_seg, value, x, y);
  36. /*debug: printf("put1-(%d,%d) ",x,y); */
  37. e = 2 * dy - dx;
  38. inc1 = 2 * (dy - dx);
  39. inc2 = 2 * dy;
  40. for (i = 0; i < dx - 1; i++) {
  41. if (e >= 0) {
  42. y += incy;
  43. e += inc1;
  44. }
  45. else
  46. e += inc2;
  47. x += incx;
  48. Segment_put(&out_seg, value, x, y);
  49. /*debug:printf("put2-(%d,%d) ",x,y); */
  50. }
  51. }
  52. else {
  53. Segment_put(&out_seg, value, x, y);
  54. /*debug:printf("put3-(%d,%d) ",x,y); */
  55. e = 2 * dx - dy;
  56. inc1 = 2 * (dx - dy);
  57. inc2 = 2 * dx;
  58. for (i = 0; i < dy - 1; i++) {
  59. if (e >= 0) {
  60. x += incx;
  61. e += inc1;
  62. }
  63. else
  64. e += inc2;
  65. y += incy;
  66. Segment_put(&out_seg, value, x, y);
  67. /*debug:rintf("put4-(%d,%d) ",x,y); */
  68. }
  69. }
  70. }